Add configurable limits for number of builds allowed under a namespace
We also support that limit being increased automatically once a successful billing charge has gone through
This commit is contained in:
parent
62971b7f20
commit
9a452ace11
10 changed files with 125 additions and 33 deletions
|
@ -2,7 +2,7 @@ import logging
|
|||
|
||||
from flask import request, make_response, Blueprint
|
||||
|
||||
from app import billing as stripe
|
||||
from app import billing as stripe, app
|
||||
from data import model
|
||||
from auth.decorators import process_auth
|
||||
from auth.permissions import ModifyRepositoryPermission
|
||||
|
@ -26,22 +26,29 @@ def stripe_webhook():
|
|||
logger.debug('Stripe webhook call: %s', request_data)
|
||||
|
||||
customer_id = request_data.get('data', {}).get('object', {}).get('customer', None)
|
||||
user = model.user.get_user_or_org_by_customer_id(customer_id) if customer_id else None
|
||||
namespace = model.user.get_user_or_org_by_customer_id(customer_id) if customer_id else None
|
||||
|
||||
event_type = request_data['type'] if 'type' in request_data else None
|
||||
if event_type == 'charge.succeeded':
|
||||
invoice_id = request_data['data']['object']['invoice']
|
||||
|
||||
if user and user.invoice_email:
|
||||
# Lookup the invoice.
|
||||
invoice = stripe.Invoice.retrieve(invoice_id)
|
||||
if invoice:
|
||||
invoice_html = renderInvoiceToHtml(invoice, user)
|
||||
send_invoice_email(user.invoice_email_address or user.email, invoice_html)
|
||||
namespace = model.user.get_user_or_org_by_customer_id(customer_id) if customer_id else None
|
||||
if namespace:
|
||||
# Increase the namespace's build allowance, since we had a successful charge.
|
||||
build_maximum = app.config.get('BILLED_NAMESPACE_MAXIMUM_BUILD_COUNT')
|
||||
if build_maximum is not None:
|
||||
model.user.increase_maximum_build_count(namespace, build_maximum)
|
||||
|
||||
if namespace.invoice_email:
|
||||
# Lookup the invoice.
|
||||
invoice = stripe.Invoice.retrieve(invoice_id)
|
||||
if invoice:
|
||||
invoice_html = renderInvoiceToHtml(invoice, namespace)
|
||||
send_invoice_email(namespace.invoice_email_address or namespace.email, invoice_html)
|
||||
|
||||
elif event_type.startswith('customer.subscription.'):
|
||||
cust_email = user.email if user is not None else 'unknown@domain.com'
|
||||
quay_username = user.username if user is not None else 'unknown'
|
||||
cust_email = namespace.email if namespace is not None else 'unknown@domain.com'
|
||||
quay_username = namespace.username if namespace is not None else 'unknown'
|
||||
|
||||
change_type = ''
|
||||
if event_type.endswith('.deleted'):
|
||||
|
@ -63,8 +70,8 @@ def stripe_webhook():
|
|||
send_subscription_change(change_type, customer_id, cust_email, quay_username)
|
||||
|
||||
elif event_type == 'invoice.payment_failed':
|
||||
if user:
|
||||
send_payment_failed(user.email, user.username)
|
||||
if namespace:
|
||||
send_payment_failed(namespace.email, namespace.username)
|
||||
|
||||
return make_response('Okay')
|
||||
|
||||
|
|
Reference in a new issue