0

I must submit two forms at once and I don't want javascript. So I think I can post the form to myself, do my work and then post it on to the third-party payment provider. This is to make the order appear in our datastore. The manual says how to submit a form over HTTP POST but I don't know how to direct the user's browser to this page. Is there a way? Instead of posting the form directly to the third party I thought doing something like this:

  def post(self):
     import urllib
     #do my work
     params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
     f = urllib.urlopen("http://www.thirdparty.com/cgi-bin/query", params)
     #direct the user to the reponse page but how?

It will work to submit the form but the user must be take to the page. Is there a way to achieve this?

Thanks for any help

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424

2 Answers2

2

There is something you can do like this:

Get the post form, do stuff on your server, and then return a redirect page that contains a form with same params but action to the remote target.

Then, you can use little js to submit the form automatically, and give something like "Not redirecting? click here" to the user.

So it's not working as what you expected, and it used js too, but I did not find any better way to handle this job. Hope this can help in any way.

Felix Yan
  • 14,841
  • 7
  • 48
  • 61
  • I think it will work. Th e whole thing is submitting a form to a third party like paypal and I need to "intercept" and save the order to my datastore. Thank you for the answer. – Niklas Rosencrantz Jan 11 '12 at 18:55
  • 1
    A common way to do this is to create the order first, then redirect the user to the order summary page (on which he can check/save/print it), and place the standard Paypal button there. – Felix Yan Jan 12 '12 at 00:16
  • Thank you for the help. I just wonder now how I should know if users cancels the order at the redirect page. I could have a batch job that cleans this up but better would be that only orders that actually are placed with the third-party provider is also written to my datastore. – Niklas Rosencrantz Jan 12 '12 at 11:13
  • 1
    So you have to save all your orders' state, at least "unpaid" and "paid" status :) – Felix Yan Jan 12 '12 at 15:13
  • for a further elaboration about this use case please feel free to view my new question where I've gotten further with the implementation http://stackoverflow.com/questions/9075541/buying-many-products-at-once-from-a-webshop Thanks! – Niklas Rosencrantz Jan 31 '12 at 07:25
2

You could use 307 to redirect, which preserves all the POST parameters (you'll need to make sure the POST params your servlet uses are the same as the ones your remote server uses).

self.response.set_status(307)
self.response.headers['Location'] = "http://www.thirdparty.com/cgi-bin/query"

However, not all browsers implement 307 correctly. See here: Response.Redirect with POST instead of Get?

That response is old, so maybe more browsers handle 307 correctly now.

Community
  • 1
  • 1
Moishe Lettvin
  • 8,462
  • 1
  • 26
  • 40