Paypal is the most popular payment gateway. Here I am describing the procedure to easly integrate your Asp.net e-commerce application with paypal. For this you must have a
Paypal account.
Step 1.
The below should be written on checkout button Click Event.
This code will redirect user to paypal website for payment and send the product and price information to paypal website.
string redirect;
redirect += "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=" + Registered_Paypal_Email_ID;
redirect += "&item_name=" + "Item Name";
redirect += "&amount=" + String.Format("{0:0.00} ", amount);
redirect += "&item_number=" + itemID;
redirect += "¤cy=USD";
//redirect += "&add =1";
redirect += "&return=http://www.yoursite.com/returnurl.aspx";
redirect += "&cancel_return=http://www.yoursite.com/cancelUrl.aspx";
redirect += "¬ify_url=http://www.Yoursite.com/noftify.aspx";
redirect += "&custom=" + OrderID;
Response.Redirect(redirect);
When the payment is recived on your paypal account then paypal will send a notification to your site on "http://www.Yoursite.com/noftify.aspx" page that you have mentioned as
notify_url on above code.
Now on receiving the notification the your application must update your database regarding order confirmation.
now Below is the c# code that you have to write on notify.aspx.cs file's onLoad() event
string requestUriString;
CultureInfo provider = new CultureInfo("en-us");
string strFormValues = Encoding.ASCII.GetString(
this.Request.BinaryRead(this.Request.ContentLength));
requestUriString = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest request =(HttpWebRequest)WebRequest.Create(requestUriString);
// Set values for the request back
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string obj2 = strFormValues + "&cmd=_notify-validate";
request.ContentLength = obj2.Length;
// Write the request back IPN strings
StreamWriter writer =
new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
writer.Write(RuntimeHelpers.GetObjectValue(obj2));
writer.Close();
//send the request, read the response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("utf-8");
StreamReader reader = new StreamReader(responseStream, encoding);
char[] buffer = new char[0x101];
int length = reader.Read(buffer, 0, 0x100);
while (length > 0)
{
// Dumps the 256 characters to a string
string OrderID;
string IPNResponse = new string(buffer, 0, length);
length = reader.Read(buffer, 0, 0x100);
try
{
// getting the total cost of the goods in
// cart for an identifier
// of the request stored in the "custom" variable
if (String.Compare(IPNResponse, "VERIFIED", false) == 0)
{
OrderID = this.Request["custom"].ToString();
if (String.Compare(OrderID, "", false) == 0)
{
Response.Write("Invalid Order ID");
return;
}
// Below on this block you can write the code to Update the status of your order in database for orderID
//
}
}
catch (Exception exception)
{
//Response.Write(exception.ToString());
return;
}
}
}
0 comments:
Post a Comment