Skip to content


PayPal Integration with IPN

Most of the e-Commerce and commercial sites are using PayPal for payments. PayPal
provides various payments methods for e-commerce and commercial
websites like Express checkout, Website Payments , email payments,
Virtual Terminals etc .

Here, I have focused on only website payments (Demo),

paypal

How

Step 1. Creating request page

Here is simple request page, which sends values to PayPal like business email , item details, amount

<form action="https://www.PayPal.com/cgi-bin/webscr"  method="post">
<input type="hidden" name="cmd"  value="_xclick">
<input type="hidden" name="business"  value="you@youremail.com">
<input type="hidden" name="item_name"  value="Item Name">

<input type="hidden" name="currency_code"  value="USD">
<input type="hidden" name="amount"  value="10.00">
<input type="hidden" name="notify_url" value="http://yourwebsite/IPNProcess.aspx" />
<input type="hidden" name="return" value="http://yourwebsite/ThankYou.aspx" />
<input type="hidden" name="cancel_return" value="http://yourwebsite/Cancel.aspx" />
<input type="submit"  value="submit"  >
</form>

HTML Variables

  • business: PayPal ID or an email address of merchant.
  • item_name: Name of item
  • currency_code :The currency of prices. The default is USD.
  • amount : total amount of item
  • notify_url : The URL to which PayPal posts information about the transaction, in the form of Instant Payment Notification messages.
  • return : The URL to which the payer’s browser is redirected after completing the payment
  • cancel_return : A URL to which the payer’s browser is redirected if payment is cancelled

Information of all hidden fields and its details are available here and here

Step 2. Validate purchase confirmation using IPN

When buyer clicks on Pay Now button from PayPal order detail form, a encrypted notification is sent to Merchant’s Website for confirmation.

Instant Payment Notification (IPN) is PayPal’s interface for
handling real-time purchase confirmation and server-to-server
communications. IPN delivers immediate notification and confirmation of
PayPal payments you receive and provides status and additional data on pending, cancelled, or failed transactions.

IPNOverview

PayPal sends various information with notification (see Table 1), we can use
Request.Form[""] to receive values of parameters as follows,

string PaymentStatus = HttpContext.Current.Request.Form["payment_status"];
string  PaymentType = HttpContext.Current.Request.Form["payment_type"];
string  PendingReason = HttpContext.Current.Request.Form["pending_reason"];
string TXN_ID = HttpContext.Current.Request.Form["txn_id"];
string TXN_Type = HttpContext.Current.Request.Form["txn_type"];

//...

Following function will create web request and send to PayPal to receive status of payment (VERIFIED/ INVALID).

public void MakeHttpPost()
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.<;b style=”color: black; background-color: rgb(160, 255, 255);">paypal.com/cgi-bin/webscr");
req.Method = "POST";
req.ContentLength = this.RequestLength.Length + 21;
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = HttpContext.Current.Request.BinaryRead (HttpContext.Current.Request.ContentLength);
string RequestLength = Encoding.ASCII.GetString(param);
RequestLength += "&cmd=_notify-validate";
req.ContentLength = this.RequestLength.Length;

StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(this.RequestLength);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string Response = streamIn.ReadToEnd();
streamIn.Close();
}

Following table shows list of parameters with notification,

Parameter Description
txn_id Unique transaction number
payment_date Payment date in the “18:30:30 Jan 1, 2000 PST” format
payer_email buyer’s e-mail
business seller’s e-mail
payer_id Unique identifier of the buyer. Those who take part in payments performed with the help of PayPal are identified by an e-mail address, but taking into consideration the possibility to change the e-mail, payer_id should be used for the buyer’s identification.
item_number Item identifier
item_name Item name
txn_type Transaction type. Possible values are:

web_accept“: The payment was performed by clicking the “Buy Now” button.

cart“: The payment was performed by using the built-in PayPal cart.

send_money“: The payment was performed by using the “Send money” function.

reversal“: Money was returned to the buyer on his initiative.

payment_status Payment state. Possible values are:

Completed“: Transaction was successfully performed, and money is transferred to the seller’s account. If txn_type=”reversal”, the money is returned to the buyer’s account.

Pending“: Payment was delayed. The delay reason is determined in the pending_reason variable. After the payment is complete, PayPal will send another one notification.

Failed“: Payment failed. This state is possible only when the payment was performed from a bank account.

Denied“: Seller cancelled the payment. The payment is in
this state when the seller cancels the payment after having had the
Pending state before.

Refunded“: Money is returned to buyer. The payment is in this state when seller cancels the payment having the Completed state.

pending_reason Reason of payment delay. Possible values are:

echeck“: Payment was performed with an e-check

multi_currency“: Payment was performed in the currency
that is specified in the settings of the seller’s account. The payment
will be completed when the seller confirms the transaction.

intl“: Seller is not a USA dweller. The payment will be completed when the seller confirms the transaction.

verify“: Seller’s account is in the “unverified” state. The payment will be completed when the seller is identified.

address“:
Settings of the seller’s account require that the buyer should specify
the delivery address, but the buyer does not specify the address. The
payment will be completed after the seller confirms the transaction.

upgrade“: Payment was performed using a credit card and
the seller’s account has the “Personal” status. To complete the
payment, the seller should upgrade the account up to “Business” or
“Premier.”

unilateral“: Seller’s e-mail is not registered in the system.

other“: Another reason. The seller needs to contact Support to know more about the reason.

payment_type Payment type. Possible values are:

echeck“: Payment was performed with an e-check.

instant“: Payment was performed with a credit card or using a bank account or money from buyer’s PayPal account.

mc_gross Payment amount.
mc_fee Commissions charges. The amount that is put on seller’s account is determined as mc_gross - mc_fee
mc_currency Payment currency.
first_name Buyer’s first name.
last_name Buyer’s last name.
address_street Street.
address_city City
address_state State/Region.
address_zip Zip Code.
address_country Country.
verify_sign Digital signature. It is used in PayPal for transaction verification.

Table 1

Step 3. Testing integration

It is important to test IPN integration before “going live”. The
PayPal Sandbox provides an environment for testing without performing
real payment transactions (see Getting Started with PayPal Sandbox). It is also a good idea to test on the live PayPal system before putting a system into production.

Follow the following steps to test integration,

  1. Create a developer account on Developer Central http://developer.paypal.com
  2. Create a Personal account and business account on the sandbox
  3. Use the URL https://www.sandbox.paypal.com/cgi-bin/webscr instead of live URL https://www.paypal.com/cgi-bin/webscr
  4. Use sandbox business email as business (seller’s e-mail)
  5. Run your application and step through a payment using the Personal account email and password for log in.

Use Instant Payment Notification (IPN) Simulator tool to test IPN.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Twitter
  • Yahoo! Buzz

Posted in Asp.net, C#.

Tagged with , .


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. prasad says

    Thanks,
    This example is simple to understand and easy to customize.
    It helps me a lot,

Continuing the Discussion

  1. Pass Individual Items to PayPal | amolwable.com linked to this post on September 27, 2009

    [...] my previous post PayPal Integration with IPN I have wrote about basic PayPal integration using websites payment method by passing the Aggregate [...]



Some HTML is OK

or, reply to this post via trackback.