diff --git a/README.md b/README.md index b0a9d90..2c01d0a 100644 --- a/README.md +++ b/README.md @@ -499,8 +499,9 @@ pre-commit install * [amirreza8002](https://github.com/amirreza8002) رفع مشکل ترجمه * [ahmadrezanavaie](https://github.com/ahmadrezanavaie) رفع مشکل ترجمه * [zamoosh](https://github.com/zamoosh) اضافه کردن وضعیت های تراکنش در بانک ملت -* [birddevelper](https://github.com/birddevelper) الزامی کردن وجود referrer برای درگاه بانک ملی +* [birddevelper](https://github.com/birddevelper) الزامی کردن وجود referrer برای درگاه های بانک ملی و سامان * [apidemy](https://github.com/apidemy) ریفکتور _is_strict_origin_policy_enabled و پیروی از DRY +* [TinyPuff](https://github.com/TinyPuff) رفع مشکل Pay.ir ## License The MIT License (MIT). Please see [License File](LICENSE) for more information. diff --git a/azbankgateways/banks/__init__.py b/azbankgateways/banks/__init__.py index fdc85ce..9172f99 100644 --- a/azbankgateways/banks/__init__.py +++ b/azbankgateways/banks/__init__.py @@ -3,6 +3,7 @@ from .bmi import BMI # noqa from .idpay import IDPay # noqa from .mellat import Mellat # noqa +from .payV1 import PayV1 # noqa from .sep import SEP # noqa from .zarinpal import Zarinpal # noqa from .zibal import Zibal # noqa diff --git a/azbankgateways/banks/payV1.py b/azbankgateways/banks/payV1.py index 2dce312..830eab7 100644 --- a/azbankgateways/banks/payV1.py +++ b/azbankgateways/banks/payV1.py @@ -2,9 +2,9 @@ import logging import requests +from requests import HTTPError, JSONDecodeError, Timeout from azbankgateways.banks import BaseBank -from azbankgateways.default_settings import TRACKING_CODE_QUERY_PARAM from azbankgateways.exceptions import BankGatewayConnectionError, SettingDoesNotExist from azbankgateways.exceptions.exceptions import ( BankGatewayRejectPayment, @@ -87,8 +87,13 @@ def pay(self): def prepare_verify_from_gateway(self): super(PayV1, self).prepare_verify_from_gateway() - for method in ["GET", "POST", "data"]: - token = getattr(self.get_request(), method).get(TRACKING_CODE_QUERY_PARAM, None) + request = self.get_request() + for method in [ + "GET", + "POST", + ]: + token = getattr(request, method, {}).get("token") + if token: self._set_reference_number(token) self._set_bank_record() @@ -105,10 +110,7 @@ def verify_from_gateway(self, request): def get_verify_data(self): super(PayV1, self).get_verify_data() - data = { - "api": self._merchant_code(), - "token": self.get_reference_number(), - } + data = {"api": self._merchant_code, "token": self.get_reference_number(), "status": self._bank.status} return data def prepare_verify(self, tracking_code): @@ -116,26 +118,22 @@ def prepare_verify(self, tracking_code): def verify(self, tracking_code): super(PayV1, self).verify(tracking_code) - data = self.get_verify_data() - response = self._send_data(self._verify_api_url, data, timeout=10) - response_json = response.json() - status = PaymentStatus.COMPLETE - if int(response_json["status"]) != 1: - if int(response_json["errorCode"]) == -5: - status = PaymentStatus.ERROR - elif int(response_json["errorCode"]) == -9: - status = PaymentStatus.EXPIRE_VERIFY_PAYMENT - elif int(response_json["errorCode"]) == -15: - status = PaymentStatus.CANCEL_BY_USER - elif int(response_json["errorCode"]) == -27: - status = PaymentStatus.RETURN_FROM_BANK + try: + response = self._send_data(self._verify_api_url, data, timeout=10) + response.raise_for_status() + response_json = response.json() + status = str(response_json.get("status", 0)) + if status == '1': + status = PaymentStatus.COMPLETE + extra_information = json.dumps(response_json) + self._bank.extra_information = extra_information else: status = PaymentStatus.ERROR + except (JSONDecodeError, HTTPError, Timeout): + status = PaymentStatus.ERROR self._set_payment_status(status) - extra_information = json.dumps(response_json) - self._bank.extra_information = extra_information self._bank.save() def _send_data(self, url, data, timeout=5) -> requests.post: