Skip to content

Commit

Permalink
small fix and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammadali-Akbarov committed Dec 1, 2024
1 parent 7d99be6 commit 48ce77d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
1 change: 1 addition & 0 deletions paynet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class PaynetTransaction(models.Model):
"""
Represents a transaction made by a user on a specific service.
"""
CREATED = 0
SUCCESSFUL = 1
Expand Down
70 changes: 37 additions & 33 deletions paynet/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 48ce77d

Please sign in to comment.