Pondering... I recently started work on my first website to integrate with PayPal. The client needed it done relatively quickly. What started out for me as a mental picture of a products Web form with a "Buy Now" button, somehow turned into a full-blown e-commerce application complete with custom shopping cart. Talk about feature creep! And most of it was my own fault because I failed to anticipate the minimal requirements for the job. I'm still in the thick of it and have just implemented an admin back-end for the merchant to add products, complete with images, to the database. Then, I had to create an HttpHandler to stream the images...

They do say it's the simple things that get you stumped. For all of you ASP.NET developers out there who have yet to integrate a site with PayPal, just wait until you finally get to add that button to your form to pass the transaction details over to PayPal... In short, it won't work! The button HTML from the PayPal site is embedded in a form tag. You can only have one form on an ASP.NET page and ASP.NET provides its own. If you have a master page, then the form tag is in there and it is applied to every page in the site when they are merged with the master at runtime.

Thinking Irishman PayPal support does not offer a practical solution. They may try to get you to download their ASP.NET SDK which is 1.1 and uses Web Services. Most people have failed to get it to work with 2.0. Then they may tell you to put the form tag "outside" the main tag or on a separate HTML page, etc. I have seen endless hacks, most of which were too stupid to even consider; IFrames anyone?!!

I trudged through the forums and saw that ASP.NET developers have been asking how to get around this for the last three years or so. PayPal refuses to acknowledge the problem and seem more inclined to offer support for the PHP community. There is something radically wrong with this mindset from a business point of view.  Can PayPal not afford to pay some contractors to go in and develop an ASP.NET 2.0 SDK that will work with both NVP and Web Services? Nothing like speed to kill. Then PayPal had the temerity to invite me to complete a survey on how good I found their support service...

The Light Goes On So I turned to Google. I spent days concocting search strings that would bring that elusive nugget I needed to solve the problem. I thought I had found it when I came across the nested master page hack - keep the outer master page stripped of any form tag and then just use it for the page with the PayPal button. It would probably work, but if you don't get a code smell from that one, you may need to get your sinuses reamed out. The search continued. You know you're desperate when you start entering your grannie's middle name in the search query string :-O

Persistence finally paid off. I found an elegant solution on Jeremy Schneider's blog that consists of a custom HtmlForm class that can have the form tag rendering toggled on and off. The class is called GhostForm and has a property, RenderFormTag. When RenderFormTag is set to false, it doesn't render the opening or closing tags, but does render all of the contents. Reference the custom GhostForm class and in the code-behind of the form on which you are placing the button, place the following in the Page_Load to disable the master page form tag:

public partial class Products : System.Web.UI.Page
 {
     protected void Page_Load(object sender, EventArgs e)
     {
         GhostForm mainForm = new GhostForm();
         mainForm.RenderFormTag = false;
         .....     
     }
         // Send your data to PayPal :-)
     .....
 }



Comments (10) -

Kim
Kim
10/29/2008 2:22:41 AM #

I always was pro America now never more.
Paypal robbed me 41 euros .
Now I understand all whose hate America.

agrace
agrace United States
10/29/2008 10:55:00 AM #

Hey Kim,

I wouldn't know... I'm Irish :-|

Do you want us to pass the hat around?

Anthony Wink

Dana
Dana United States
11/1/2008 3:45:42 PM #

I'm wondering which PayPal product were you successful in using? I have an ASP application that I have all the buyer info, product cost, shipping and taxes already calculated, and I just want PayPal to handle the credit card or PP charge processing. I naively thought it would be a simple call to PP passing these items as arguments, but no such luck. I would like to use Website Payments Standard - it seems like it would work for me, but I'm not sure... Does anyone have any suggestions, and would this "elegant solution" you discuss really work? Thanks for any help.

agrace
agrace United States
11/1/2008 4:24:52 PM #

Hi Dana,

I simply used the Ghost Form class to get around the ASPNET/PayPal form issue. I wrote some custom code with a cart and hooked up to PayPal in accordance with the Website Payments Standard. Check my other post here:
www.codersbarn.com/.../...tton-with-ASPNET-20.aspx

Anthony Smile

scott
scott United States
12/3/2008 6:08:12 AM #

Jeremy's site seems to be down and I'm in need of the ghost class. Where can I find it?

Ryan
Ryan United States
3/14/2012 10:48:21 PM #

I had this problem, but figured out a way around it.  In the Master Page's Page_Load() function, I pulled the current page without the extension, then checked the variable for the page which the PayPal button was on:


        string currPage = System.IO.Path.GetFileNameWithoutExtension(Request.Path);
        if (currPage == "whatever")
        {
            form1.Action = "https://www.paypal.com/cgi-bin/webscr";;
            form1.Method = "post";
        }
        else
        {
            form1.Action = "";
            form1.Method = "";
        }

This way, the Master Page's form will act as the PayPal button's form on this page (or any pages you wish).  Hope this helps!

Armaghan
Armaghan United States
3/31/2012 5:10:43 PM #

The easiest way to do this is to set the PostbackUrl to Paypal's on your button. No need to use the Ghost form or anything else.

admin
admin United States
4/1/2012 10:09:42 PM #

Armaghan,

Have you actually tried it? There is only one form allowed in an ASP.NET form...

Aleksander
Aleksander Bulgaria
5/3/2012 1:38:09 AM #

The next 2 days I will be doing just that. I remember that I have used different work arounds for having more than 1 form, but this example worth trying it Smile

Mani
Mani United States
5/28/2012 8:38:18 PM #

@Ryan,

I tried your suggestion and get a bunch of build errors in vs2010 (         \Visual Studio 2010\WebSites\WebSite1\Site.master.cs(16,13): error CS0103: The name 'form1' does not exist in the current context). Question is how to reference the specific "paypal" form that I am interested in?

-Mani

Pingbacks and trackbacks (2)+