Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

Paypal button doesn't work with Masterpage

I have a paypal buy-now-button on the web, written in asp.net 2.0.  The buy-now-button works great if I am not using masterpage, once I put the button in the masterpage it doesn't work.  It just postback but does nothing.  Below is my code, please help.

In aspx:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<% =PayPalForm %>
....
<asp:Button ID="button1" runat="server" Text="Buy Now" />
</asp:Content>

In aspx.vb:

Protected Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
            Dim strPayPalForm As New StringBuilder
            strPayPalForm.Append("<form  action=""https://www.sandbox.paypal.com/cgi-bin/webscr"" method=""post"">")
            strPayPalForm.Append("<input type=""hidden"" name=""cmd"" value=""_xclick"">")
            strPayPalForm.Append("<input type=""hidden"" name=""business"" value=""info@handywise.com"">")
            strPayPalForm.Append("<input type=""hidden"" name=""item_name"" value=""Add Funds"">")
             strPayPalForm.Append("<input type=""hidden"" name=""amount"" value=""80"">")
            strPayPalForm.Append("<input type=""hidden"" name=""no_shipping"" value=""1"">")
            strPayPalForm.Append("<input type=""hidden"" name=""return"" value="""">")
            'strPayPalForm.Append("<input type=""hidden"" name=""rm"" value=""2"">")
            strPayPalForm.Append("<input type=""hidden"" name=""cancel_return"" value="""">")
            strPayPalForm.Append("<input type=""hidden"" name=""no_note"" value=""1"">")
            strPayPalForm.Append("<input type=""hidden"" name=""currency_code"" value=""USD"">")
            strPayPalForm.Append("<input type=""hidden"" name=""lc"" value=""CA"">")
            strPayPalForm.Append("<input type=""hidden"" name=""bn"" value=""PP-BuyNowBF"">")
            'strPayPalForm.Append("<input type=""hidden"" name=""shopping_url"" value=""http://www.myweb.com/Default.aspx"">")
            strPayPalForm.Append("<input type=""hidden"" name=""notify_url"" value=""http://www.myweb.com/ipn.aspx"">")
            strPayPalForm.Append("</form>")
            PayPalForm = strPayPalForm.ToString

            Dim jscript As String
            jscript = "<script>document.forms[0].submit();</script>"
            Page.ClientScript.RegisterClientScriptBlock(GetType(Button), "KeyClientBlock", jscript)
0
cmleung2
Asked:
cmleung2
  • 3
  • 2
1 Solution
 
RejojohnyCommented:
instead of writing all that code to write hidden fields in the click event, you might as well add all of those hidden fields during design time ..

do you have other form under
<% =PayPalForm %>
....here ??

anyway, give this a try
strPayPalForm.Append("<form id='frmPayPal' action=""https://www.sandbox.paypal.com/cgi-bin/webscr"" method=""post"">")

give an id to the form and then

jscript = "<script>document.forms['frmPayPal'].submit();</script>"

Rejo
0
 
timandkidsCommented:
I could be wrong, but I believe you are trying to place a <form> (the PayPal form) inside a <form> (the ASP.NET form), which isn't going to work correctly. You can have multiple forms on a page, but not nested forms.

You could handle this in one of three ways that I can think of...

Place all the variables into a URL string, as querystring variables, instead of as form variables. I believe PayPal lets you do this. In other words, when the button is clicked in your VB code, you would response.redirect to the paypal page with the variables appended to the querystring.

Or, in Javascript when the button is clicked, you could alter the ACTION of the ASP.NET <form> to go to the PayPal location instead of posting back to itself, which is the default behavior of every ASP.NET form, and then submit() it. You may also need to alter the __VIEWSTATE variable of the form, kind of like:

<script>
function ButtonClick()
{
 document.WhateverTheNameOfTheASPNETFormIs.action = "https://www.sandbox.paypal.com/cgi-bin/webscr":
 document.WhateverTheNameOfTheASPNETFormIs.__VIEWSTATE = "":
 document.WhateverTheNameOfTheASPNETFormIs.submit();
}
</script>

Or, you could take over the response code and print a form to the screen and have it automatically submit onload, kind of like:

Response.Clear()
Response.Write("<form action=""https://www.sandbox.paypal.com/cgi-bin/webscr"" method=""post"" name=""PayPalForm"">")
Response.Write("<input type=""hidden"" name=""cmd"" value=""_xclick"">")
etc...
Response.Write("</form>")
Response.Write("<script>document.PayPalForm.submit();</script>")
Response.End()
0
 
timandkidsCommented:
Forgive me, but this line in my post above:
document.WhateverTheNameOfTheASPNETFormIs.action = "https://www.sandbox.paypal.com/cgi-bin/webscr";

...may need to be:
document.WhateverTheNameOfTheASPNETFormIs.action.value = "https://www.sandbox.paypal.com/cgi-bin/webscr";

...and the same for the .__VIEWSTATE line. Just add ".value" in there at the end.

Also, if you want the third option I mention above, you would put all that code into your VB button_click subroutine.

Finally, if your values that you are passing to PayPal are always the same (as they appear to be in your VB above), you could go with the first option I mentioned in the post above as just a static anchor tag:

<a href="https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&amount=80...etc...">Click Here to Give Us Money</a>

...then you wouldn't need any VB to go along with it.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
cmleung2Author Commented:
I am not good with javascript, what is the WhateverTheNameOfThe ASPNETForms, do I need to assign a name first for the window?  I want to use the current window, would that be form[0]?  Thanks
0
 
cmleung2Author Commented:
using forms[0] works. Thanks.
0
 
timandkidsCommented:
I'm glad that worked. You could have also pulled up the form in IE and right-clicked and "view source" to see the NAME property of the <form>. ASP.NET will dynamically generate that name, I believe, but I am pretty sure it will always be the same. But if forms[0] works, then great.

Keep in mind the way you chose to do it, in altering the action of the ASP.NET form, EVERYTHING on the page will be posted to PayPal, not just "PayPal form fields". In other words, if you have a number of different form fields on the page and a number of different submit buttons for different purposes, EVERY SINGLE ONE OF THOSE FORM FIELDS AND THEIR VALUES will be posted to PayPal.

If the only form info you have on the page is PayPal stuff, then this is nothing to worry about. If, on the other hand, you have other form fields on the page, depending on what they are, it could conceivably be a big security compromise (such as if you are gathering people's SSNs for some reason -- you don't want that info to be posted to PayPal). If there is anything on that page that you DO NOT WANT PayPal TO KNOW ABOUT, then you should probably go with a different method, like the URL string or the Response.Write() idea, where you can better control exactly what gets posted to PayPal.

Just something to keep in mind.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now