From 2c8a5909cb76f66671b95e0cb6b9fbd2e09ac3e9 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 8 Jul 2022 18:51:04 +0530 Subject: [PATCH] fix: generate absolute URI when attaching links in invoice and payment notification fixes: https://gitea.hostea.org/Hostea/dashboard/issues/37 --- billing/templates/billing/emails/payment-receipt.txt | 2 +- billing/utils.py | 9 ++++++++- billing/views.py | 3 +++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/billing/templates/billing/emails/payment-receipt.txt b/billing/templates/billing/emails/payment-receipt.txt index 5236f9e..f2477c0 100644 --- a/billing/templates/billing/emails/payment-receipt.txt +++ b/billing/templates/billing/emails/payment-receipt.txt @@ -14,7 +14,7 @@ Hostea Receipt - {{payment.date.month}}/{{payment.date.day}}/{{payment.date.year To view the receipt online, please see the following link: -{% url 'billing.invoice.details' payment_public_id=payment.public_ref %} +{{link}} We appreciate your business! diff --git a/billing/utils.py b/billing/utils.py index 57dd0fd..8598c45 100644 --- a/billing/utils.py +++ b/billing/utils.py @@ -13,6 +13,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . from enum import Enum, unique +from urllib.parse import urlparse, urlunparse from datetime import datetime, timedelta, timezone from payments import get_payment_model, RedirectNeeded, PaymentStatus @@ -33,6 +34,10 @@ def __get_delta(): delta = now - timedelta(seconds=(60 * 60 * 24 * 30)) # one month return delta +def get_invoice_link(payment: Payment): + invoice_link = reverse("billing.invoice.details", args=(payment.public_ref,)) + parsed = urlparse(settings.PAYMENT_HOST) + return urlunparse((parsed.scheme, parsed.netloc, invoice_link, "", "", "")) def payment_fullfilled(instance: Instance) -> bool: delta = __get_delta() @@ -87,7 +92,8 @@ def generate_invoice(instance: Instance) -> Payment: instance=instance, ) - invoice_link = reverse("billing.invoice.details", args=(payment.public_ref,)) + invoice_link = get_invoice_link(payment=payment) + ctx = { "username": instance.owned_by.username, "link": invoice_link, @@ -102,6 +108,7 @@ def generate_invoice(instance: Instance) -> Payment: email = instance.owned_by.email sender = settings.DEFAULT_FROM_EMAIL + send_mail( subject="[Hostea] An invoice is generated for your Hostea VM", message=body, diff --git a/billing/views.py b/billing/views.py index 00ac7f3..a282eb6 100644 --- a/billing/views.py +++ b/billing/views.py @@ -33,6 +33,7 @@ from .utils import ( generate_invoice as generate_invoice_util, GenerateInvoiceErrorCode, GenerateInvoiceException, + get_invoice_link ) @@ -111,9 +112,11 @@ def payment_success(request, payment_public_id): payment = get_object_or_404( get_payment_model(), public_ref=payment_public_id, paid_by=request.user ) + ctx = { "username": request.user.username, "payment": payment, + "link": get_invoice_link(payment=payment), } body = render_to_string(