|
4 | 4 | from decimal import Decimal |
5 | 5 |
|
6 | 6 | from django.conf import settings |
| 7 | +from django.core.exceptions import ValidationError |
7 | 8 | from django.utils.module_loading import import_string |
8 | 9 | from rest_framework import views |
9 | 10 | from rest_framework.response import Response |
@@ -33,6 +34,9 @@ def wrapper(*args, **kwargs): |
33 | 34 | logger.error(f"Account does not exist: {exc} {args} {kwargs}") |
34 | 35 | raise exceptions.AccountDoesNotExist(str(exc)) from exc |
35 | 36 |
|
| 37 | + except ValidationError: |
| 38 | + raise exceptions.AccountDoesNotExist("Invalid account identifier.") |
| 39 | + |
36 | 40 | except PaymeTransactions.DoesNotExist as exc: |
37 | 41 | logger.error(f"Transaction does not exist: {exc} {args} {kwargs}") |
38 | 42 | raise exceptions.AccountDoesNotExist(str(exc)) from exc |
@@ -112,17 +116,11 @@ def fetch_account(self, params: dict): |
112 | 116 | """ |
113 | 117 | Fetch account based on settings and params. |
114 | 118 | """ |
115 | | - account_field = settings.PAYME_ACCOUNT_FIELD |
116 | | - |
117 | | - account_value = params['account'].get(account_field) |
| 119 | + account_value = params["account"].get(settings.PAYME_ACCOUNT_FIELD) |
118 | 120 | if not account_value: |
119 | 121 | raise exceptions.InvalidAccount("Missing account field in parameters.") |
120 | 122 |
|
121 | | - # hard change |
122 | | - if account_field == "order_id": |
123 | | - account_field = "id" |
124 | | - |
125 | | - account = AccountModel.objects.get(**{account_field: account_value}) |
| 123 | + account = AccountModel.objects.get(pk=account_value) |
126 | 124 |
|
127 | 125 | return account |
128 | 126 |
|
@@ -169,13 +167,13 @@ def create_transaction(self, params) -> response.CreateTransaction: |
169 | 167 | defaults = { |
170 | 168 | "amount": amount, |
171 | 169 | "state": PaymeTransactions.INITIATING, |
172 | | - "account_id": account.id, |
| 170 | + "account_id": account.pk, |
173 | 171 | } |
174 | 172 |
|
175 | 173 | # Handle already existing transaction with the same ID for one-time payments |
176 | 174 | if settings.PAYME_ONE_TIME_PAYMENT: |
177 | 175 | # Check for an existing transaction with a different transaction_id for the given account |
178 | | - if PaymeTransactions.objects.filter(account_id=account.id).exclude(transaction_id=transaction_id).exists(): |
| 176 | + if PaymeTransactions.objects.filter(account_id=account.pk).exclude(transaction_id=transaction_id).exists(): |
179 | 177 | message = f"Transaction {transaction_id} already exists (Payme)." |
180 | 178 | logger.warning(message) |
181 | 179 | raise exceptions.TransactionAlreadyExists(message) |
|
0 commit comments