Merge pull request 'FIX(wallet): add cancel state and add success to rollback init states' (#8) from FEATURE/new-commission-calculation-and-toman-api into master

Reviewed-on: https://git.mindplus-dev.ir/sahama/wallet/pulls/8
This commit is contained in:
ghasemi 2026-02-14 08:13:53 -05:00
commit e27fe27bf0
3 changed files with 33 additions and 1 deletions

View file

@ -11,6 +11,7 @@ class StateChoices(IntegerChoices):
FAILED = 6 , _('failed')
EXPECTED_FAILURE = 7 , _('expected_failure') # no exact data available but guessed to be failed
ROLLED_BACK = 8 , _('ROLLED_BACK')
CANCELED = 9, _('CANCELED')
class TypeChoices(IntegerChoices):

View file

@ -0,0 +1,18 @@
# Generated by Django 5.1.7 on 2026-02-14 11:48
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('wallet', '0017_transaction_reference_alter_transaction_state_and_more'),
]
operations = [
migrations.AlterField(
model_name='transaction',
name='state',
field=models.IntegerField(choices=[(1, 'created'), (2, 'delayed'), (3, 'pending'), (4, 'incomplete'), (5, 'success'), (6, 'failed'), (7, 'expected_failure'), (8, 'ROLLED_BACK'), (9, 'CANCELED')], default=1),
),
]

View file

@ -356,7 +356,7 @@ class Transaction(BaseModel):
)
return self
def rollback(self):
def cancel(self):
if self.state not in [StateChoices.PENDING, StateChoices.DELAYED, StateChoices.INCOMPLETE]:
raise APIException('this transaction is not pending')
@ -364,6 +364,19 @@ class Transaction(BaseModel):
self.rollback_withdraw_to_payer_balance()
self.change_state(
[StateChoices.PENDING, StateChoices.DELAYED, StateChoices.INCOMPLETE],
StateChoices.CANCELED,
error_message=_('TRANSACTION_STATE_NOT_SUITABLE_FOR_CANCELING')
)
return self
def rollback(self):
if self.state not in [StateChoices.PENDING, StateChoices.SUCCESS, StateChoices.DELAYED, StateChoices.INCOMPLETE]:
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')
)