• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1179
  • Last Modified:

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.

 

Thanks
0
dhiraj79
Asked:
dhiraj79
  • 2
1 Solution
 
oleggoldCommented:
in vb.net,good for asp.net 2
protected void Page_Load(object sender, EventArgs e)
 
{
 
//Post back to either sandbox or live
 
string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
 
//string strLive = "https://www.paypal.com/cgi-bin/webscr";
 
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);
 
streamOut.Write(strRequest);
 
streamOut.Close();
 
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
 
string strResponse = streamIn.ReadToEnd();
 
streamIn.Close();
 
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
 
}
 
else
 
{
 
//log response/ipn data for manual investigation
 
}
 
}
 
 

Open in new window

0
 
dhiraj79Author Commented:
Thanks for the reply
Can I use .ashx (httpHandler) page as IPNLISTENER?
0
 
wcfguruCommented:
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 eg.www.xxx.com/ConfirmIPNResult.aspx

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 paypal.com.
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);
        objIPN.VerifyTransaction();
    }

    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

PPWrapper.zip
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now