feat: payment status checking util

Aravinth Manivannan 2022-06-25 16:27:43 +05:30
parent e688528fa3
commit ec49caa973
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
2 changed files with 43 additions and 0 deletions

View File

@ -27,8 +27,10 @@ from payments import get_payment_model, RedirectNeeded, PaymentStatus
from accounts.tests import register_util, login_util
from dash.tests import create_configurations, create_instance_util
from dash.models import Instance
from .models import Payment
from .utils import payment_fullfilled
class BillingTest(TestCase):
@ -49,6 +51,8 @@ class BillingTest(TestCase):
t=self, c=c, instance_name=instance_name, config=self.instance_config[0]
instance = Instance.objects.get(name=instance_name)
payment_uri = reverse("billing.invoice.generate", args=(instance_name,))
# generate invoice
@ -78,11 +82,17 @@ class BillingTest(TestCase):
resp = c.get(reverse("billing.invoice.pending"))
self.assertEqual(str.encode(invoice_uri) in resp.content, True)
self.assertEqual(payment_fullfilled(instance=instance), False)
# simulate payment. There's probably a better way to do this
payment = get_payment_model().objects.get(paid_by=self.user)
payment.status = PaymentStatus.CONFIRMED
self.assertEqual(payment_fullfilled(instance=instance), True)
# check if paid invoice is listed in paid invoice list view
resp = c.get(reverse("billing.invoice.paid"))
self.assertEqual(str.encode(invoice_uri) in resp.content, True)

billing/utils.py Normal file
View File

@ -0,0 +1,33 @@
# 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
# 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 payments import get_payment_model, RedirectNeeded, PaymentStatus
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from dash.models import Instance
def payment_fullfilled(instance: Instance) -> bool:
Payment = get_payment_model()
now = datetime.now(tz=timezone.utc)
delta = now - timedelta(seconds=(60 * 60 * 24 * 30)) # one month
payment = None
for p in Payment.objects.filter(date__gt=(delta), instance_name=instance.name):
if p.status == PaymentStatus.CONFIRMED:
return True
return False