I am working on a django webapp. I connected the paytm payment gateway with the django app. I did everything according to the docs, and everything works. almost.
I am having a problem when calling the callback URL once the payment is over.
Here is the code
views.py
def donate(request):
if request.method == "POST":
form = DonateForm(request.POST)
name = request.POST.get('firstName')
phone = request.POST.get('phone')
email = request.POST.get('email')
amount = float("{0:.2f}".format(int(request.POST.get('amount'))))
ord_id = OrdID()
cust_id = CustID()
paytm_params = {
"MID" : MERCHANTID,
"WEBSITE" : "WEBSTAGING",
"INDUSTRY_TYPE_ID" : "Retail",
"CHANNEL_ID" : "WEB",
"ORDER_ID" : ord_id,
"CUST_ID" : cust_id,
"MOBILE_NO" : phone,
"EMAIL" : email,
"TXN_AMOUNT" : str(amount),
"CALLBACK_URL" : "http://127.0.0.1:8000/handlerequest/",
}
paytm_params['CHECKSUMHASH'] = Checksum.generate_checksum(paytm_params, MERCHANTKEY)
return render(request, 'paytm.html', {'paytm_params': paytm_params})
else:
form = DonateForm()
context = {'Donate': form}
return render(request, 'donate.html', context=context)
@csrf_exempt
def handlerequest(request):
form = request.POST
response_dict = {}
for i in form.keys():
response_dict[i] = form[i]
if i == 'CHECKSUMHASH':
checksum = form[i]
verify = Checksum.verify_checksum(response_dict, MERCHANTKEY, checksum)
if verify:
if response_dict['RESPCODE'] == '01':
print('order successful')
else:
print('error: ' + response_dict['RESPMSG'])
return render(request, 'paymentstatus.html', {'response': response_dict})
urls.py
path('donate', views.donate, name='donate'),
path('handlerequest', views.handlerequest, name='handlerequest'),
donate.html
<form class="test_paytm" action="{% url 'donate' %}" method="post">
{% csrf_token %}
<div class="row">
<div class="col">
{{ Donate.firstName|as_crispy_field }}
</div>
<div class="col">
{{ Donate.lastName|as_crispy_field }}
</div>
</div>
<div class="row">
<div class="col">
{{ Donate.email|as_crispy_field }}
</div>
<div class="col">
{{ Donate.phone|as_crispy_field }}
</div>
</div>
<div class="row">
<div class="col">
{{ Donate.amount|as_crispy_field }}
</div>
</div>
<button type="submit" name="button" class="btn btn-lg mb-5 contact_submit">Donate</button>
</form>
paymentstatus.html
<div class="container">
{% if response_dict.RESPCODE == 01 %}
<center>
<h2>Thank you for your donation</h2>
<p>
We are thrilled to have your support. Through your donation we will be able to accomplish our goal. You truly make the difference for us, and we are
extremely grateful!
</p>
</center>
<h3>Order ID: {{response_dict.ORDERID}}</h3>
<h3>Order Date: {{response_dict.TXNDATE}}</h3>
<h3>Amount: {{response_dict.TXNAMOUNT}}</h3>
<h3>Payment Mode: {{response_dict.PAYMENTMODE}}</h3>
{% else %}
<center>
<p>
There seems to be a problem. We will try to fix this from our end.
</p>
</center>
{% endif %}
</div>
But once the payment is over, The website is not calling handlerequest
from views.py
correctly. That is why I had added the @csrf_exempt
so that an outside page can call the url without any issues. But I am still getting the 403 error. I am not sure what I am doing wrong