Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed PayV1 gateway #117

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions azbankgateways/banks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
from .sep import SEP # noqa
from .zarinpal import Zarinpal # noqa
from .zibal import Zibal # noqa
from .payV1 import PayV1
18 changes: 15 additions & 3 deletions azbankgateways/banks/banks.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,24 @@ def verify(self, tracking_code):

def ready(self) -> Bank:
self.pay()
self._request.session['tracking_code'] = self.get_tracking_code()
self._request.session['callback_url'] = self._client_callback_url
bank = Bank.objects.create(
bank_choose_identifier=self.identifier,
bank_type=self.get_bank_type(),
amount=self.get_amount(),
reference_number=self.get_reference_number(),
response_result=self.get_transaction_status_text(),
tracking_code=self.get_tracking_code(),
callback_url=self._client_callback_url,
)
self._bank = bank
bank_object = Bank.objects.get(tracking_code=bank.tracking_code)
self._set_payment_status(PaymentStatus.WAITING)
if self._client_callback_url:
if self._client_callback_url and str(bank_object.bank_type).upper() != 'PAYV1':
self._bank.callback_url = self._client_callback_url
elif str(bank_object.bank_type).upper() == 'PAYV1':
bank_object.callback_url = self._client_callback_url
return bank

@abc.abstractmethod
Expand All @@ -145,13 +151,19 @@ def verify_from_gateway(self, request):
self.prepare_verify_from_gateway()
self._set_payment_status(PaymentStatus.RETURN_FROM_BANK)
self.verify(self.get_tracking_code())


def get_client_callback_url(self):
"""این متد پس از وریفای شدن استفاده خواهد شد. لینک برگشت را بر میگرداند.حال چه وریفای موفقیت آمیز باشد چه با
لغو کاربر مواجه شده باشد"""
tracking_code = str(self._request.session.get('tracking_code'))
callback_url = self._request.session.get('callback_url')
del self._request.session['tracking_code']
del self._request.session['callback_url']

return append_querystring(
self._bank.callback_url,
{settings.TRACKING_CODE_QUERY_PARAM: self.get_tracking_code()},
callback_url,
{settings.TRACKING_CODE_QUERY_PARAM: tracking_code},
)

def redirect_client_callback(self):
Expand Down
63 changes: 39 additions & 24 deletions azbankgateways/banks/payV1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
BankGatewayRejectPayment,
BankGatewayStateInvalid,
)
from azbankgateways.models import BankType, CurrencyEnum, PaymentStatus
from azbankgateways.models import BankType, CurrencyEnum, PaymentStatus, Bank


class PayV1(BaseBank):
Expand Down Expand Up @@ -58,7 +58,8 @@ def get_pay_data(self):
"amount": self.get_gateway_amount(),
"redirect": self._get_gateway_callback_url(),
"mobile": self.get_mobile_number(),
"factorNumber": self.get_tracking_code(),
# "factorNumber": self.get_tracking_code(),
"factorNumber": self._request.session.get('tracking_code'),
}
return data

Expand Down Expand Up @@ -87,14 +88,16 @@ 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', None)

if token:
self._set_reference_number(token)
self._set_bank_record()
break
else:
raise BankGatewayStateInvalid
else:
raise BankGatewayStateInvalid

def verify_from_gateway(self, request):
super(PayV1, self).verify_from_gateway(request)
Expand All @@ -106,7 +109,7 @@ def verify_from_gateway(self, request):
def get_verify_data(self):
super(PayV1, self).get_verify_data()
data = {
"api": self._merchant_code(),
"api": self._merchant_code,
"token": self.get_reference_number(),
}
return data
Expand All @@ -119,23 +122,35 @@ def verify(self, 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
else:
status = PaymentStatus.ERROR

try:
response_json = response.json()
if int(response_json["status"]) == 1:
status = PaymentStatus.COMPLETE
extra_information = json.dumps(response_json)
self._bank.extra_information = extra_information
except:
status = PaymentStatus.ERROR
# error codes
if "</html>" in response.text:
for i in range(-28, 1):
if f'<span class="server-error-code">{i}</span>' in response.text:
print(f"Status Code: {i}")
status_code = int(i)
break

if status_code == -5:
status = PaymentStatus.ERROR
elif status_code == -9:
status = PaymentStatus.EXPIRE_VERIFY_PAYMENT
elif status_code == -15:
status = PaymentStatus.CANCEL_BY_USER
elif status_code == -27:
status = PaymentStatus.RETURN_FROM_BANK
else:
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:
Expand All @@ -149,4 +164,4 @@ def _send_data(self, url, data, timeout=5) -> requests.post:
logging.exception("PayV1 time out gateway {}".format(data))
raise BankGatewayConnectionError()

return response
return response