0

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

enter image description here

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.

sreekanthkura7
  • 115
  • 2
  • 18
  • 1. Have you tried other combinations? For example, urls.py without slash and APPEND_SLASH equals to True. 2. Where do you set returnUrl? In Cashfree? can it be http://127.0.0.1:8000/cashfreeresponse/? I think, as the trackeback says,that Django is redirecting your URL and loosing the POST data while doing it – pyjavo Mar 09 '20 at 22:27
  • @javidazac Yes, I tried all different combinations of APPEND_SLASH, urls.py and returnUrl. I updated the question with that information. I pass returnUrl as a parameter when I makes request to the Cashfree payment gateway. – sreekanthkura7 Mar 10 '20 at 15:00
  • hi @javidazac, do you know answer to this question [here](https://stackoverflow.com/questions/61327185/django-all-auth-create-account-with-email-unique-constraint-failed-display-m) – sreekanthkura7 Apr 30 '20 at 18:34
  • hi @javidazac, I am sorry to ask you again but do you know answer for this question [here](https://stackoverflow.com/questions/62123264/django-how-to-generate-activate-url-that-django-allauth-generates-for-email-con?noredirect=1#comment110158933_62123264). – sreekanthkura7 Jun 13 '20 at 20:39

2 Answers2

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.

sreekanthkura7
  • 115
  • 2
  • 18
0

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'),
Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
  • Hi Daniel, It didn't work. It is still redirecting the POST request as GET after following your suggestion. It was inside app 'pages'. So I used `path('polls/', include('polls.urls'))` and `from django.urls import reverse` and `requestUrl = request.build_absolute_uri(reverse('cashfreeresponse'))` then I passed requestUrl to payment gateway. – sreekanthkura7 Mar 11 '20 at 15:10
  • please post the full urls.py of your project and of the app containing the `cashfree_response` view – Daniel Hepper Mar 11 '20 at 15:11
  • I updated the question with urls.py of the project and the pages app – sreekanthkura7 Mar 11 '20 at 15:20
  • I tried both ways. I got the same result with or without slash. – sreekanthkura7 Mar 11 '20 at 15:31
  • It still must be without slash. `returnUrl` should be `"http://127.0.0.1:8000/pages/cashfreeresponse"`, please verify. – Daniel Hepper Mar 11 '20 at 15:37
  • I tried again now with `path('cashfreeresponse',views.cashfree_response, name='cashfree_response')` and `returnUrl = request.build_absolute_uri(reverse('cashfree_response'))` and I did verify the returnUrl before passing by `print(returnUrl)` and it printed as `http://127.0.0.1:8000/pages/cashfreeresponse` – sreekanthkura7 Mar 11 '20 at 15:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209463/discussion-between-daniel-hepper-and-sreekanthkura7). – Daniel Hepper Mar 11 '20 at 15:46