Pay pal IPN Integration

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.


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

dhiraj79Author Commented:
Thanks for the reply
Can I use .ashx (httpHandler) page as IPNLISTENER?
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.