Post Data to PayPal from Code Behind


Y

Yourself

Hi, I'm trying to post data to PayPal for a shopping cart, basically I want
to replicate a form like the following, but create the variables dynamically
from code behind:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business"
value="(e-mail address removed)">
<input type="hidden" name="item_name"
value="Aggregated items">
<input type="hidden" name="amount" value="3.00">
<input type="submit" value="PayPal">
</form>

How can I create the hidden input variables in a code behind file and have
the data posted to https://www.paypal.com/cgi-bin/webscr and the user
redirected to that site?

I'm using the .NET 2 framework, but I still can't figure this out.

The only way I've been able to get somewhere is to have a PayPalForm.aspx
file which is displayed in an iframe (inside a checkout user control), and
has the following code:

<form name="paypalform" id="paypalform"
action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<asp:placeHolder ID="pchMain" runat="server"></asp:placeHolder>
</form>

Then, in the PayPalForm.aspx.cs file, I can add the hidden input variables
to the placeholder.

The problem with this approach though, is I need to access other controls (a
radio button list) that are on the checkout.ascx file which the iframe sits
on.

So I have:

Checkout.ascx
Radio Button List
Iframe
PayPalForm.aspx

and I need to access the radio button list from within PayPalForm.aspx.cs

Does that make sense??!!
 
Ad

Advertisements

S

Superman

Yourself,

You're on the right track. Rather than making a post to the paypal
script, you should put all your stuff in a querystring, redirect to
that URL. There are a few ways to integrate into paypal, you can either
use the my website to their website and back to mine (standard), or
call their web services from your website (pro/premium).

My guess is that you just need to package all of that stuff into the
query string and redirect the browser to the url.

ex:

https://www.paypal.com/cgi-bin/webscr?cmd=_cart&[email protected]...

Don't forget to check out the paypal sandbox for testing.

http://www.sandbox.paypal.com

Good luck!

~Brenton MCSD.NET
 
Y

Yourself

Thanks for the tip, I had thought about using the querystring to pass the
data, but I was hoping to avoid it, as it makes it even easier to change the
prices etc.

Am I correct in thinking that there is no possible way of posting data to an
external URL from code behind AND redirecting the user?

If so, I think I will stick to my iframe technique, and work out a way of
getting data between the PayPalForm.aspx and the Checkout.ascx - possibly
using ViewState?
 
Ad

Advertisements

V

vMike

Yourself said:
Hi, I'm trying to post data to PayPal for a shopping cart, basically I want
to replicate a form like the following, but create the variables dynamically
from code behind:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business"
value="(e-mail address removed)">
<input type="hidden" name="item_name"
value="Aggregated items">
<input type="hidden" name="amount" value="3.00">
<input type="submit" value="PayPal">
</form>

How can I create the hidden input variables in a code behind file and have
the data posted to https://www.paypal.com/cgi-bin/webscr and the user
redirected to that site?

I'm using the .NET 2 framework, but I still can't figure this out.

The only way I've been able to get somewhere is to have a PayPalForm.aspx
file which is displayed in an iframe (inside a checkout user control), and
has the following code:

<form name="paypalform" id="paypalform"
action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<asp:placeHolder ID="pchMain" runat="server"></asp:placeHolder>
</form>

Then, in the PayPalForm.aspx.cs file, I can add the hidden input variables
to the placeholder.

The problem with this approach though, is I need to access other controls (a
radio button list) that are on the checkout.ascx file which the iframe sits
on.

So I have:

Checkout.ascx
Radio Button List
Iframe
PayPalForm.aspx

and I need to access the radio button list from within PayPalForm.aspx.cs

Does that make sense??!!
Here is how I do this.

I have a form which has the static data. I have a placeholder in the form. I
build the non-static form data using stringbuilder and add is using

Dim ctrl as control = Page.ParseControl(PayPalFormDataString)
plc1.controls.add(ctrl).

I have the form automatically submit when it is called using javascript. (I
also have a button appear for those that have it disabled).

Couple of caveats... you want to make sure the page is only called by a
valid checkout button click and not from someone just going to that page.
You also need to handle the broken back button issue .. I use a redirect,
and finally you don't want the page to cache locally. I use <%@ OutputCache
Location="None" VaryByParam="None" %>

I also have a javascript waiting progress bar to provide some motion while
the post is processing.

An alternative to all of this is paypal has some code to handle a lot of
this but I choose not to use.


Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top