Merge pull request 'fix: don't send pre-payment notification email' (#56) from fix-rm-not-invoice into master

Reviewed-on: https://gitea.gna.org/Hostea/dashboard/pulls/56
wip-gitea
Loïc Dachary 2022-09-12 15:48:14 -04:00
commit c7def47215
4 changed files with 42 additions and 35 deletions

View File

@ -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")

View File

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

View File

@ -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,)))

View File

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