Prepare voucher usage

This commit is contained in:
Kumi 2021-05-22 08:38:30 +02:00
parent 44f149f7d9
commit ce8bc06699
5 changed files with 85 additions and 0 deletions

11
payment/const.py Normal file
View file

@ -0,0 +1,11 @@
# Payment Status Constants
PAYMENT_STATUS_INITIATED = -100
PAYMENT_STATUS_AUTHORIZED = -1
PAYMENT_STATUS_SUCCESS = 0
PAYMENT_STATUS_FAILED = 1
PAYMENT_STATUS_REFUNDED = 2
PAYMENT_STATUS_CANCELLED = 100

View file

View file

@ -0,0 +1,18 @@
from dbsettings.functions import getValue
from random import SystemRandom
import fast_luhn
def generate_voucher_code(prefix=getValue("payment.voucher.prefix", "9011"), length=getValue("payment.voucher.length", 16)):
if length <= len(str(prefix)):
raise ValueError("Voucher code length must be longer than its prefix!")
length_randpart = length - len(str(prefix)) - 1
base_code = prefix
for i in range(length_randpart):
base_code += str(SystemRandom().randint(0, 9))
return fast_luhn.complete(base_code)

55
payment/voucher/models.py Normal file
View file

@ -0,0 +1,55 @@
from django.db import models
from django.contrib.auth import get_user_model
from .helpers import generate_voucher_code
from payment.const import PAYMENT_STATUS_SUCCESS, PAYMENT_STATUS_FAILED, PAYMENT_STATUS_REFUNDED
from clients.models import ClientProfile
class Voucher(models.Model):
code = models.IntegerField(default=generate_voucher_code)
value = models.DecimalField(max_digits=10, decimal_places=2)
created_at = models.DateTimeField(auto_now_add=True)
@property
def is_active(self):
try:
self.voucheractivation
return True
except VoucherActivation.DoesNotExist:
return False
class VoucherActivation(models.Model):
voucher = models.OneToOneField(Voucher, models.PROTECT)
user = models.ForeignKey(get_user_model(), models.PROTECT)
activated_at = models.DateTimeField(auto_now_add=True)
data = models.CharField(max_length=256)
class VoucherApplication(models.Model):
voucher = models.OneToOneField(Voucher, models.PROTECT)
client = models.ForeignKey(get_user_model(), models.PROTECT)
applied_at = models.DateTimeField(auto_now_add=True)
class VoucherInvoicePayment(InvoicePayment):
success = models.BooleanField(default=True)
@property
def gateway(self):
return "Voucher"
@classmethod
def initiate(cls, invoice, amount=None):
amount = amount or (invoice.balance * -1)
payment = cls.objects.create(invoice=invoice, amount=amount, gateway_id=uuid.uuid4())
if invoice.user.balance <= 0:
payment.success = False
payment.save()
invoice.finalize()
return reverse_lazy("payment:status", args=[payment.uuid])
@property
def status(self):
return PAYMENT_STATUS_SUCCESS if self.success else PAYMENT_STATUS_FAILED

View file

@ -17,4 +17,5 @@ googlemaps
Babel
staticmap
django-mathfilters
fast-luhn
git+https://kumig.it/kumisystems/PyInvoice