locked
Post to PayPal from content page -- Solution for me RRS feed

  • Question

  • User139366633 posted

    Heloo all,

    I have received alot of help from this site, and just wanted to post my solution to what seems to be an all too common problem. Hopefully this helps others.

    My problem was trying to work around the nested form issue that seems to derive from using master pages. My solution is not very elegant (at all!) or utilize fancy coding, but it has worked for my simple needs. Keep in mind that my needs were simple:

    1. I am using my own cart and simply needed to send a single item_name to PayPal which was the Order Number that my cart creates.

    2. I am only sending a single amount to PayPal, and am not using the PayPal site to calculate shipping, discounts etc...

    The first thing I did was to place the following input fields on my .aspx page WITHOUT the <form> tags that PayPal usually deems are necessary.

     

    <input type="hidden" name="cmd" value="_xclick" />

    <input type="hidden" name="business" value="youremail@email.com" />

    <input type="hidden" name="item_name" value='<%=OrderNumber.GetOrderNumber()%>'/>  

    <input type="hidden" name="amount" value='<%=PayPalCartAmount()%>' /> 

    Note that I am populating the values of the item_name and amount fields from code behind using functions similiar to below. (GetOrderNumber function resides in an external class while PayPalCartAmount resides in .aspx code behind)

    Public Function PayPalCartAmount() As String
    code for blah,blah,blah
            Return blah,blah,blah
        End Function


     

    Finally I added the following button to the page to submit it all:

    <asp:ImageButton ID="submit" runat="server" ImageUrl="/images/checkout-paypal.gif"  AlternateText="Purchase Using Paypal" PostBackUrl="https://www.paypal.com/us/cgi-bin/webscr" />

    I assume that this works because the <form> tags do in fact still exist when the master page and content page is rendered together upon page load? If someone with more experience could verify this I would appreciate it.

    Thanks again and I hope this saves someone unnecessary headaches.

    Saturday, September 11, 2010 1:04 PM

All replies

  • User-815382200 posted

    Hi,

    I had similar issue and I've solved it by using a GET method of HTTP (so passing parameters in a querystring). I was told to do so by paypal staff on www.x.com (paypal support page).

    Since ASP .NET is built with single form architecture, you can't use default paypal buttons and you can't use the above code to make a postback to another server, because of security reasons.

    One workaround is to use second form below the asp .net form (don't use the runat='server' in it!). That will work with default paypal buttons but the downside will be that you will loose all the formatting, themes and server-side code on this page. Your button will be below everything else on the page. For me it wasn't accpetable so I went with query string method.

    You do it like this:

    markup:

    <asp:Literal ID="PaypalLink" runat="server" />

    codebehind:

    PaypalLink.Text = string.Format("<a href='https://www.paypal.com/us/cgi-bin/websct?cmd=_xclick&business={0}&item_name={1}&amount={2}' target='_self'>Go to Paypal</a>", param1,param2,param3); //set those params to whatever you need

    Alternatively, you can create redirecting page, make a postback to that page and then use javascript to redirect to paypal, but using javascript isn't reliable method and require additional request from client to server. 

    There is no other simple way to walk this around.

    Also, you don't have to worry about the security with using querystring as in the original method all your variables are also visible in page source so nothing changes.

     

    Hope it helps...

    Saturday, September 11, 2010 4:31 PM