Create FailedInvoice when invoice.payment_failed webhook is fired

Also set the context parameters for sending emails
This commit is contained in:
PCoder 2019-06-13 09:55:36 +02:00
parent e981bf1542
commit 655316305b

View file

@ -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(