From 48ce77db974bf056749be9de7948dd0e5a179599 Mon Sep 17 00:00:00 2001 From: muhammadalive Date: Sun, 1 Dec 2024 09:53:56 +0500 Subject: [PATCH] small fix and improvements --- paynet/models.py | 1 + paynet/views.py | 70 +++++++++++++++++++++++++----------------------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/paynet/models.py b/paynet/models.py index f640740..16e90fc 100644 --- a/paynet/models.py +++ b/paynet/models.py @@ -4,6 +4,7 @@ class PaynetTransaction(models.Model): """ Represents a transaction made by a user on a specific service. + """ CREATED = 0 SUCCESSFUL = 1 diff --git a/paynet/views.py b/paynet/views.py index a3e382b..07d70fc 100644 --- a/paynet/views.py +++ b/paynet/views.py @@ -178,20 +178,22 @@ def check_transaction(self, params, rpc_id): transaction_id=serializer.validated_data['transactionId'], service_id=serializer.validated_data['serviceId'] ) - return Response({ - "jsonrpc": "2.0", - "id": rpc_id, - "result": { - "transactionState": transaction.status, - "timestamp": transaction.updated_at.strftime('%Y-%m-%d %H:%M:%S'), - "providerTrnId": transaction.id - } - }) + except Transaction.DoesNotExist as exc: raise exceptions.TransactionNotFound( rpc_id=rpc_id, exc=exc ) from exc + return Response({ + "jsonrpc": "2.0", + "id": rpc_id, + "result": { + "transactionState": transaction.status, + "timestamp": transaction.updated_at.strftime('%Y-%m-%d %H:%M:%S'), # noqa + "providerTrnId": transaction.id + } + }) + def cancel_transaction(self, params, rpc_id): """ cancel a transaction method process @@ -205,34 +207,35 @@ def cancel_transaction(self, params, rpc_id): service_id=serializer.validated_data['serviceId'] ) - if transaction.status == Transaction.CANCELLED: - raise exceptions.TransactionAlreadyCancelled( - rpc_id=rpc_id - ) - - if not self.check_balance(transaction.amount): - raise exceptions.InsufficientFunds() - - transaction.status = Transaction.CANCELLED - transaction.save() - - # callback cancelled transaction event - self.cancelled_payment(params) - - return Response({ - "jsonrpc": "2.0", - "id": rpc_id, - "result": { - "providerTrnId": transaction.id, - "timestamp": transaction.updated_at.strftime('%Y-%m-%d %H:%M:%S'), - "transactionState": Transaction.CANCELLED - } - }) except Transaction.DoesNotExist as exc: raise exceptions.TransactionNotFound( rpc_id=rpc_id, exc=exc ) from exc + if transaction.status == Transaction.CANCELLED: + raise exceptions.TransactionAlreadyCancelled( + rpc_id=rpc_id + ) + + if not self.check_balance(transaction.amount): + raise exceptions.InsufficientFunds() + + transaction.status = Transaction.CANCELLED + transaction.save() + + # callback cancelled transaction event + self.cancelled_payment(params) + + return Response({ + "jsonrpc": "2.0", + "id": rpc_id, + "result": { + "providerTrnId": transaction.id, + "timestamp": transaction.updated_at.strftime('%Y-%m-%d %H:%M:%S'), # noqa + "transactionState": Transaction.CANCELLED + } + }) + def check_balance(self, amount) -> bool: """ Check if the amount is within the balance limits. @@ -281,7 +284,8 @@ def get_information(self, params, rpc_id): if getattr(settings, 'PAYNET_ACCOUNT_INFO_FIELDS', None): info_fields = settings.PAYNET_ACCOUNT_INFO_FIELDS - account = AccountModel.objects.filter(id=account_id).values(*info_fields) + account = AccountModel.objects\ + .filter(id=account_id).values(*info_fields) if not account: raise exceptions.ClientNotFound(rpc_id=rpc_id)