wallet with category
This commit is contained in:
parent
9de126d585
commit
a491adbd81
2 changed files with 4 additions and 172 deletions
|
|
@ -1,14 +1,9 @@
|
||||||
import datetime
|
|
||||||
import uuid
|
|
||||||
|
|
||||||
from django.contrib.auth import get_user_model
|
|
||||||
from django.core.validators import MinValueValidator
|
from django.core.validators import MinValueValidator
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.models import F
|
from django.db.models import F
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from oauth2_provider.models import get_application_model
|
from rest_framework.exceptions import APIException, NotAcceptable
|
||||||
from rest_framework.exceptions import APIException, ValidationError
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from oauth2_provider import settings as oauth2_settings
|
from oauth2_provider import settings as oauth2_settings
|
||||||
|
|
||||||
|
|
@ -79,9 +74,10 @@ class TransactionQuerySet(models.QuerySet):
|
||||||
payer = transaction.get_payer()
|
payer = transaction.get_payer()
|
||||||
payee = transaction.get_payee()
|
payee = transaction.get_payee()
|
||||||
payer_wallet, created = Wallet.objects.get_or_create(owner_uuid=transaction.payer_id, owner_type=transaction.payer_type, defaults={'balance': payer.balance})
|
payer_wallet, created = Wallet.objects.get_or_create(owner_uuid=transaction.payer_id, owner_type=transaction.payer_type, defaults={'balance': payer.balance})
|
||||||
payee_wallet, created = Wallet.objects.get_or_create(owner_uuid=transaction.payee_id, owner_type=transaction.payee_type, defaults={'balance': payer.balance})
|
payee_wallet, created = Wallet.objects.get_or_create(owner_uuid=transaction.payee_id, owner_type=transaction.payee_type, defaults={'balance': payee.balance})
|
||||||
transaction.payer_wallet = payer_wallet
|
transaction.payer_wallet = payer_wallet
|
||||||
transaction.payee_wallet = payee_wallet
|
transaction.payee_wallet = payee_wallet
|
||||||
|
transaction.save()
|
||||||
|
|
||||||
|
|
||||||
class Transaction(BaseModel):
|
class Transaction(BaseModel):
|
||||||
|
|
@ -118,20 +114,6 @@ class Transaction(BaseModel):
|
||||||
|
|
||||||
# unique_together = ('application', 'detail_id',)
|
# unique_together = ('application', 'detail_id',)
|
||||||
|
|
||||||
def withdraw_from_payer_balance(self):
|
|
||||||
payer = self.get_payer()
|
|
||||||
payer_is_allowed_negative_balance = self.payer_type == TypeChoices.APPLICATION.value and str(
|
|
||||||
self.payer_id) in settings.ALLOWED_NEGATIVE_BALANCE_APPLICATIONS
|
|
||||||
if self.amount > payer.balance and not payer_is_allowed_negative_balance:
|
|
||||||
raise APIException('not enough balance')
|
|
||||||
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):
|
def get_pay_entity(self, entity_id, entity_type):
|
||||||
if entity_type == TypeChoices.APPLICATION:
|
if entity_type == TypeChoices.APPLICATION:
|
||||||
entity: Application = Application.objects.get(pk=entity_id)
|
entity: Application = Application.objects.get(pk=entity_id)
|
||||||
|
|
@ -159,157 +141,6 @@ class Transaction(BaseModel):
|
||||||
else:
|
else:
|
||||||
raise APIException(error_message)
|
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')
|
|
||||||
|
|
||||||
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')
|
|
||||||
|
|
||||||
if not self.state == StateChoices.CREATED:
|
|
||||||
raise APIException('this transaction is not ready to pay')
|
|
||||||
|
|
||||||
if not self.payer_id:
|
|
||||||
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():
|
|
||||||
if self.delay:
|
|
||||||
self.change_state(
|
|
||||||
StateChoices.CREATED,
|
|
||||||
StateChoices.DELAYED,
|
|
||||||
error_message='this transaction is not ready to pay',
|
|
||||||
payer_id=payer_id,
|
|
||||||
payer_type=payer_type
|
|
||||||
)
|
|
||||||
|
|
||||||
else:
|
|
||||||
self.change_state(
|
|
||||||
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.withdraw_from_payer_balance()
|
|
||||||
return self
|
|
||||||
|
|
||||||
def receipt(self):
|
|
||||||
if not self.state == StateChoices.DELAYED:
|
|
||||||
raise APIException('this transaction is not delayed')
|
|
||||||
|
|
||||||
with transaction.atomic():
|
|
||||||
self.change_state(
|
|
||||||
StateChoices.DELAYED,
|
|
||||||
StateChoices.SUCCESS,
|
|
||||||
error_message='this transaction is not delayed'
|
|
||||||
)
|
|
||||||
self.deposit_to_payee_balance()
|
|
||||||
return self
|
|
||||||
|
|
||||||
def submit(self):
|
|
||||||
payer = self.get_payer()
|
|
||||||
payee = self.get_payee()
|
|
||||||
|
|
||||||
if not payer:
|
|
||||||
raise APIException('payer identification error')
|
|
||||||
|
|
||||||
if not payee:
|
|
||||||
raise APIException('payee identification error')
|
|
||||||
|
|
||||||
if not self.state == StateChoices.CREATED:
|
|
||||||
raise APIException('this transaction is not ready to pay')
|
|
||||||
|
|
||||||
with transaction.atomic():
|
|
||||||
self.withdraw_from_payer_balance()
|
|
||||||
self.change_state(
|
|
||||||
StateChoices.CREATED,
|
|
||||||
StateChoices.PENDING,
|
|
||||||
error_message='this transaction is not ready to pay'
|
|
||||||
)
|
|
||||||
return self
|
|
||||||
|
|
||||||
def verify(self):
|
|
||||||
if not self.state == StateChoices.PENDING:
|
|
||||||
raise APIException('this transaction is not pending')
|
|
||||||
|
|
||||||
with transaction.atomic():
|
|
||||||
self.deposit_to_payee_balance()
|
|
||||||
self.change_state(
|
|
||||||
StateChoices.PENDING,
|
|
||||||
StateChoices.SUCCESS,
|
|
||||||
error_message='this transaction is not pending'
|
|
||||||
)
|
|
||||||
return self
|
|
||||||
|
|
||||||
def get_role(self, entity):
|
|
||||||
|
|
||||||
if self.payer_id == entity.uuid:
|
|
||||||
return 1
|
|
||||||
elif self.payee_id == entity.uuid:
|
|
||||||
return 2
|
|
||||||
else:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def update_details(self, force_update=False):
|
|
||||||
if not isinstance(self.details, dict):
|
|
||||||
self.details = {}
|
|
||||||
self.save()
|
|
||||||
# detail = {'payer_name', 'payee_name', 'application_name', 'payer_avatar_url', 'payee_avatar_url', 'application_avatar_url'}
|
|
||||||
is_updated = False
|
|
||||||
if not self.details.get('payer_name'):
|
|
||||||
print('update payer')
|
|
||||||
if self.payer_type == TypeChoices.USER.value:
|
|
||||||
payer_details: PublicUser = get_user_details(self.payer_id)
|
|
||||||
self.details['payer_name'] = payer_details.first_name + payer_details.last_name
|
|
||||||
self.details['payer_avatar_url'] = payer_details.avatar
|
|
||||||
else:
|
|
||||||
payer_details = get_application_details(self.payer_id)
|
|
||||||
self.details['payer_name'] = payer_details.name
|
|
||||||
# TODO: get avatar from details
|
|
||||||
is_updated = True
|
|
||||||
print("payer_details:", payer_details)
|
|
||||||
print("is_updated:", is_updated)
|
|
||||||
|
|
||||||
if not self.details.get('payee_name'):
|
|
||||||
print('update payee')
|
|
||||||
if self.payee_type == TypeChoices.USER.value:
|
|
||||||
payee_details: PublicUser = get_user_details(self.payee_id)
|
|
||||||
self.details['payee_name'] = payee_details.first_name + payee_details.last_name
|
|
||||||
self.details['payee_avatar_url'] = payee_details.avatar
|
|
||||||
else:
|
|
||||||
payee_details = get_application_details(self.payee_id)
|
|
||||||
self.details['payee_name'] = payee_details.name
|
|
||||||
# TODO: get avatar from details
|
|
||||||
|
|
||||||
is_updated = True
|
|
||||||
print("payee_details:", payee_details)
|
|
||||||
print("is_updated:", is_updated)
|
|
||||||
|
|
||||||
if not self.details.get('application_name'):
|
|
||||||
print('update application')
|
|
||||||
application_details = get_application_details(self.application_id)
|
|
||||||
print('application_details:', application_details)
|
|
||||||
try:
|
|
||||||
self.details['application_name'] = application_details.name
|
|
||||||
# TODO: get avatar from details
|
|
||||||
|
|
||||||
is_updated = True
|
|
||||||
except Exception as e:
|
|
||||||
print("something went wrong")
|
|
||||||
print(e)
|
|
||||||
|
|
||||||
if is_updated:
|
|
||||||
print('update details')
|
|
||||||
self.save()
|
|
||||||
|
|
||||||
######################## category
|
######################## category
|
||||||
def category_withdraw_from_payer_balance(self):
|
def category_withdraw_from_payer_balance(self):
|
||||||
payer_is_allowed_negative_balance = self.payer_wallet.owner_type == TypeChoices.APPLICATION.value and str(
|
payer_is_allowed_negative_balance = self.payer_wallet.owner_type == TypeChoices.APPLICATION.value and str(
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
from .application import *
|
||||||
Loading…
Add table
Reference in a new issue