From 9f5032bd28e3179a9ef8b8a1a1caa1b0ef92cf46 Mon Sep 17 00:00:00 2001 From: Hostea dashboard Date: Mon, 12 Sep 2022 16:53:43 +0530 Subject: [PATCH] feat: send subscription renewal notification emails --- .../management/commands/generate_invoice.py | 26 ++++++++++++++++++- .../billing/emails/renew-subscription.txt | 17 ++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 billing/templates/billing/emails/renew-subscription.txt diff --git a/billing/management/commands/generate_invoice.py b/billing/management/commands/generate_invoice.py index a8b1ca8..7b81135 100644 --- a/billing/management/commands/generate_invoice.py +++ b/billing/management/commands/generate_invoice.py @@ -15,6 +15,8 @@ from django.core.management.base import BaseCommand from django.core.exceptions import ValidationError from django.conf import settings +from django.core.mail import send_mail +from django.template.loader import render_to_string from django.contrib.auth import get_user_model from oauth2_provider.models import get_application_model @@ -23,7 +25,7 @@ from oauth2_provider.generators import generate_client_id, generate_client_secre from accounts.utils import gen_secret from dash.models import Instance from infrastructure.models import InstanceCreated -from billing.utils import generate_invoice, payment_fullfilled +from billing.utils import generate_invoice, payment_fullfilled, get_invoice_link Application = get_application_model() @@ -41,5 +43,27 @@ class Command(BaseCommand): f"Payment not fulfilled for instance: {paid_instance.instance}" ) payment = generate_invoice(instance=paid_instance.instance) + owner = paid_instance.instance.owned_by + ctx = { + "username": owner.username, + "payment": payment, + "link": get_invoice_link(payment=payment), + } + + body = render_to_string( + "billing/emails/renew-subscription.txt", + context=ctx, + ) + + email = owner.email + sender = settings.DEFAULT_FROM_EMAIL + + send_mail( + subject="[Gna!] Payment receipt your Hostea VM", + message=body, + from_email=f"No reply Gna!<{sender}>", # TODO read from settings.py + recipient_list=[email], + ) + else: self.stdout.write("No instances available") diff --git a/billing/templates/billing/emails/renew-subscription.txt b/billing/templates/billing/emails/renew-subscription.txt new file mode 100644 index 0000000..b6296ba --- /dev/null +++ b/billing/templates/billing/emails/renew-subscription.txt @@ -0,0 +1,17 @@ +Hello {{ username }}! + +Your Gna! VM subscription is due for renewal. Please click the link +below to renew your subscription: + +{{link}} + +----------------------------------------------------- + +- Instance Name: {{ payment.instance_name }} +- Configuration: {{payment.instance_configuration_id.name}} +- Total Amount: {{payment.total}} {{payment.currency|upper}} + +We appreciate your business! + +Cheers, +Gna! team