Skip to content

Instantly share code, notes, and snippets.

@ziyadparekh
Created June 30, 2020 17:39
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 ziyadparekh/1f5e467adb5f42b93175eb201eefa970 to your computer and use it in GitHub Desktop.
Save ziyadparekh/1f5e467adb5f42b93175eb201eefa970 to your computer and use it in GitHub Desktop.
Safepay Python Code samples
# Create your views here.
import hashlib
import base64
import os
import hmac
import urllib
from datetime import datetime
import random
from django.shortcuts import redirect
from django.http import HttpResponse
import requests
from django.views.decorators.csrf import csrf_exempt
import json
@csrf_exempt
def index(request):
if request.method == "POST":
current_date = datetime.now()
order_number = str(random.randint(1000, 1000000)) + '-' + current_date.strftime("%Y-%m-%dT%H-%M-%S")
amount = request.POST.get('x_amount')
x_currency = request.POST.get('x_currency')
x_url_complete = request.POST.get('x_url_complete')
x_url_cancel = request.POST.get('x_url_cancel')
x_test = request.POST.get('x_test')
if x_test == 'true':
url = "https://sandbox.api.getsafepay.com/order/v1/init"
env = "sandbox"
redirectingUrl = "https://sandbox.api.getsafepay.com/components"
clientId = credential.apiKeyStaging
else:
url = "https://api.getsafepay.com/order/v1/init"
env = "production"
redirectingUrl = "https://www.getsafepay.com/components"
clientId = credential.apiKeyProduction
body = {
"client": clientId,
"amount": float(amount),
"currency": x_currency,
"environment": env
}
response = requests.post(url,json=body)
responseJson = response.json()
if response.status_code == 200:
token = responseJson['data']['token']
redirect_url = "https://"+os.environ['domain']+"/gateway/fulfill"
redirectingUrl = redirectingUrl + "?env="+env+"&beacon="+token+"&source=shopify&order_id="+x_reference+"&redirect_url="+redirect_url+"&cancel_url="+x_url_cancel
return redirect(redirectingUrl)
else:
content = {'reason': response.text}
return HttpResponse(json.dumps(content))
@csrf_exempt
def fulfillOrder(request):
if request.method == "POST":
token = request.POST.get("tracker")
message = bytes(request.POST.get("tracker"), 'utf-8')
if transaction.x_test == 'true':
secret = bytes(credentials.secretKeyStaging, 'utf-8')
else:
secret = bytes(credentials.secretKeyProduction, 'utf-8')
hash = hmac.new(secret, message, hashlib.sha256)
signature = hash.hexdigest()
if signature != request.POST.get("sig"):
return HttpResponse("Unauthorized")
## Mark your order paid
if response.status_code == 200:
print("Status code 200")
return redirect(transaction.x_url_complete)
elif response.status_code == 422:
print("Status code 422")
return redirect(transaction.x_url_cancel)
else:
return HttpResponse("Transaction not found")
@SaifullahUsmani693
Copy link

Please I am a Django developer and I want to know what each field stands for.I want to know the API and Its Documentation Links. I need it urgently and I want to integrate this payment method. It will be great if you guys can explain the code line by line and provide some documentation and the html if possible. Thanks

@MuqeetJanjua
Copy link

Please I am a Django developer and I want to know what each field stands for.I want to know the API and Its Documentation Links. I need it urgently and I want to integrate this payment method. It will be great if you guys can explain the code line by line and provide some documentation and the html if possible. Thanks

Have implemented it? I need to implement it in Django app. You can contact me on muqeetahmad25@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment