From 655316305bda064307a46436865ace0f1fcefb55 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 13 Jun 2019 09:55:36 +0200 Subject: [PATCH] Create FailedInvoice when invoice.payment_failed webhook is fired Also set the context parameters for sending emails --- webhook/views.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/webhook/views.py b/webhook/views.py index 7eed6141..d20b266c 100644 --- a/webhook/views.py +++ b/webhook/views.py @@ -7,7 +7,9 @@ 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 +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 @@ -110,9 +112,15 @@ def handle_invoice_webhook(request): 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, - invoice.lines.data.metadata + VM_ID ) logger.error(admin_msg_sub) # send email to admin @@ -128,6 +136,8 @@ def handle_invoice_webhook(request): '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(