Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.
Open
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
70 changes: 41 additions & 29 deletions views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.conf import settings
from django.shortcuts import redirect
from django.http import HttpResponse
import requests
import json

Expand All @@ -17,7 +19,7 @@
amount = 1000 # Rial / Required
description = "توضیحات مربوط به تراکنش را در این قسمت وارد کنید" # Required
phone = 'YOUR_PHONE_NUMBER' # Optional
# Important: need to edit for realy server.
# Important: need to edit for real server.
CallbackURL = 'http://127.0.0.1:8080/verify/'


Expand All @@ -31,39 +33,49 @@ def send_request(request):
}
data = json.dumps(data)
# set content length by data
headers = {'content-type': 'application/json', 'content-length': str(len(data)) }
headers = {'accept': 'application/json', 'content-type': 'application/json', 'content-length': str(len(data))}
try:
response = requests.post(ZP_API_REQUEST, data=data,headers=headers, timeout=10)
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']}
response_json = response.json()
authority = response_json['Authority']
if response_json['Status'] == 100:
return redirect(ZP_API_STARTPAY+authority)
else:
return {'status': False, 'code': str(response['Status'])}
return response

return HttpResponse('Error')
return HttpResponse('response failed')
except requests.exceptions.Timeout:
return {'status': False, 'code': 'timeout'}
return HttpResponse('Timeout Error')
except requests.exceptions.ConnectionError:
return {'status': False, 'code': 'connection error'}
return HttpResponse('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
def verify(request):
authority = request.GET.get('Authority')
status = request.GET.get('Status')
if status == 'OK' and authority:
data = {
"MerchantID": settings.MERCHANT,
"Amount": amount,
"Authority": authority,
}
data = json.dumps(data)
# set content length by data
headers = {'accept': 'application/json', 'content-type': 'application/json', 'content-length': str(len(data))}
try:
response = requests.post(ZP_API_VERIFY, data=data, headers=headers)
if response.status_code == 200:
response_json = response.json()
reference_id = response_json['RefID']
if response['Status'] == 100:
return HttpResponse(f'successful , RefID: {reference_id}')
else:
return HttpResponse('Error')
return HttpResponse('response failed')
except requests.exceptions.Timeout:
return HttpResponse('Timeout Error')
except requests.exceptions.ConnectionError:
return HttpResponse('Connection Error')
else:
return HttpResponse('Not ok')