Pay pal IPN Integration

Posted on 2008-11-04
Last Modified: 2013-11-29
Hi all,

I read on pay pal site that IPN CAN BE USED FOR


1.notify your server every time a new transaction occurs

2. if the status of a prior transaction changes, use IPN


I want to know that suppose someone paid on my website and due to any reason the payment statuse is pending at the moment(Say if verification is enabled).

Now on my return page Page_ load I will get pending and update my database accordingly.

The user will not be able to use the paid service till the payment status is "completed " in my database.


Now suppose the payment status is completed after one hour.

How the pay pal will notify me so that I can give the user paid services.


Question by:dhiraj79
    LVL 21

    Expert Comment

    LVL 21

    Expert Comment

    in,good for 2
    protected void Page_Load(object sender, EventArgs e)
    //Post back to either sandbox or live
    string strSandbox = "";
    //string strLive = "";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);
    //Set values for the request back
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
    string strRequest = Encoding.ASCII.GetString(param);
    strRequest += "&cmd=_notify-validate";
    req.ContentLength = strRequest.Length;
    //for proxy
    //WebProxy proxy = new WebProxy(new Uri("http://urlort#");
    //req.Proxy = proxy;
    //Send the request to PayPal and get the response
    StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
    StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
    string strResponse = streamIn.ReadToEnd();
    if (strResponse == "VERIFIED")
    //check the payment_status is Completed
    //check that txn_id has not been previously processed
    //check that receiver_email is your Primary PayPal email
    //check that payment_amount/payment_currency are correct
    //process payment
    else if (strResponse == "INVALID")
    //log for manual investigation
    //log response/ipn data for manual investigation

    Open in new window


    Author Comment

    Thanks for the reply
    Can I use .ashx (httpHandler) page as IPNLISTENER?
    LVL 1

    Accepted Solution

    1. SetUp requirements or paypal
    a.Login to your PayPal account/ Sandbox .
    b.In the profile set the Instant Payment Notification (IPN) link to the location of the webpage where paypal should notify the result of the IPN transaction

    c.Using the setup above, paypal knows which URL to execute when the IPN transaction result is completed.

    2.SetUp the Thank you page to be displayed and add it to your PayPal Profile ie. the page where paypal
    will navigate the user back to your site.

    3. User clicks checkout from the shopping cart and transfered
    The user will log in to their paypal account and make a payment.

    4. When the user completes the purchase they will be redirected to the thankyou page configured in Paypal(STEP 2 above).This link should display a message
    "Thank you for your purchase you will shortly receive an email blah blah blah.."
    Dont care doing anything now as Paypal will take some time to process the payment and provide the result.
    This is asynchronous processing happening and not a direct payment/realtime processing.

    5.Paypal will take perhaps some time to process the payment and then it will send a receipt(or more clearly execute the IPN page configured in Step 1b above.

    6. In the aspx page PaymentVerified()
    a.Add database logging code, EMail informing about success/failure.
    b. Send User Login credentials or anything else to the customer
    c.Activates user accounts

    7.Remember you dont need to do anything very flashy on your notification aspx page
    as paypal will only execute this page as a background process.What is important
    is the code behind ie.what you want to be logged when Paypal calls this page and
    how much of feedback logic you can implement here.

    8.The real part is the C# dll which provides you the delegates to communicate
    asynchronously to PayPal.

    Refer to the attachment for the full working code.
    //Code behind of a blank aspx file for paypal notification
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using PPWrapper;
    public partial class _Default : System.Web.UI.Page 
        //true: for testing Sandbox URL
        //false:for live environment
        public PP_IPN objIPN = new PP_IPN(true); 
        protected void Page_Load(object sender, EventArgs e)
            //Add the events
            objIPN.PaymentUnverifedEvent += new PP_IPN.PaymentUnverifedEventHandler(PaymentNotVerified);
            objIPN.PaymentVerified += new PP_IPN.PaymentVerifiedEventHandler(PaymentVerified);
        void PaymentVerified(object sender, PPWrapper.PaymentStatusEventArgs e)
            //Paypal processed successfully
            //but this does not imply that payment was completed
            //check for status flags uncomment and implement your own logic
            if e.PaymentStatus = BasicInformation.Payment_Status_Variables.Canceled_Reversal
            if e.PaymentStatus = BasicInformation.Payment_Status_Variables.Completed
            if e.PaymentStatus = BasicInformation.Payment_Status_Variables.Expired
            if e.PaymentStatus = BasicInformation.Payment_Status_Variables.Processed
            if e.PaymentStatus = BasicInformation.Payment_Status_Variables.Refunded
        void PaymentNotVerified(object sender, EventArgs e)
            throw new Exception("Error Processing payment");

    Open in new window

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
    For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space. provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
    Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now