From 669a22a004b00db88e3f66ea88a12a03f733e1ac Mon Sep 17 00:00:00 2001 From: Hostea dashboard Date: Sun, 4 Sep 2022 18:19:45 +0530 Subject: [PATCH 1/2] fix: don't send pre-payment notification email closes: https://gitea.gna.org/Hostea/dashboard/issues/49 --- billing/tests.py | 11 ----------- billing/utils.py | 23 ----------------------- 2 files changed, 34 deletions(-) diff --git a/billing/tests.py b/billing/tests.py index 14d3cda..365fbc5 100644 --- a/billing/tests.py +++ b/billing/tests.py @@ -122,17 +122,6 @@ class BillingTest(TestCase): True, ) - invoice_generated_mail = mail.outbox.pop() - self.assertEqual( - all( - [ - invoice_generated_mail.to[0] == self.email, - "An invoice is generated" in invoice_generated_mail.body, - ] - ), - True, - ) - ## payment failure page; no real functionality but user is redirected here # by stripe if payment is successful resp = c.get(reverse("billing.invoice.fail", args=(payment.public_ref,))) diff --git a/billing/utils.py b/billing/utils.py index 349fd4c..8e050e2 100644 --- a/billing/utils.py +++ b/billing/utils.py @@ -98,27 +98,4 @@ def generate_invoice(instance: Instance) -> Payment: instance=instance, ) - invoice_link = get_invoice_link(payment=payment) - - ctx = { - "username": instance.owned_by.username, - "link": invoice_link, - "payment": payment, - } - - body = render_to_string( - "billing/emails/payment-notification.txt", - context=ctx, - ) - - email = instance.owned_by.email - sender = settings.DEFAULT_FROM_EMAIL - - send_mail( - subject="[Hostea] An invoice is generated for your Hostea VM", - message=body, - from_email=f"No reply Hostea<{sender}>", # TODO read from settings.py - recipient_list=[email], - ) - return payment From 9f5032bd28e3179a9ef8b8a1a1caa1b0ef92cf46 Mon Sep 17 00:00:00 2001 From: Hostea dashboard Date: Mon, 12 Sep 2022 16:53:43 +0530 Subject: [PATCH 2/2] 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