This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathviews.py
69 lines (57 loc) · 2.29 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from django.conf import settings
import requests
import json
#? sandbox merchant
if settings.SANDBOX:
sandbox = 'sandbox'
else:
sandbox = 'www'
ZP_API_REQUEST = f"https://{sandbox}.zarinpal.com/pg/rest/WebGate/PaymentRequest.json"
ZP_API_VERIFY = f"https://{sandbox}.zarinpal.com/pg/rest/WebGate/PaymentVerification.json"
ZP_API_STARTPAY = f"https://{sandbox}.zarinpal.com/pg/StartPay/"
amount = 1000 # Rial / Required
description = "توضیحات مربوط به تراکنش را در این قسمت وارد کنید" # Required
phone = 'YOUR_PHONE_NUMBER' # Optional
# Important: need to edit for realy server.
CallbackURL = 'http://127.0.0.1:8080/verify/'
def send_request(request):
data = {
"MerchantID": settings.MERCHANT,
"Amount": amount,
"Description": description,
"Phone": phone,
"CallbackURL": CallbackURL,
}
data = json.dumps(data)
# set content length by data
headers = {'content-type': 'application/json', 'content-length': str(len(data)) }
try:
response = requests.post(ZP_API_REQUEST, data=data,headers=headers, timeout=10)
if response.status_code == 200:
response = response.json()
if response['Status'] == 100:
return {'status': True, 'url': ZP_API_STARTPAY + str(response['Authority']), 'authority': response['Authority']}
else:
return {'status': False, 'code': str(response['Status'])}
return response
except requests.exceptions.Timeout:
return {'status': False, 'code': 'timeout'}
except requests.exceptions.ConnectionError:
return {'status': False, 'code': 'connection error'}
def verify(authority):
data = {
"MerchantID": settings.MERCHANT,
"Amount": amount,
"Authority": authority,
}
data = json.dumps(data)
# set content length by data
headers = {'content-type': 'application/json', 'content-length': str(len(data)) }
response = requests.post(ZP_API_VERIFY, data=data,headers=headers)
if response.status_code == 200:
response = response.json()
if response['Status'] == 100:
return {'status': True, 'RefID': response['RefID']}
else:
return {'status': False, 'code': str(response['Status'])}
return response