dynamicweb/webhook/views.py
PCoder 655316305b Create FailedInvoice when invoice.payment_failed webhook is fired
Also set the context parameters for sending emails
2019-06-13 09:55:36 +02:00

172 lines
No EOL
6.5 KiB
Python

import logging
import stripe
# Create your views here.
from django.conf import settings
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from hosting.models import (
MonthlyHostingBill, HostingBillLineItem, FailedInvoice
)
from membership.models import StripeCustomer
from utils.mailer import BaseEmail
from utils.tasks import send_plain_email_task
logger = logging.getLogger(__name__)
@require_POST
@csrf_exempt
def handle_invoice_webhook(request):
payload = request.body
event = None
try:
if 'HTTP_STRIPE_SIGNATURE' in request.META:
sig_header = request.META['HTTP_STRIPE_SIGNATURE']
else:
logger.error("No HTTP_STRIPE_SIGNATURE header")
# Invalid payload
return HttpResponse(status=400)
event = stripe.Webhook.construct_event(
payload, sig_header, settings.INVOICE_WEBHOOK_SECRET
)
except ValueError as e:
# Invalid payload
err_msg = "FAILURE handle_invoice_webhook: Invalid payload details"
err_body = "Details %s" % str(e)
return handle_error(err_msg, err_body)
except stripe.error.SignatureVerificationError as e:
# Invalid signature
err_msg = "FAILURE handle_invoice_webhook: SignatureVerificationError"
err_body = "Details %s" % str(e)
return handle_error(err_msg, err_body)
# Do something with event
logger.debug("Passed invoice signature verification")
# Get the user from the invoice
invoice = event.data.object
logger.debug("Checking whether StripeCustomer %s exists" % invoice.customer)
try:
stripe_customer = StripeCustomer.objects.get(stripe_id=invoice.customer)
except StripeCustomer.DoesNotExist as dne:
# StripeCustomer does not exist
err_msg = "FAILURE handle_invoice_webhook: StripeCustomer %s doesn't exist" % invoice.customer
err_body = "Details %s" % str(dne)
return handle_error(err_msg, err_body)
if event.type == "invoice.payment_succeeded":
logger.debug("Invoice payment succeeded")
# Create a new invoice for the user
invoice_dict = {
'created': invoice.created,
'receipt_number': invoice.receipt_number,
'invoice_number': invoice.number,
'paid_at': invoice.status_transitions.paid_at if invoice.paid else 0,
'period_start': invoice.period_start,
'period_end': invoice.period_end,
'billing_reason': invoice.billing_reason,
'discount': invoice.discount.coupon.amount_off if invoice.discount else 0,
'total': invoice.total,
# to see how many line items we have in this invoice and
# then later check if we have more than 1
'lines_data_count': len(
invoice.lines.data) if invoice.lines.data is not None else 0,
'invoice_id': invoice.id,
'lines_meta_data_csv': ','.join(
[line.metadata.VM_ID if hasattr(line.metadata, 'VM_ID') else ''
for line in invoice.lines.data]
),
'subscription_ids_csv': ','.join(
[line.id if line.type == 'subscription' else '' for line in
invoice.lines.data]
),
'line_items': invoice.lines.data,
'customer': stripe_customer
}
mhb = MonthlyHostingBill.create(invoice_dict)
mbli = HostingBillLineItem.objects.filter(monthly_hosting_bill=mhb).first()
vm_id = mbli.get_vm_id()
if vm_id is None:
vm_id = mhb.order.vm_id
# Send an email to admin
admin_msg_sub = "Invoice payment success for user {} and VM {}".format(
stripe_customer.user.email,
vm_id if vm_id is not None else "Unknown"
)
email_to_admin_data = {
'subject': admin_msg_sub,
'from_email': settings.DCL_SUPPORT_FROM_ADDRESS,
'to': settings.WEBHOOK_EMAIL_TO.split(","),
'body': "\n".join(
["%s=%s" % (k, v) for (k, v) in invoice_dict.items()]),
}
logger.debug("Sending msg %s to %s" % (admin_msg_sub,
settings.WEBHOOK_EMAIL_TO))
send_plain_email_task.delay(email_to_admin_data)
elif event.type == "invoice.payment_failed":
# Create a failed invoice, so that we have a trace of which invoices
# need a followup
FailedInvoice.create(
stripe_customer, number_of_attempts = 1, invoice_id=invoice.id
)
VM_ID = invoice.lines.data[0].metadata["VM_ID"]
admin_msg_sub = "Invoice payment FAILED for user {} and {}".format(
stripe_customer.user.email,
VM_ID
)
logger.error(admin_msg_sub)
# send email to admin
email_to_admin_data = {
'subject': admin_msg_sub,
'from_email': settings.DCL_SUPPORT_FROM_ADDRESS,
'to': settings.WEBHOOK_EMAIL_TO.split(","),
'body': "\n".join(
["%s=%s" % (k, v) for (k, v) in invoice.__dict__.items()]),
}
# send email to user
context = {
'base_url': "{0}://{1}".format(request.scheme,
request.get_host()),
'dcl_text': settings.DCL_TEXT,
'VM_ID': VM_ID,
'number_of_remaining_hours': 48 # for the first failure we wait 48 hours
}
email_data = {
'subject': 'IMPORTANT: The payment for VM {VM_ID} at {dcl_text} failed'.format(
dcl_text=settings.DCL_TEXT,
VM_ID=invoice.lines.data.metadata
),
'to': stripe_customer.user.email,
'context': context,
'template_name': 'invoice_failed',
'template_path': 'datacenterlight/emails/',
'from_address': settings.DCL_SUPPORT_FROM_ADDRESS
}
email = BaseEmail(**email_data)
email.send()
send_plain_email_task.delay(email_to_admin_data)
else:
logger.error("Unhandled event : " + event.type)
return HttpResponse(status=200)
def handle_error(error_msg, error_body):
logger.error("%s -- %s" % (error_msg, error_body))
email_to_admin_data = {
'subject': error_msg,
'from_email': settings.DCL_SUPPORT_FROM_ADDRESS,
'to': [settings.ADMIN_EMAIL],
'body': error_body,
}
send_plain_email_task.delay(email_to_admin_data)
return HttpResponse(status=400)