Post to PayPal with urlllib [newbie question]

7 views
Skip to first unread message

snew...@gmail.com

unread,
Aug 23, 2006, 2:47:20 PM8/23/06
to Django users
I've got a product page that submits to the PayPal shopping cart, and I
have form controls on that page with values that need to modify the
price. (discount codes)

In my research, it appears that I need to post the page to a view,
check the discount code, and then send the data via post to the Paypal
Page using urllib/urllib2.

I created a view called "checkout", and made it the action of my the
form on my product page. (using target="paypal" to pop it into a new
window)

So far, so good, but I'm not sure how to proceed. I should be able to
capture and modify the post data, then urlencode the post values using
urllib, but I cannot figure out how to post the form data AND redirect
the page to the paypal URL in one shot.

Also, is the view the right place for this logic to occur? Seems like I
should encapsulate it into a function to keep my views.py nice clean.

I really appreciate any thoughts or suggestions, even if it's to tell
me that I'm way off base in my logic!

Jeremy Dunck

unread,
Aug 23, 2006, 3:07:22 PM8/23/06
to django...@googlegroups.com
On 8/23/06, snew...@gmail.com <snew...@gmail.com> wrote:
> So far, so good, but I'm not sure how to proceed. I should be able to
> capture and modify the post data, then urlencode the post values using
> urllib, but I cannot figure out how to post the form data AND redirect
> the page to the paypal URL in one shot.

You can't redirect and have the browser do a post:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2
"
If the 301 status code is received in response to a request other
than GET or HEAD, the user agent MUST NOT automatically redirect the
request unless it can be confirmed by the user, since this might
change the conditions under which the request was issued.

Note: When automatically redirecting a POST request after
receiving a 301 status code, some existing HTTP/1.0 user agents
will erroneously change it into a GET request.

"

Because of that, you'll probably end up splitting into two requests,
avoiding the problem you're having.

> Also, is the view the right place for this logic to occur? Seems like I
> should encapsulate it into a function to keep my views.py nice clean.

I'd argue it's a model function.

Product.get_price(discount_code=None) or something like that.

But that doesn't give you a solution by itself. :)

Derek Hoy

unread,
Aug 23, 2006, 7:41:25 PM8/23/06
to django...@googlegroups.com


> ... I should be able to


> capture and modify the post data, then urlencode the post values using
> urllib, but I cannot figure out how to post the form data AND redirect
> the page to the paypal URL in one shot.

I'm just about to do the same thing. The only way I could see to do
this is to have a view for the data entry, then once it's
complete/valid, redirect to a confirmation page, which shows a summary
of the order, with all the necessary data in hidden fields. Clicking
'continue' then submits the form for payment.

This is new territory for me, but there are folks round here who've
done this stuff before.

--
Derek

snew...@gmail.com

unread,
Aug 24, 2006, 3:49:51 PM8/24/06
to Django users
I thought about trying to post all the data to a form in the view page,
then automatically submit the form using JavaScript, but it seems like
such a cludgy solution.

Jeremy Dunck

unread,
Aug 24, 2006, 5:42:56 PM8/24/06
to django...@googlegroups.com

It's also insecure because you'd be giving the client the ability to
alter the price before forwarding to PayPal.

I'll be your customer if you want to do that. ;-)

Derek Hoy

unread,
Aug 24, 2006, 7:10:11 PM8/24/06
to django...@googlegroups.com

Any kind of form-based submission will have the same problem- you have
to do some validation in a callback anyway, don't you?

I'm using Worldpay, and doing:
- view/form to get choices and validate à la Django
- redirect to simple confirm form
- submit to Worldpay with POST data in hidden fields
- get callback, and do validation
- notify administration, save order details etc.
- return confirmation message to Worldpay for them to display

I haven't done this before, so I'd be happy to take advice.

--
Derek

Jeremy Dunck

unread,
Aug 24, 2006, 7:18:44 PM8/24/06
to django...@googlegroups.com
On 8/24/06, Derek Hoy <dere...@gmail.com> wrote:
> I'm using Worldpay, and doing:
> - view/form to get choices and validate à la Django
> - redirect to simple confirm form
> - submit to Worldpay with POST data in hidden fields
> - get callback, and do validation
> - notify administration, save order details etc.
> - return confirmation message to Worldpay for them to display

In that situation, I guess you just wouldn't ship the order if the
customer underpays (by tweaking the payment amount), right?

Derek Hoy

unread,
Aug 24, 2006, 7:58:12 PM8/24/06
to django...@googlegroups.com
On 8/25/06, Jeremy Dunck <jdu...@gmail.com> wrote:
>
> In that situation, I guess you just wouldn't ship the order if the
> customer underpays (by tweaking the payment amount), right?

Unless it was you :)
--
Derek

Reply all
Reply to author
Forward
0 new messages