Skip to content

Instantly share code, notes, and snippets.

@pourya2374
Created February 28, 2019 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pourya2374/5764dbf76ba592870cb3737e7972ffa9 to your computer and use it in GitHub Desktop.
Save pourya2374/5764dbf76ba592870cb3737e7972ffa9 to your computer and use it in GitHub Desktop.
from zeep import Client
class Zarinpal:
MERCHANT_ID = '{YOUR MERCHANT_ID}'
ZARINPAL_WEBSERVICE = 'https://www.zarinpal.com/pg/services/WebGate/wsdl'
ERRORS = {
-1: 'نقص اطلاعات',
-2: 'آی.پی یا merchant پذیرنده صحیح نیست',
-3: 'امکان پرداخت با رقم درخواستی میسر نیست',
-4: 'سطح تائید پذیرنده پائین‌تر از سطح نقره‌ای',
-11: 'درخواست یافت نشد',
-12: 'امکان ویرایش درخواست وجود ندارد',
-21: 'هیچ نوع عملیات مالی برای این تراکنش یافت نشد',
-22: 'تراکنش ناموفق',
-33: 'رقم تراکنش با رقم پرداخت شده مطابقت ندارد',
-34: 'سقف تقسیم تراکنش از لحاظ تعداد یا رقم عبور کرده',
-40: 'اجازه دسترسی وجود ندارد',
-41: 'اطلاعات اضافی ارسال شده غیرمعتبر است',
-42: 'مدت زمان طول عمر شناسه معتبر نیست',
-54: 'درخواست مورد نظر آرشیو شده',
'NOK': 'تراکنش ناموفق یا اینکه کاربر پرداخت را کنسل کرده',
100: 'پرداخت موفق',
101: 'پرداخت موفق'
}
def __init__(self):
self.client = Client(Zarinpal.ZARINPAL_WEBSERVICE)
def payment_request(self, amount, desc, phone, return_url):
response = self.client.service.PaymentRequest(
Zarinpal.MERCHANT_ID,
amount,
desc,
"",
phone,
return_url
)
if response.Status == 100:
authority = response.Authority.lstrip('0')
else:
raise Exception(Zarinpal.ERRORS[response.Status])
return authority, self.gen_pay_url(authority)
def verify_payment(self, authority, amount):
# todo: add tenacity package for retrying
response = self.client.service.PaymentVerification(
Zarinpal.MERCHANT_ID,
authority,
amount
)
if response.Status == 100 or response.Status == 101:
return response.RefID
else:
return None
def gen_pay_url(self, authority):
return 'https://www.zarinpal.com/pg/StartPay/{}/ZarinGate'.format(authority)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment