38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
import logging
|
|
import stripe
|
|
|
|
from flask import request, make_response, Blueprint
|
|
|
|
from data import model
|
|
from app import app
|
|
from util.invoice import renderInvoiceToHtml
|
|
from util.email import send_invoice_email
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
webhooks = Blueprint('webhooks', __name__)
|
|
|
|
@webhooks.route('/stripe', methods=['POST'])
|
|
def stripe_webhook():
|
|
request_data = request.get_json()
|
|
logger.debug('Stripe webhook call: %s' % request_data)
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
return make_response('Okay')
|