├── README.md ├── urls.py └── views.py /README.md: -------------------------------------------------------------------------------- 1 | # zarinpal-django-py3 2 | 3 | این یک نمونه کد برای استفاده از زرین پال در جنگو در پایتون ۳ می‌باشد 4 | 5 | 6 | ## نصب ماژول Requests 7 | 8 | ``` 9 | pip install requests 10 | ``` 11 | 12 | **add to settings.py** 13 | 14 | > SANDBOX MODE 15 | 16 | > MERCHANT = "00000000-0000-0000-0000-000000000000" 17 | 18 | > SANDBOX = True 19 | -------------------------------------------------------------------------------- /urls.py: -------------------------------------------------------------------------------- 1 | # Github.com/Rasooll 2 | from django.urls import path 3 | from . import views 4 | 5 | urlpatterns = [ 6 | path('request/', views.send_request, name='request'), 7 | path('verify/', views.verify , name='verify'), 8 | ] 9 | -------------------------------------------------------------------------------- /views.py: -------------------------------------------------------------------------------- 1 | from django.conf import settings 2 | import requests 3 | import json 4 | 5 | 6 | #? sandbox merchant 7 | if settings.SANDBOX: 8 | sandbox = 'sandbox' 9 | else: 10 | sandbox = 'www' 11 | 12 | 13 | ZP_API_REQUEST = f"https://{sandbox}.zarinpal.com/pg/rest/WebGate/PaymentRequest.json" 14 | ZP_API_VERIFY = f"https://{sandbox}.zarinpal.com/pg/rest/WebGate/PaymentVerification.json" 15 | ZP_API_STARTPAY = f"https://{sandbox}.zarinpal.com/pg/StartPay/" 16 | 17 | amount = 1000 # Rial / Required 18 | description = "توضیحات مربوط به تراکنش را در این قسمت وارد کنید" # Required 19 | phone = 'YOUR_PHONE_NUMBER' # Optional 20 | # Important: need to edit for realy server. 21 | CallbackURL = 'http://127.0.0.1:8080/verify/' 22 | 23 | 24 | def send_request(request): 25 | data = { 26 | "MerchantID": settings.MERCHANT, 27 | "Amount": amount, 28 | "Description": description, 29 | "Phone": phone, 30 | "CallbackURL": CallbackURL, 31 | } 32 | data = json.dumps(data) 33 | # set content length by data 34 | headers = {'content-type': 'application/json', 'content-length': str(len(data)) } 35 | try: 36 | response = requests.post(ZP_API_REQUEST, data=data,headers=headers, timeout=10) 37 | 38 | if response.status_code == 200: 39 | response = response.json() 40 | if response['Status'] == 100: 41 | return {'status': True, 'url': ZP_API_STARTPAY + str(response['Authority']), 'authority': response['Authority']} 42 | else: 43 | return {'status': False, 'code': str(response['Status'])} 44 | return response 45 | 46 | except requests.exceptions.Timeout: 47 | return {'status': False, 'code': 'timeout'} 48 | except requests.exceptions.ConnectionError: 49 | return {'status': False, 'code': 'connection error'} 50 | 51 | 52 | def verify(authority): 53 | data = { 54 | "MerchantID": settings.MERCHANT, 55 | "Amount": amount, 56 | "Authority": authority, 57 | } 58 | data = json.dumps(data) 59 | # set content length by data 60 | headers = {'content-type': 'application/json', 'content-length': str(len(data)) } 61 | response = requests.post(ZP_API_VERIFY, data=data,headers=headers) 62 | 63 | if response.status_code == 200: 64 | response = response.json() 65 | if response['Status'] == 100: 66 | return {'status': True, 'RefID': response['RefID']} 67 | else: 68 | return {'status': False, 'code': str(response['Status'])} 69 | return response --------------------------------------------------------------------------------