payment in transaction models
This commit is contained in:
parent
95ed3a2d93
commit
4fcbbae4a8
4 changed files with 139 additions and 76 deletions
|
|
@ -20,7 +20,7 @@ class AccessToken(AbstractAccessToken):
|
||||||
class Application(AbstractApplication):
|
class Application(AbstractApplication):
|
||||||
id = None
|
id = None
|
||||||
uuid = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4, unique=True, db_index=True)
|
uuid = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4, unique=True, db_index=True)
|
||||||
balance = models.BigIntegerField(_('balance'), default=0)
|
balance = models.PositiveBigIntegerField(_('balance'), default=0)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = False
|
abstract = False
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ class User(AbstractUser):
|
||||||
null=True
|
null=True
|
||||||
)
|
)
|
||||||
last_checkout_request = models.DateTimeField(_('otp expire'), max_length=30, blank=True, null=True)
|
last_checkout_request = models.DateTimeField(_('otp expire'), max_length=30, blank=True, null=True)
|
||||||
balance = models.BigIntegerField(_('balance'), default=0)
|
balance = models.PositiveBigIntegerField(_('balance'), default=0)
|
||||||
|
|
||||||
iban = models.CharField(null=True, max_length=30, blank=True)
|
iban = models.CharField(null=True, max_length=30, blank=True)
|
||||||
iban_verified = models.BooleanField(null=True)
|
iban_verified = models.BooleanField(null=True)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
# Generated by Django 5.0.6 on 2024-08-10 14:04
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('wallet', '0002_rename_last_update_transaction_updated_at_and_more'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='transaction',
|
||||||
|
name='detail_id',
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='transaction',
|
||||||
|
name='detail',
|
||||||
|
field=models.JSONField(blank=True, default={}, null=True),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='transaction',
|
||||||
|
name='created_at',
|
||||||
|
field=models.DateTimeField(auto_now_add=True, null=True),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='transaction',
|
||||||
|
name='updated_at',
|
||||||
|
field=models.DateTimeField(auto_now=True, null=True),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -10,40 +10,39 @@ from django.conf import settings
|
||||||
from oauth2_provider import settings as oauth2_settings
|
from oauth2_provider import settings as oauth2_settings
|
||||||
from .constans import StateChoices, TypeChoices
|
from .constans import StateChoices, TypeChoices
|
||||||
from apps.users.models import User
|
from apps.users.models import User
|
||||||
|
from ..gooyal_oauth2.models import Application
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: move DRF functionalities to view or serializers
|
||||||
class TransactionManager(models.Manager):
|
class TransactionManager(models.Manager):
|
||||||
def create_transaction(
|
def create_transaction(
|
||||||
self,
|
self,
|
||||||
amount,
|
amount,
|
||||||
payee: User,
|
payee,
|
||||||
payer: User = None,
|
payee_type,
|
||||||
|
payer=None,
|
||||||
|
payer_type=None,
|
||||||
delay=0,
|
delay=0,
|
||||||
# detail_id=None,
|
detail=None,
|
||||||
application=None,
|
application=None,
|
||||||
# state=None,
|
|
||||||
uuid=None
|
uuid=None
|
||||||
):
|
):
|
||||||
if not (isinstance(payer, User) or payer is None):
|
if amount <= 0:
|
||||||
raise ValidationError('payer type not valid')
|
|
||||||
if not isinstance(payee, User):
|
|
||||||
raise ValidationError('payee type not valid')
|
|
||||||
if not amount > 0:
|
|
||||||
raise ValidationError('amount should be greater than zero')
|
raise ValidationError('amount should be greater than zero')
|
||||||
if not delay >= 0:
|
if delay < 0:
|
||||||
raise ValidationError('delay should be greater than equal zero')
|
raise ValidationError('delay should be greater than equal zero')
|
||||||
|
|
||||||
transaction = self.model(
|
transaction = self.model(
|
||||||
uuid=uuid,
|
uuid=uuid,
|
||||||
payer=payer,
|
payer=payer,
|
||||||
|
payer_type=payer_type,
|
||||||
payee=payee,
|
payee=payee,
|
||||||
|
payee_type=payee_type,
|
||||||
amount=amount,
|
amount=amount,
|
||||||
delay=delay,
|
delay=delay,
|
||||||
# detail_id=detail_id,
|
application=application,
|
||||||
application=application
|
detail=detail
|
||||||
)
|
)
|
||||||
# if state:
|
|
||||||
# transaction.state = state
|
|
||||||
|
|
||||||
transaction.save()
|
transaction.save()
|
||||||
return transaction
|
return transaction
|
||||||
|
|
@ -61,10 +60,8 @@ class Transaction(models.Model):
|
||||||
state = models.IntegerField(choices=StateChoices.choices, default=StateChoices.CREATED)
|
state = models.IntegerField(choices=StateChoices.choices, default=StateChoices.CREATED)
|
||||||
created_at = models.DateTimeField(auto_now_add=True, null=True)
|
created_at = models.DateTimeField(auto_now_add=True, null=True)
|
||||||
updated_at = models.DateTimeField(auto_now=True, null=True)
|
updated_at = models.DateTimeField(auto_now=True, null=True)
|
||||||
payer_application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL, on_delete=models.PROTECT,
|
application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL, on_delete=models.PROTECT,
|
||||||
related_name='payer_transactions', null=True)
|
related_name='transactions', null=True)
|
||||||
payee_application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL, on_delete=models.PROTECT,
|
|
||||||
related_name='payee_transactions', null=True)
|
|
||||||
detail = models.JSONField(null=True, blank=True, default={})
|
detail = models.JSONField(null=True, blank=True, default={})
|
||||||
|
|
||||||
objects = TransactionManager()
|
objects = TransactionManager()
|
||||||
|
|
@ -72,98 +69,132 @@ class Transaction(models.Model):
|
||||||
# class Meta:
|
# class Meta:
|
||||||
# unique_together = ('application', 'detail_id',)
|
# unique_together = ('application', 'detail_id',)
|
||||||
|
|
||||||
def pay(self, payer=None):
|
def withdraw_from_payer_balance(self):
|
||||||
if not self.payer and not payer:
|
payer = self.get_payer()
|
||||||
|
payer.balance = F('balance') - self.amount
|
||||||
|
payer.save(update_fields=['balance'])
|
||||||
|
|
||||||
|
def deposit_to_payee_balance(self):
|
||||||
|
payee = self.get_payee()
|
||||||
|
payee.balance = F('balance') + self.amount
|
||||||
|
payee.save(update_fields=['balance'])
|
||||||
|
|
||||||
|
def get_pay_entity(self, entity_id, entity_type):
|
||||||
|
if entity_type == TypeChoices.APPLICATION:
|
||||||
|
entity: Application = Application.objects.get(pk=entity_id)
|
||||||
|
else:
|
||||||
|
entity: User = User.objects.get(pk=entity_id)
|
||||||
|
|
||||||
|
return entity
|
||||||
|
|
||||||
|
def get_payer(self):
|
||||||
|
return self.get_pay_entity(self.payer_id, self.payer_type)
|
||||||
|
|
||||||
|
def get_payee(self):
|
||||||
|
return self.get_pay_entity(self.payee_id, self.payee_type)
|
||||||
|
|
||||||
|
def change_state(self, from_state, to_state, error_message=None, **data):
|
||||||
|
if self.state == from_state:
|
||||||
|
updated = Transaction.objects.filter(pk=self.pk, state=from_state).update(
|
||||||
|
state=to_state,
|
||||||
|
**data
|
||||||
|
)
|
||||||
|
if updated: # double check in db level
|
||||||
|
self.state = to_state
|
||||||
|
else:
|
||||||
|
raise APIException(error_message)
|
||||||
|
else:
|
||||||
|
raise APIException(error_message)
|
||||||
|
|
||||||
|
def pay(self, payer_id=None, payer_type=None):
|
||||||
|
if not self.payer_id and not (payer_id and payer_type):
|
||||||
raise APIException('payer identification error')
|
raise APIException('payer identification error')
|
||||||
|
|
||||||
if self.payer and payer and not self.payer == payer:
|
if self.payer_id and payer_id and not (self.payer_id == payer_id and self.payer_type == payer_type):
|
||||||
raise APIException('payer identification error')
|
raise APIException('payer identification error')
|
||||||
|
|
||||||
# TODO: check bundry conditions
|
|
||||||
if not self.state == StateChoices.CREATED:
|
if not self.state == StateChoices.CREATED:
|
||||||
raise APIException('this transaction is not ready to pay')
|
raise APIException('this transaction is not ready to pay')
|
||||||
|
|
||||||
if not self.payer:
|
if not self.payer_id:
|
||||||
self.payer = payer
|
self.payer_id = payer_id
|
||||||
|
self.payer_type = payer_type
|
||||||
|
|
||||||
|
payer = self.get_payer()
|
||||||
|
|
||||||
|
if payer.balance < self.amount:
|
||||||
|
raise APIException('not enough balance')
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
self.updated_at = datetime.datetime.now()
|
|
||||||
|
|
||||||
if self.payer.balance < self.amount:
|
|
||||||
raise APIException('not enough balance')
|
|
||||||
|
|
||||||
self.payer.balance = F('balance') - self.amount
|
|
||||||
self.payer.save(update_fields=['balance'])
|
|
||||||
|
|
||||||
self.payer.refresh_from_db()
|
|
||||||
if self.payer.balance < 0:
|
|
||||||
raise APIException('not enough balance')
|
|
||||||
|
|
||||||
if self.delay:
|
if self.delay:
|
||||||
self.state = StateChoices.DELAYED
|
self.change_state(
|
||||||
self.save()
|
StateChoices.CREATED,
|
||||||
|
StateChoices.DELAYED,
|
||||||
|
error_message='this transaction is not ready to pay',
|
||||||
|
payer_id=payer_id,
|
||||||
|
payer_type=payer_type
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.payee.balance = F('balance') + self.amount
|
self.change_state(
|
||||||
self.payee.save(update_fields=['balance'])
|
StateChoices.CREATED,
|
||||||
|
StateChoices.SUCCESS,
|
||||||
|
error_message='this transaction is not ready to pay',
|
||||||
|
payer_id=payer_id,
|
||||||
|
payer_type=payer_type
|
||||||
|
)
|
||||||
|
self.deposit_to_payee_balance()
|
||||||
|
|
||||||
self.state = StateChoices.SUCCESS
|
self.withdraw_from_payer_balance()
|
||||||
self.save()
|
|
||||||
|
|
||||||
self.save()
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def receipt(self):
|
def receipt(self):
|
||||||
# TODO: check bundry conditions
|
|
||||||
if not self.state == StateChoices.DELAYED:
|
if not self.state == StateChoices.DELAYED:
|
||||||
raise APIException('this transaction is not delayed')
|
raise APIException('this transaction is not delayed')
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
self.updated_at = datetime.datetime.now()
|
self.change_state(
|
||||||
self.payee.balance = F('balance') + self.amount
|
StateChoices.DELAYED,
|
||||||
self.state = StateChoices.SUCCESS
|
StateChoices.SUCCESS,
|
||||||
self.payee.save()
|
error_message='this transaction is not delayed'
|
||||||
self.save()
|
)
|
||||||
|
self.deposit_to_payee_balance()
|
||||||
|
return self
|
||||||
|
|
||||||
def submit(self):
|
def submit(self):
|
||||||
if not self.payer:
|
payer = self.get_payer()
|
||||||
|
payee = self.get_payee()
|
||||||
|
|
||||||
|
if not payer:
|
||||||
raise APIException('payer identification error')
|
raise APIException('payer identification error')
|
||||||
|
|
||||||
if not self.payee:
|
if not payee:
|
||||||
raise APIException('payee identification error')
|
raise APIException('payee identification error')
|
||||||
|
|
||||||
# TODO: check bundry conditions
|
|
||||||
if not self.state == StateChoices.CREATED:
|
if not self.state == StateChoices.CREATED:
|
||||||
raise APIException('this transaction is not ready to pay')
|
raise APIException('this transaction is not ready to pay')
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
self.updated_at = datetime.datetime.now()
|
self.withdraw_from_payer_balance()
|
||||||
|
self.change_state(
|
||||||
if self.payer.balance < self.amount:
|
StateChoices.CREATED,
|
||||||
raise APIException('not enough balance')
|
StateChoices.PENDING,
|
||||||
|
error_message='this transaction is not ready to pay'
|
||||||
self.payer.balance = F('balance') - self.amount
|
)
|
||||||
self.payer.save(update_fields=['balance'])
|
|
||||||
|
|
||||||
self.payer.refresh_from_db()
|
|
||||||
self.state = StateChoices.PENDING
|
|
||||||
if self.payer.balance < 0:
|
|
||||||
raise APIException('not enough balance')
|
|
||||||
|
|
||||||
self.save()
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def verify(self):
|
def verify(self):
|
||||||
# TODO: check bundry conditions
|
|
||||||
if not self.state == StateChoices.PENDING:
|
if not self.state == StateChoices.PENDING:
|
||||||
raise APIException('this transaction is not pending')
|
raise APIException('this transaction is not pending')
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
self.updated_at = datetime.datetime.now()
|
self.deposit_to_payee_balance()
|
||||||
self.payee.balance = F('balance') + self.amount
|
self.change_state(
|
||||||
self.state = StateChoices.SUCCESS
|
StateChoices.PENDING,
|
||||||
self.payee.save()
|
StateChoices.SUCCESS,
|
||||||
self.save()
|
error_message='this transaction is not pending'
|
||||||
|
)
|
||||||
|
return self
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.payer} -> {self.payee} ({self.uuid}): {self.amount}"
|
return f"{self.get_payer()} -> {self.get_payee()} ({self.uuid}): {self.amount}"
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue