Click here to Skip to main content
12,453,873 members (80,593 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi I am really struggling with my code to add paypal sandbox to my page.

So far I have managed to get my Checkout button to direct me to the paypal page where payment occurs.

How ever I have tried to modify my add to cart button to add to the paypal cart without any success what so ever.

Here is my HTML:

 <asp:DataList ID="dlProducts" runat="server" BackColor="White" 
        BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
        GridLines="Both" RepeatColumns="3" 
       >
        <ItemTemplate>
            Product Name:
            <asp:Label ID="lblProductNane" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
            <br />
            <asp:Image ID="Image1" runat="server" GenerateEmptyAlternateText="True" 
                Height="100px" ImageUrl='<%# "~/ProductImages/"+Eval("ImageName") %>' 
                Width="100px" />

Here is my code behind:

    protected void btnBasket_Click(object sender, System.EventArgs e)
    {
        Response.Write(PaypalPostForm());
 
        PayPalPostScript(Page);
    }
 
     public string PaypalPostForm()
     {
        string PostURL = "http://www.paypal.com/cgi-bin/webscr";
        string cmd = "_cart";
        string Upload = "1";
        string BusinessEmail = "";
        string Currency = "GBP";
        string method = "post";
        string shipAmount = "4.95";
 
        StringBuilder ppForm = new StringBuilder();
        ppForm.AppendFormat("&lt;Form name='frmPP' id='frmPP' action='{0}' method='{1}'>", PostURL, method);
        ppForm.AppendFormat("&lt;input type='hidden' name='shipping' value='{0}'>", shipAmount);
        ppForm.AppendFormat("&lt;input type='hidden' name='cmd' value='{0}'>", cmd);
        ppForm.AppendFormat("&lt;input type='hidden' name='upload' value='{0}'>", Upload);
        ppForm.AppendFormat("&lt;input type='hidden' name='business' value='{0}'>", BusinessEmail);
        ppForm.AppendFormat("&lt;input type='hidden' name='currency_code' value='{0}'>", Currency);
        ppForm.AppendFormat("&lt;input type='hidden' name='item_number_1' value='{0}'>", 1);
        for(int i = 0; i<this.dlProducts.Items.Count; i++)
        {
            Label lbldes = this.dlProducts.Items[i].FindControl("lblProductNane") as Label;
            string Description = (lbldes.Text).ToString();
            Label lblPri = this.dlProducts.Items[i].FindControl("lblListPrice") as Label;
            string Price = (lblPri.Text).ToString();
           ppForm.AppendFormat("&lt;input type='hidden' name='item_name_1' value='{0}'>", Description);
            ppForm.AppendFormat("&lt;input type='hidden' name='amount_1' value='{0}'>", Price);
        }
        ppForm.AppendFormat("&lt;input type='hidden' name='quantity_1' value='{0}'>", 1);
        ppForm.Append("&lt;/form>");
        return ToString();
 
    }
 
     private void PayPalPostScript(System.Web.UI.Page Page)
     {
         //This registers Javascript to the page which is used to post the PayPal Form details
         StringBuilder strScript = new StringBuilder();
         strScript.Append("&lt;script language='javascript'>");
         strScript.Append("var ctlForm = document.getElementById('frmPP');");
         strScript.Append("ctlForm.submit();");
         strScript.Append("&lt;/script>");
         ClientScript.RegisterClientScriptBlock(this.GetType(), "PPSubmit", strScript.ToString());
     }
 
};
Posted 8-Feb-13 5:27am
Comments
Hasham Ahmad 8-Feb-13 11:55am
   
where is the problem occurs?
Member 9599975 8-Feb-13 12:04pm
   
Hi when i press checkout on the form it correctly directs me to paypal but to an empty basket.

I would expect "add to cart" to send the details of the cart item selected but doesnt.
   
And?!
—SA

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

From you question I assume that you are trying to connect to Sandbox. So, please use SandBox URL. Namely, this, [action="https://www.paypal.com/cgi-bin/webscr"], is for Paypal real account.

If you are going to test your website against the Sandbox, change it as:
[action="https://www.sandbox.paypal.com/cgi-bin/webscr"].
  Permalink  
Comments
Member 9599975 8-Feb-13 17:03pm
   
Thanks but this still hasn't worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100