From a8b32a16f48233685fb7c9e6ee6651b41a884379 Mon Sep 17 00:00:00 2001 From: Haydar Ghasemi Date: Tue, 17 Feb 2026 15:44:27 +0330 Subject: [PATCH] FIX(settlement): add rollback_deposit_to_payee_balance add rollback_deposit_to_payee_balance --- apps/wallet/models.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/apps/wallet/models.py b/apps/wallet/models.py index 708cf60..d009fd4 100755 --- a/apps/wallet/models.py +++ b/apps/wallet/models.py @@ -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):