Send an email automatically when a payment fails.

This commit is contained in:
jakedt 2014-04-22 13:56:34 -04:00
parent 58dbb540a1
commit 189903ffe9
2 changed files with 41 additions and 18 deletions

View file

@ -8,7 +8,7 @@ from data import model
from auth.auth import process_auth
from auth.permissions import ModifyRepositoryPermission
from util.invoice import renderInvoiceToHtml
from util.email import send_invoice_email, send_subscription_change
from util.email import send_invoice_email, send_subscription_change, send_payment_failed
from util.names import parse_repository_name
from util.http import abort
from endpoints.trigger import BuildTrigger, ValidationRequestException
@ -25,27 +25,23 @@ def stripe_webhook():
request_data = request.get_json()
logger.debug('Stripe webhook call: %s' % request_data)
customer_id = request_data.get('data', {}).get('object', {}).get('customer', None)
user = model.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':
data = request_data['data'] if 'data' in request_data else {}
obj = data['object'] if 'object' in data else {}
invoice_id = obj['invoice'] if 'invoice' in obj else None
customer_id = obj['customer'] if 'customer' in obj else None
invoice_id = ['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.email, invoice_html)
if invoice_id and customer_id:
# Find the user associated with the customer ID.
user = model.get_user_or_org_by_customer_id(customer_id)
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.email, invoice_html)
elif event_type.startswith('customer.subscription.'):
customer_id = request_data['data']['object']['customer']
cust = model.get_user_or_org_by_customer_id(customer_id)
cust_email = cust.email if cust is not None else 'unknown@domain.com'
quay_username = cust.username if cust is not None else 'unknown'
cust_email = user.email if user is not None else 'unknown@domain.com'
quay_username = user.username if user is not None else 'unknown'
change_type = ''
if event_type.endswith('.deleted'):
@ -64,6 +60,10 @@ def stripe_webhook():
change_type = 'switched %s -> %s' % (old_plan, new_plan)
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)
return make_response('Okay')

View file

@ -41,6 +41,22 @@ Customer email: <a href="mailto:{2}">{2}</a><br>
Quay user or org name: {3}<br>
"""
PAYMENT_FAILED = """
Hi {0},<br>
<br>
Your recent payment for Quay.io failed, which usually results in our payments processorcanceling
your subscription automatically. If you would like to continue to use Quay.io without interruption,
please add a new card to Quay.io and re-subscribe to your plan.<br>
<br>
You can find the card and subscription management features under your account settings.<br>
<br>
Thanks and have a great day!<br>
<br>
-Quay.io Support<br>
"""
SUBSCRIPTION_CHANGE_TITLE = 'Subscription Change - {0} {1}'
@ -82,3 +98,10 @@ def send_subscription_change(change_description, customer_id, customer_email, qu
msg.html = SUBSCRIPTION_CHANGE.format(change_description, customer_id, customer_email,
quay_username)
mail.send(msg)
def send_payment_failed(customer_email, quay_username):
msg = Message('Quay.io Subscription Payment Failure', sender='support@quay.io',
recipients=[customer_email])
msg.html = PAYMENT_FAILED.format(quay_username)
mail.send(msg)