FIX(settlement): add rollback_deposit_to_payee_balance
add rollback_deposit_to_payee_balance
This commit is contained in:
parent
0a179bf350
commit
a8b32a16f4
1 changed files with 23 additions and 9 deletions
|
|
@ -253,8 +253,8 @@ class Transaction(BaseModel):
|
|||
else:
|
||||
raise UnprocessableEntity('not enough balance')
|
||||
|
||||
def rollback_withdraw_to_payer_balance(self):
|
||||
logger.debug('start rollback deposit_to_payee_balance')
|
||||
def rollback_withdraw_from_payer_balance(self):
|
||||
logger.debug('start rollback withdraw_from_payer_balance')
|
||||
logger.info(f'payer_account balance {self.payer_account} -> {self.amount}')
|
||||
self.payer_account.balance = F('balance') + self.amount
|
||||
self.payer_account.save(update_fields=['balance'])
|
||||
|
|
@ -265,6 +265,12 @@ class Transaction(BaseModel):
|
|||
self.payee_account.balance = F('balance') + self.amount
|
||||
self.payee_account.save(update_fields=['balance'])
|
||||
|
||||
def rollback_deposit_to_payee_balance(self):
|
||||
logger.debug('start rollback deposit_to_payee_balance')
|
||||
logger.info(f'payee_account balance {self.payee_account} -> {self.amount}')
|
||||
self.payee_account.balance = F('balance') - self.amount
|
||||
self.payee_account.save(update_fields=['balance'])
|
||||
|
||||
# TODO: refactor
|
||||
def pay(self, payer_id=None, payer_type=None):
|
||||
if not self.payer_id and not (payer_id and payer_type):
|
||||
|
|
@ -369,7 +375,7 @@ class Transaction(BaseModel):
|
|||
raise APIException('this transaction is not pending')
|
||||
|
||||
with transaction.atomic():
|
||||
self.rollback_withdraw_to_payer_balance()
|
||||
self.rollback_withdraw_from_payer_balance()
|
||||
self.change_state(
|
||||
[StateChoices.PENDING, StateChoices.DELAYED, StateChoices.INCOMPLETE],
|
||||
StateChoices.CANCELED,
|
||||
|
|
@ -382,12 +388,20 @@ class Transaction(BaseModel):
|
|||
raise APIException('this transaction is not pending')
|
||||
|
||||
with transaction.atomic():
|
||||
self.rollback_withdraw_to_payer_balance()
|
||||
self.change_state(
|
||||
[StateChoices.PENDING, StateChoices.SUCCESS, StateChoices.DELAYED, StateChoices.INCOMPLETE],
|
||||
StateChoices.ROLLED_BACK,
|
||||
error_message=_('TRANSACTION_STATE_NOT_SUITABLE_FOR_ROLLBACK')
|
||||
)
|
||||
self.rollback_withdraw_from_payer_balance()
|
||||
if self.state == StateChoices.SUCCESS:
|
||||
self.rollback_deposit_to_payee_balance()
|
||||
self.change_state(
|
||||
[StateChoices.SUCCESS],
|
||||
StateChoices.ROLLED_BACK,
|
||||
error_message=_('TRANSACTION_STATE_NOT_SUITABLE_FOR_ROLLBACK')
|
||||
)
|
||||
else:
|
||||
self.change_state(
|
||||
[StateChoices.PENDING, StateChoices.DELAYED, StateChoices.INCOMPLETE],
|
||||
StateChoices.CANCELED,
|
||||
error_message=_('TRANSACTION_STATE_NOT_SUITABLE_FOR_CANCEL')
|
||||
)
|
||||
return self
|
||||
|
||||
def get_role(self, entity):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue