Home » ASP.Net

ASP. Net : Adding a Paypal Buy Now in code behind

31. August 2009 by jdelpay 1 Comments

Problem:

You can't have HTML form tags inside aspx form tags. The problem is that ASP.NET pages are forms themselves, surrounded by a single <form Runat="Server"> tag that automatically posts back to itself. Therefore, you cannot embed an HTML form inside this server form.
Shown below is typical code for a PayPal "BuyNow" button.

 

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="dradams@dradamsweb.com">
<input type="hidden" name="item_name" value="ASP.NET 2.0 Tutorial">
<input type="hidden" name="item_number" value="WDS03">
<input type="hidden" name="amount" value="52.00">
<input type="hidden" name="return" 
  value="http://www.dradamsweb.com/default.aspx">
<input type="hidden" name="cancel_return" 
  value="http://www.dradamsweb.com/default.aspx">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="image" border="0" name="submit"
  src="https://www.paypal.com/en_US/i/btn/x-click-but01.gif" 
  alt="Make payments with PayPal - it's fast, free and secure!">
</form>


Solution

On your default.aspx page add a button from the toolbox
Double click the button you just added to open the default.aspx.cs page (code Behind)
Add and customize the code below

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; 


public partial class cc_Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
const string Server_URL = "https://www.paypal.com/cgi-bin/webscr?";
const string return_URL = "http://www.PageWhenOk/default.aspx";
const string cancelreturn_URL = "http://www.PageWhenCancel.com/cc.fail.aspx"; 


string cmd = "_xclick";
string business ="your business email";
string item_name = "Name of the item";
double baseamt = 49.00;//amount
int add = 1;
double amount = 49.00 ;
double shipping = 0.00 ;
double handling = 0.00;
int no_shipping = 1;
int no_note= 1;
string currency_code = "EUR"; //Replace with country Currency code
string lc = "FR";
string bn = "PP-BuyNowBF";
string basedes = "ILCV 49.00" ;

string redirect="";
redirect+= Server_URL;
redirect += "cmd=" + cmd;
redirect += "&business=" + business;
redirect += "&item_name=" + item_name;
redirect += "&baseamt=" + baseamt;
redirect += "&add=" + add;
redirect += "&amount=" + amount;
redirect += "&shipping=" + shipping;
redirect += "&handling=" + handling;
redirect += "&no_shipping=" + no_shipping;
redirect += "&no_note=" + no_note;
redirect += "&currency_code=" + currency_code;
redirect += "&lc=" + lc;
redirect += "&bn=" + bn;
redirect += "&basedes=" + basedes;
redirect += "&return=" + return_URL;
redirect += "&cancel_return" + cancelreturn_URL;
//Redirect to the payment page
Response.Redirect(redirect);

}
}

 


Voila! any questions, feel free to email me. jaydee_777@hotmail.com

Comments

Alem
Sweden Alem said:

Dude you have  been saved  to me 1  week to implement  this  problem , thank you very much


Comments are closed