Last active
April 13, 2020 23:32
-
-
Save dhavalsavalia/dd4f95a030920acf5ec202447aba9a0d to your computer and use it in GitHub Desktop.
file for for Django-PayTM demo | Medium Article:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from . import Checksum | |
from django.shortcuts import render | |
from django.conf import settings | |
from django.http import HttpResponse | |
from django.views.decorators.csrf import csrf_exempt | |
from payments.utils import VerifyPaytmResponse | |
import requests | |
def home(request): | |
return HttpResponse("<html><a href='http://localhost:8000/payment'>PayNow</html>") | |
def payment(request): | |
order_id = Checksum.__id_generator__() | |
bill_amount = "100" | |
data_dict = { | |
'MID': settings.PAYTM_MERCHANT_ID, | |
'INDUSTRY_TYPE_ID': settings.PAYTM_INDUSTRY_TYPE_ID, | |
'WEBSITE': settings.PAYTM_WEBSITE, | |
'CHANNEL_ID': settings.PAYTM_CHANNEL_ID, | |
'CALLBACK_URL': settings.PAYTM_CALLBACK_URL, | |
'MOBILE_NO': '7405505665', | |
'EMAIL': '[email protected]', | |
'CUST_ID': '123123', | |
'ORDER_ID':order_id, | |
'TXN_AMOUNT': bill_amount, | |
} # This data should ideally come from database | |
data_dict['CHECKSUMHASH'] = Checksum.generate_checksum(data_dict, settings.PAYTM_MERCHANT_KEY) | |
context = { | |
'payment_url': settings.PAYTM_PAYMENT_GATEWAY_URL, | |
'comany_name': settings.PAYTM_COMPANY_NAME, | |
'data_dict': data_dict | |
} | |
return render(request, 'payments/payment.html', context) | |
@csrf_exempt | |
def response(request): | |
resp = VerifyPaytmResponse(request) | |
if resp['verified']: | |
# save success details to db; details in resp['paytm'] | |
return HttpResponse("<center><h1>Transaction Successful</h1><center>", status=200) | |
else: | |
# check what happened; details in resp['paytm'] | |
return HttpResponse("<center><h1>Transaction Failed</h1><center>", status=400) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Redirecting...</title> | |
</head> | |
<body> | |
<br><br><h1><center>{{ company_name }}</center></h1><br><br><br> | |
<h1><center>Redirecting to payment gateway<br>Please do not refresh or go back.</center></h1> | |
<form method="POST" action="{{ payment_url }}", name="form1"> | |
{% csrf_token %} | |
{% for key, value in data_dict.items %} | |
<input type="hidden" name="{{key}}" value="{{value}}"> | |
{% endfor %} | |
<script type="text/javascript"> | |
document.form1.submit(); | |
</script> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from . import Checksum | |
from django.conf import settings | |
import requests | |
import json | |
def VerifyPaytmResponse(response): | |
response_dict = {} | |
if response.method == "POST": | |
data_dict = {} | |
for key in response.POST: | |
data_dict[key] = response.POST[key] | |
MID = data_dict['MID'] | |
ORDERID = data_dict['ORDERID'] | |
verify = Checksum.verify_checksum(data_dict, settings.PAYTM_MERCHANT_KEY, data_dict['CHECKSUMHASH']) | |
if verify: | |
STATUS_URL = settings.PAYTM_TRANSACTION_STATUS_URL | |
headers = { | |
'Content-Type': 'application/json', | |
} | |
data = '{"MID":"%s","ORDERID":"%s"}'%(MID, ORDERID) | |
check_resp = requests.post(STATUS_URL, data=data, headers=headers).json() | |
if check_resp['STATUS']=='TXN_SUCCESS': | |
response_dict['verified'] = True | |
response_dict['paytm'] = check_resp | |
return (response_dict) | |
else: | |
response_dict['verified'] = False | |
response_dict['paytm'] = check_resp | |
return (response_dict) | |
else: | |
response_dict['verified'] = False | |
return (response_dict) | |
response_dict['verified'] = False | |
return response_dict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment