Friday, March 7, 2014

Paypal IPN(Instant Payment Notification) Integration










Paypal IPN Integration sounds very easy at first but has several pin points which needs to be take care of.

Follow 3 simple steps to make IPN flow work for you web application: 

Step 1: Open a business account with Paypal 


 For testing you may open an account with http://sandbox.paypal.com
 For live server you have to open an account with http://paypal.com

Step 2 : Add a form button (Paypal Button) to your existing html page


<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="custom" value="${userId}">
<input type="hidden" name="business" value="abc@gmail.com">
<input type="hidden" name="lc" value="CA"> <input type="hidden" name="item_name" value="Tangled Roots">
 <input type="hidden" name="button_subtype" value="services">
 <input type="hidden" name="no_note" value="0"> 
<input type="hidden" name="cn" value="Add special instructions to the seller"> 
<input type="hidden" name="no_shipping" value="2">
<input type="hidden" name="return" value="http://www.xyz.com/return.html">
<input type="hidden" name="notify_url" value="http://www.xyz.com/notify.html">
<input type="hidden" name="currency_code" value="USD"> <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHosted">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"><img alt="" border="0"
src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif"
width="1" height="1">
</form>
   

The "business" tag should have the email address of the business account opened in step 1.

As you see in the above form return URL is always GET URL which you want to get back to once the transaction is successful.

Another URL is notify_url which should always be POST URL which you want Paypal to post back once the transaction to be successful. "tnx_id" is one of post parameters which  Paypal will send back.

Following diagram shows how paypal treats our notify_url as IPN Listener.



Step 3 : Write your IPN Listener

              IPN Listener is nothing but a method accepting POST Requests.

Mandatory Step : Mention IPN notification URL and enable it to see results

           1.  Login in to you business account and click on My Setting Tools under profile menu
           2.  On right panel  under "Getting paid and managing my risk" click on  update   against  "Instant payment notifications".
           3. Mention the notification URL and check Enabled radio button.


Hurray !  IPN Integrated





1 comment: