Django:无法读取付款网关发送的POST参数 [英] Django: unable to read POST parameters sent by payment gateway

查看:164
本文介绍了Django:无法读取付款网关发送的POST参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理付款后,我无法读取付款网关发送的POST参数.支付网关通过POST请求使用某些参数重定向到returnUrl(我在处理支付之前将其传递给支付网关).

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.

在url.py

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

在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)显示为GET,但也假定POST方法也显示为print(request.GET.get('cf_subReferenceId'))print(request.POST.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.

但是看起来支付网关将参数作为POST方法发送.当我将returnUrl用作http://127.0.0.1:8000/cashfreeresponse而不是http://127.0.0.1:8000/cashfreeresponse/(第一个中缺少'/')时,我得到的错误是

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.

但是我在Google chrome页面中看到付款网关通过POST请求发送参数.下图显示了付款网关通过POST请求发送的参数

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

如果我将urls.py更改为

If I change urls.py to

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

在settings.py中

in settings.py to

APPEND_SLASH = False

returnUrlhttp://127.0.0.1:8000/cashfreeresponse,那么我没有收到任何错误,但仍然没有收到任何参数.如何读取通过POST请求付款网关发送的那些参数? 此处是付款网关的文档.

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.

更新:

我尝试了APPEND_SLASH,urls.py和returnUrl的所有不同组合.在任何情况下都没有用.以下是每种组合遇到的问题类型.

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'

对于上述所有组合,django都将作为GET方法重定向,而POST数据将丢失.

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

您通过POST调用了此URL,但该URL并非以斜杠结尾,并且已设置APPEND_SLASH. Django在维护POST数据时无法重定向到斜杠URL.更改表单,使其指向127.0.0.1:8000/cashfreeresponse/(请注意末尾的斜杠),或在Django设置中将APPEND_SLASH = False设置为

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/'

对于这两种情况,我都遇到了csrf验证失败的错误.

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.

推荐答案

@login_required装饰器引起了该问题.我认为@login_required重定向页面以查看用户是否登录(即使用户已经登录),然后重定向回该视图.在此过程中,它将丢失POST数据并将请求方法转换为GET.

@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.

这篇关于Django:无法读取付款网关发送的POST参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆