2022-06-25 10:57:43 +00:00
|
|
|
# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2022-07-07 19:21:11 +00:00
|
|
|
from enum import Enum, unique
|
2022-07-08 13:21:04 +00:00
|
|
|
from urllib.parse import urlparse, urlunparse
|
2022-06-25 10:57:43 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2022-07-07 19:21:11 +00:00
|
|
|
|
2022-06-25 10:57:43 +00:00
|
|
|
from payments import get_payment_model, RedirectNeeded, PaymentStatus
|
2022-07-07 19:21:11 +00:00
|
|
|
from django.core.mail import send_mail
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.template.loader import render_to_string
|
2022-06-25 10:57:43 +00:00
|
|
|
from django.contrib.auth.models import User
|
2022-07-07 19:21:11 +00:00
|
|
|
from django.conf import settings
|
2022-06-25 10:57:43 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
|
|
|
from dash.models import Instance
|
|
|
|
|
2022-07-07 19:21:11 +00:00
|
|
|
Payment = get_payment_model()
|
2022-06-25 10:57:43 +00:00
|
|
|
|
2022-07-07 19:21:11 +00:00
|
|
|
|
|
|
|
def __get_delta():
|
2022-06-25 10:57:43 +00:00
|
|
|
now = datetime.now(tz=timezone.utc)
|
|
|
|
delta = now - timedelta(seconds=(60 * 60 * 24 * 30)) # one month
|
2022-07-07 19:21:11 +00:00
|
|
|
return delta
|
|
|
|
|
2022-07-08 14:01:06 +00:00
|
|
|
|
2022-07-08 13:21:04 +00:00
|
|
|
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, "", "", ""))
|
2022-07-07 19:21:11 +00:00
|
|
|
|
2022-07-08 14:01:06 +00:00
|
|
|
|
2022-07-07 19:21:11 +00:00
|
|
|
def payment_fullfilled(instance: Instance) -> bool:
|
|
|
|
delta = __get_delta()
|
2022-06-25 10:57:43 +00:00
|
|
|
|
|
|
|
payment = None
|
2022-07-08 15:13:22 +00:00
|
|
|
for p in Payment.objects.filter(
|
|
|
|
date__gt=(delta), instance_name=instance.name, vm_deleted=False
|
|
|
|
):
|
2022-06-25 10:57:43 +00:00
|
|
|
if p.status == PaymentStatus.CONFIRMED:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2022-07-07 19:21:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
@unique
|
|
|
|
class GenerateInvoiceErrorCode(Enum):
|
|
|
|
ALREADY_PAID = "already paid"
|
|
|
|
DUPLICATE_PAYMENT = "DUPLICATE PAYMENT"
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class GenerateInvoiceException(Exception):
|
|
|
|
error: str
|
|
|
|
code: GenerateInvoiceErrorCode
|
|
|
|
|
|
|
|
def __init__(self, code: GenerateInvoiceErrorCode):
|
|
|
|
self.error = str(code)
|
|
|
|
self.code = code
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.error
|
|
|
|
|
|
|
|
|
|
|
|
def generate_invoice(instance: Instance) -> Payment:
|
|
|
|
delta = __get_delta()
|
|
|
|
|
|
|
|
payment = None
|
2022-07-08 15:13:22 +00:00
|
|
|
for p in Payment.objects.filter(
|
|
|
|
date__gt=(delta), instance_name=instance.name, vm_deleted=False
|
|
|
|
):
|
2022-07-07 19:21:11 +00:00
|
|
|
if p.status == PaymentStatus.CONFIRMED:
|
|
|
|
raise GenerateInvoiceException(code=GenerateInvoiceErrorCode.ALREADY_PAID)
|
|
|
|
if any([p.status == PaymentStatus.INPUT, p.status == PaymentStatus.WAITING]):
|
|
|
|
if payment is None:
|
|
|
|
payment = p
|
|
|
|
else:
|
|
|
|
print(f"Duplicate payment {p}, deleting in favor of {payment}")
|
|
|
|
p.delete()
|
|
|
|
|
|
|
|
if payment is None:
|
|
|
|
print("Payment not found, generating new payment")
|
|
|
|
payment = Payment.objects.create(
|
|
|
|
variant="stripe", # this is the variant from PAYMENT_VARIANTS
|
|
|
|
instance=instance,
|
|
|
|
)
|
|
|
|
|
2022-07-08 13:21:04 +00:00
|
|
|
invoice_link = get_invoice_link(payment=payment)
|
|
|
|
|
2022-07-07 19:21:11 +00:00
|
|
|
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
|