Django: unable to read POST parameters sent by payment gateway

巧了我就是萌 提交于 2020-04-07 08:05:33

问题


I am unable to read POST parameters sent by payment gateway after payment was processed. Payment gateway redirects to returnUrl (I pass this to payment gateway before payment was processed) with some parameters by POST request.

In url.py

path('cashfreeresponse/',views.cashfree_response, name='cashfree_response'),

in views.py

@csrf_exempt
@login_required
def cashfree_response(request):
    print(request.method)
    if request.method == "POST":
        print('inside post method')
        print(request.POST.get('cf_subReferenceId'))
    if request.method == "GET":
        print('inside get method')
        print(request.GET.get('cf_subReferenceId'))

print(request.method) is showing as GET but it was supposed be POST method also print(request.GET.get('cf_subReferenceId')) and print(request.POST.get('cf_subReferenceId')) are showing as None.

But it looks like payment gateway sending parameters as POST method. When I pass returnUrl as http://127.0.0.1:8000/cashfreeresponse instead of http://127.0.0.1:8000/cashfreeresponse/ ('/' is missing in the first one) then I am getting error as

You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/cashfreeresponse/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

but I see in my google chrome page that payment gateway sending parameters with POST request. Below image shows parameters sent by payment gateway by POST request

If I change urls.py to

path('cashfreeresponse',views.cashfree_response, name='cashfree_response'),

in settings.py to

APPEND_SLASH = False

and returnUrl to http://127.0.0.1:8000/cashfreeresponse then I am not getting any error but still not receiving any parameters. How do I read those parameters sent by payment gateway by POST request? Here is the documentation for payment gateway.

Update:

I tried all different combinations of APPEND_SLASH, urls.py and returnUrl. It didn't work in any case. Below are the types of issues I got with each combination.

APPEND_SLASH = True
path('cashfreeresponse/',views.cashfree_response, name='cashfree_response'),
returnUrl = 'http://127.0.0.1:8000/cashfreeresponse/'

APPEND_SLASH = True
path('cashfreeresponse',views.cashfree_response, name='cashfree_response'),
returnUrl = 'http://127.0.0.1:8000/cashfreeresponse'

APPEND_SLASH = False
path('cashfreeresponse/',views.cashfree_response, name='cashfree_response'),
returnUrl = 'http://127.0.0.1:8000/cashfreeresponse/'

APPEND_SLASH = False
path('cashfreeresponse',views.cashfree_response, name='cashfree_response'),
returnUrl = 'http://127.0.0.1:8000/cashfreeresponse'

For all the above combinations django is redirecting as GET method and POST data was lost.

APPEND_SLASH = True
path('cashfreeresponse/',views.cashfree_response, name='cashfree_response'),
returnUrl = 'http://127.0.0.1:8000/cashfreeresponse'

for this case I am getting below error

You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/cashfreeresponse/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

APPEND_SLASH = True
path('cashfreeresponse',views.cashfree_response, name='cashfree_response'),
returnUrl = 'http://127.0.0.1:8000/cashfreeresponse/'

APPEND_SLASH = False
path('cashfreeresponse',views.cashfree_response, name='cashfree_response'),
returnUrl = 'http://127.0.0.1:8000/cashfreeresponse/'

for these both cases, I am getting csrf verification failed error.

APPEND_SLASH = False
path('cashfreeresponse/',views.cashfree_response, name='cashfree_response'),
returnUrl = 'http://127.0.0.1:8000/cashfreeresponse'

for this case, I got page not found error.


回答1:


@login_required decorator was causing the issue. I think @login_required redirecting the page to see if user logged in (even if user already logged in) then redirecting back to this view. During this process, it is loosing POST data and converting the request method to GET.




回答2:


APPEND_SLASH shouldn't matter, as it only affects URLs that cannot be found, but you are explicitly specifying a URL pattern without a slash. So the issue is with your path definition.

Assuming we are talking about the main, project-level urls.py, the correct setting would be:

path('cashfreeresponse',views.cashfree_response, name='cashfree_response'),
returnUrl = 'http://127.0.0.1:8000/cashfreeresponse'

But my guess is that your path named cashfree_response is defined in an app (e.g. polls/urls.py) and that it is included at a subpath in the project-level (e.g. mysite/urls) like that:

path('polls/', include('polls.urls')),

In that case, the correct returnUrl would be http://127.0.0.1:8000/polls/cashfreeresponse.

Instead of hard-coding the URL, you can use reverse together with request.

from django.urls import reverse
request.build_absolute_uri(reverse('cashfreeresponse')

Note: You might have to use reverse('<appname>:cashfreeresponse') here, e.g. reverse('polls:cashfreeresponse')

Edit: from your update, it seems like you still have a trailing slash in the path definition:

    path('cashfreeresponse/',views.cashfree_response, name='cashfree_response'),

Change that to:

    path('cashfreeresponse',views.cashfree_response, name='cashfree_response'),


来源:https://stackoverflow.com/questions/60582564/django-unable-to-read-post-parameters-sent-by-payment-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!