update bank gateway
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
import logging
|
||||
|
||||
from zeep import Client, Transport
|
||||
import requests
|
||||
|
||||
from azbankgateways.banks import BaseBank
|
||||
from azbankgateways.exceptions import SettingDoesNotExist
|
||||
from azbankgateways.exceptions.exceptions import BankGatewayRejectPayment
|
||||
from azbankgateways.exceptions.exceptions import (
|
||||
BankGatewayConnectionError,
|
||||
BankGatewayRejectPayment,
|
||||
)
|
||||
from azbankgateways.models import BankType, CurrencyEnum, PaymentStatus
|
||||
from azbankgateways.utils import get_json
|
||||
|
||||
|
||||
class Zarinpal(BaseBank):
|
||||
@@ -16,8 +20,12 @@ class Zarinpal(BaseBank):
|
||||
kwargs.setdefault("SANDBOX", 0)
|
||||
super(Zarinpal, self).__init__(**kwargs)
|
||||
self.set_gateway_currency(CurrencyEnum.IRT)
|
||||
self._payment_url = "https://www.zarinpal.com/pg/StartPay/{}/ZarinGate"
|
||||
self._sandbox_url = "https://sandbox.zarinpal.com/pg/StartPay/{}/ZarinGate"
|
||||
self._payment_type = 'payment'
|
||||
if self._sandbox:
|
||||
self._payment_type = 'sandbox'
|
||||
self._payment_url = f"https://{self._payment_type}.zarinpal.com/pg/v4/payment/request.json"
|
||||
self._startpay_url = f"https://{self._payment_type}.zarinpal.com/pg/StartPay/"
|
||||
self._verify_url = f"https://{self._payment_type}.zarinpal.com/pg/v4/payment/verify.json"
|
||||
|
||||
def get_bank_type(self):
|
||||
return BankType.ZARINPAL
|
||||
@@ -37,9 +45,7 @@ class Zarinpal(BaseBank):
|
||||
return 1000
|
||||
|
||||
def _get_gateway_payment_url_parameter(self):
|
||||
if self._sandbox:
|
||||
return self._sandbox_url.format(self.get_reference_number())
|
||||
return self._payment_url.format(self.get_reference_number())
|
||||
return self._startpay_url + "{}".format(self.get_reference_number())
|
||||
|
||||
def _get_gateway_payment_parameter(self):
|
||||
return {}
|
||||
@@ -54,14 +60,19 @@ class Zarinpal(BaseBank):
|
||||
def get_pay_data(self):
|
||||
description = "خرید با شماره پیگیری - {}".format(self.get_tracking_code())
|
||||
|
||||
return {
|
||||
"Description": description,
|
||||
"MerchantID": self._merchant_code,
|
||||
"Amount": self.get_gateway_amount(),
|
||||
"Email": None,
|
||||
"Mobile": self.get_mobile_number(),
|
||||
"CallbackURL": self._get_gateway_callback_url(),
|
||||
data = {
|
||||
"description": description,
|
||||
"merchant_id": self._merchant_code,
|
||||
"amount": self.get_gateway_amount(),
|
||||
"currency": self.get_gateway_currency(),
|
||||
"metadata": {},
|
||||
"callback_url": self._get_gateway_callback_url(),
|
||||
}
|
||||
mobile_number = self.get_mobile_number()
|
||||
if mobile_number:
|
||||
data["metadata"].update({"mobile": mobile_number})
|
||||
data.update(self.get_custom_data())
|
||||
return data
|
||||
|
||||
def prepare_pay(self):
|
||||
super(Zarinpal, self).prepare_pay()
|
||||
@@ -69,10 +80,9 @@ class Zarinpal(BaseBank):
|
||||
def pay(self):
|
||||
super(Zarinpal, self).pay()
|
||||
data = self.get_pay_data()
|
||||
client = self._get_client()
|
||||
result = client.service.PaymentRequest(**data)
|
||||
if result.Status == 100:
|
||||
token = result.Authority
|
||||
result = self._send_data(api=self._payment_url, data=data)
|
||||
if result['data']:
|
||||
token = result['data']['authority']
|
||||
self._set_reference_number(token)
|
||||
else:
|
||||
logging.critical("Zarinpal gateway reject payment")
|
||||
@@ -84,7 +94,7 @@ class Zarinpal(BaseBank):
|
||||
|
||||
def prepare_verify_from_gateway(self):
|
||||
super(Zarinpal, self).prepare_verify_from_gateway()
|
||||
token = self.get_request().GET.get("Authority", None)
|
||||
token = self.get_request().GET.get("Authority")
|
||||
self._set_reference_number(token)
|
||||
self._set_bank_record()
|
||||
|
||||
@@ -98,9 +108,9 @@ class Zarinpal(BaseBank):
|
||||
def get_verify_data(self):
|
||||
super(Zarinpal, self).get_verify_data()
|
||||
return {
|
||||
"MerchantID": self._merchant_code,
|
||||
"Authority": self.get_reference_number(),
|
||||
"Amount": self.get_gateway_amount(),
|
||||
"merchant_id": self._merchant_code,
|
||||
"authority": self.get_reference_number(),
|
||||
"amount": self.get_gateway_amount(),
|
||||
}
|
||||
|
||||
def prepare_verify(self, tracking_code):
|
||||
@@ -109,27 +119,26 @@ class Zarinpal(BaseBank):
|
||||
def verify(self, transaction_code):
|
||||
super(Zarinpal, self).verify(transaction_code)
|
||||
data = self.get_verify_data()
|
||||
client = self._get_client(timeout=10)
|
||||
try:
|
||||
result = client.service.PaymentVerification(**data)
|
||||
if result.Status in [100, 101]:
|
||||
self._set_payment_status(PaymentStatus.COMPLETE)
|
||||
else:
|
||||
self._set_payment_status(PaymentStatus.CANCEL_BY_USER)
|
||||
logging.debug("Zarinpal gateway unapprove payment")
|
||||
except:
|
||||
result = self._send_data(api=self._verify_url, data=data)
|
||||
if result['data'] and result['data']['code'] in [100, 101]:
|
||||
self._set_payment_status(PaymentStatus.COMPLETE)
|
||||
else:
|
||||
self._set_payment_status(PaymentStatus.CANCEL_BY_USER)
|
||||
logging.debug("Zarinpal gateway unapprove payment")
|
||||
|
||||
def _get_client(self, timeout=5):
|
||||
transport = Transport(timeout=timeout, operation_timeout=timeout)
|
||||
if self._sandbox:
|
||||
return Client(
|
||||
"https://sandbox.zarinpal.com/pg/services/WebGate/wsdl",
|
||||
transport=transport,
|
||||
)
|
||||
def _send_data(self, api, data):
|
||||
try:
|
||||
response = requests.post(api, json=data, timeout=self.get_timeout())
|
||||
except requests.Timeout:
|
||||
logging.exception("ZARINPAL time out gateway {}".format(data))
|
||||
raise BankGatewayConnectionError()
|
||||
except requests.ConnectionError:
|
||||
logging.exception("ZARINPAL time out gateway {}".format(data))
|
||||
raise BankGatewayConnectionError()
|
||||
|
||||
return Client(
|
||||
"https://www.zarinpal.com/pg/services/WebGate/wsdl",
|
||||
transport=transport,
|
||||
)
|
||||
response_json = get_json(response)
|
||||
if response_json['data']:
|
||||
self._set_transaction_status_text(response_json['data']['message'])
|
||||
else:
|
||||
self._set_transaction_status_text(response_json['errors']['message'])
|
||||
return response_json
|
||||
|
||||
Reference in New Issue
Block a user