Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
PayPal Integration sample using C#
Posted Date: 21 Jun 2008 Resource Type: Articles Category: Web Applications
|
Posted By: pradeep Member Level: Silver Rating: Points: 25
|
This code sample shows how to integrate Paypal payment gateway services in your ASP.NET web pages.
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 received 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; }
}
}
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|