2022-06-21 19:07:03 +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/>.
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
|
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
|
|
from django.template.response import TemplateResponse
|
2022-07-07 15:21:33 +00:00
|
|
|
from django.core.mail import send_mail
|
2022-06-21 19:07:03 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.http import HttpResponse
|
2022-07-07 15:21:33 +00:00
|
|
|
from django.template.loader import render_to_string
|
2022-06-21 19:07:03 +00:00
|
|
|
from django.urls import reverse
|
2022-07-07 15:21:33 +00:00
|
|
|
from django.db.models import Q
|
|
|
|
from django.conf import settings
|
2022-06-21 19:07:03 +00:00
|
|
|
|
|
|
|
from payments import get_payment_model, RedirectNeeded, PaymentStatus
|
|
|
|
from dash.models import Instance
|
2022-06-28 18:52:49 +00:00
|
|
|
from infrastructure.utils import create_vm_if_not_exists
|
2022-06-30 09:07:49 +00:00
|
|
|
from dash.utils import footer_ctx
|
2022-06-21 19:07:03 +00:00
|
|
|
|
2022-07-07 19:21:11 +00:00
|
|
|
from .utils import (
|
|
|
|
generate_invoice as generate_invoice_util,
|
|
|
|
GenerateInvoiceErrorCode,
|
|
|
|
GenerateInvoiceException,
|
2022-07-08 14:01:06 +00:00
|
|
|
get_invoice_link,
|
2022-07-07 19:21:11 +00:00
|
|
|
)
|
|
|
|
|
2022-06-21 19:07:03 +00:00
|
|
|
|
|
|
|
def default_ctx(title: str, username: str):
|
|
|
|
"""
|
|
|
|
Default context for all dashboard
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
"title": title,
|
|
|
|
"username": username,
|
2022-06-30 09:07:49 +00:00
|
|
|
"footer": footer_ctx(),
|
2022-06-21 19:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def pending_invoices(request):
|
|
|
|
PAGE_TITLE = "Pending Invoices"
|
|
|
|
|
|
|
|
user = request.user
|
|
|
|
Payment = get_payment_model()
|
|
|
|
payments = Payment.objects.filter(~Q(status=PaymentStatus.CONFIRMED), paid_by=user)
|
|
|
|
ctx = default_ctx(title=PAGE_TITLE, username=user.username)
|
|
|
|
ctx["payments"] = payments
|
|
|
|
return render(request, "billing/invoices/pending/index.html", context=ctx)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def paid_invoices(request):
|
|
|
|
PAGE_TITLE = "Payment Receipts"
|
|
|
|
|
|
|
|
user = request.user
|
|
|
|
Payment = get_payment_model()
|
|
|
|
payments = Payment.objects.filter(status=PaymentStatus.CONFIRMED, paid_by=user)
|
|
|
|
ctx = default_ctx(title=PAGE_TITLE, username=user.username)
|
|
|
|
ctx["payments"] = payments
|
|
|
|
return render(request, "billing/invoices/paid/index.html", context=ctx)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def generate_invoice(request, instance_name: str):
|
|
|
|
instance = get_object_or_404(Instance, name=instance_name, owned_by=request.user)
|
|
|
|
|
2022-07-07 19:21:11 +00:00
|
|
|
try:
|
|
|
|
payment = generate_invoice_util(instance=instance)
|
|
|
|
return redirect(reverse("billing.invoice.details", args=(payment.public_ref,)))
|
|
|
|
except GenerateInvoiceException as e:
|
|
|
|
if e.code == GenerateInvoiceErrorCode.ALREADY_PAID:
|
2022-06-21 19:07:03 +00:00
|
|
|
return HttpResponse("BAD REQUEST: Already paid", status=400)
|
2022-07-07 19:21:11 +00:00
|
|
|
return redirect(reverse("dash.home"))
|
2022-06-21 19:07:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def payment_details(request, payment_public_id):
|
|
|
|
|
|
|
|
payment = get_object_or_404(
|
|
|
|
get_payment_model(), public_ref=payment_public_id, paid_by=request.user
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
form = payment.get_form(data=request.POST or None)
|
|
|
|
except RedirectNeeded as redirect_to:
|
|
|
|
return redirect(str(redirect_to))
|
|
|
|
|
|
|
|
PAGE_TITLE = f'Invoice for VM "{payment.instance_name}"'
|
|
|
|
|
|
|
|
ctx = default_ctx(title=PAGE_TITLE, username=request.user.username)
|
|
|
|
ctx["form"] = form
|
|
|
|
ctx["payment"] = payment
|
|
|
|
return render(request, "billing/index.html", ctx)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def payment_success(request, payment_public_id):
|
|
|
|
PAGE_TITLE = "Payment Success"
|
|
|
|
ctx = default_ctx(title=PAGE_TITLE, username=request.user.username)
|
|
|
|
payment = get_object_or_404(
|
|
|
|
get_payment_model(), public_ref=payment_public_id, paid_by=request.user
|
|
|
|
)
|
2022-07-08 13:21:04 +00:00
|
|
|
|
2022-07-07 15:21:33 +00:00
|
|
|
ctx = {
|
|
|
|
"username": request.user.username,
|
|
|
|
"payment": payment,
|
2022-07-08 13:21:04 +00:00
|
|
|
"link": get_invoice_link(payment=payment),
|
2022-07-07 15:21:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
body = render_to_string(
|
|
|
|
"billing/emails/payment-receipt.txt",
|
|
|
|
context=ctx,
|
|
|
|
)
|
|
|
|
|
|
|
|
email = request.user.email
|
|
|
|
sender = settings.DEFAULT_FROM_EMAIL
|
|
|
|
|
|
|
|
send_mail(
|
2022-09-13 14:27:26 +00:00
|
|
|
subject="[Gna!] Payment receipt your Gna! VM",
|
2022-07-07 15:21:33 +00:00
|
|
|
message=body,
|
2022-09-12 11:06:29 +00:00
|
|
|
from_email=f"No reply Gna!<{sender}>", # TODO read from settings.py
|
2022-07-07 15:21:33 +00:00
|
|
|
recipient_list=[email],
|
|
|
|
)
|
|
|
|
|
2022-06-28 18:52:49 +00:00
|
|
|
return redirect(reverse("infra.create", args=(payment.instance_name,)))
|
2022-06-21 19:07:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def payment_failure(request, payment_public_id):
|
|
|
|
PAGE_TITLE = "Payment failure"
|
|
|
|
payment = get_object_or_404(
|
|
|
|
get_payment_model(), public_ref=payment_public_id, paid_by=request.user
|
|
|
|
)
|
|
|
|
ctx = default_ctx(title=PAGE_TITLE, username=request.user.username)
|
|
|
|
return HttpResponse(f"{payment} failed")
|