From d95c321e28bcc39354277c46460337d381a4cbf8 Mon Sep 17 00:00:00 2001 From: jakedt Date: Tue, 15 Apr 2014 17:00:32 -0400 Subject: [PATCH] Respond to subscription change events so I can stop polling the stripe event list. --- endpoints/webhooks.py | 8 +++++++- util/email.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/endpoints/webhooks.py b/endpoints/webhooks.py index 4bba9a25f..a68d819c7 100644 --- a/endpoints/webhooks.py +++ b/endpoints/webhooks.py @@ -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 +from util.email import send_invoice_email, send_subscription_change from util.names import parse_repository_name from util.http import abort from endpoints.trigger import BuildTrigger, ValidationRequestException @@ -41,6 +41,12 @@ def stripe_webhook(): 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' + send_subscription_change(event_type, customer_id, cust_email, quay_username) return make_response('Okay') diff --git a/util/email.py b/util/email.py index 6b838895e..e4e97ebea 100644 --- a/util/email.py +++ b/util/email.py @@ -34,6 +34,14 @@ not given access. Please disregard this email.
""" +SUBSCRIPTION_CHANGE = """ +Event name: {0}
+Customer id: {1}
+Customer email: {2}
+Quay user or org name: {3}
+""" + + def send_change_email(username, email, token): msg = Message('Quay.io email change. Please confirm your email.', sender='support@quay.io', # Why do I need this? @@ -64,3 +72,11 @@ def send_invoice_email(email, contents): recipients=[email]) msg.html = contents mail.send(msg) + + +def send_subscription_change(event_name, customer_id, customer_email, quay_username): + msg = Message('Quay.io Customer Subscription Change', + sender='support@quay.io', # Why do I need this? + recipients=['stripe@quay.io']) + msg.html = SUBSCRIPTION_CHANGE.format(event_name, customer_id, customer_email, quay_username) + mail.send(msg)