Thursday, March 11, 2010

Paypal Integration Demystified

Asynchronous paypal integration can be a handful for programmers especially with so many different ways of doing the same thing and few proper fullscale implementations in .NET.
I really wished that Paypal could have just created an API/Dll for the .NET developer community and they could just focus on integrating to the payment gateway instead of having to learn the API and create wrappers.

Just out of interest here is the overview of the Paypal IPN process as well as the code which you can use at your own risk!!!

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.If anybody can extend this code to other gateways then
do drop in a message for me.

As usual the code is here

6 comments:

  1. Im trying to find if the transaction ID returned does not already exist. Where to add that logic? We tried under PaymentVerified()
    but the variables under BasicInformation is not returned.

    ReplyDelete
  2. GURU: In confirmipnresult.aspx.cs, Line 21,

    objIPN.PaymentVerified += new PP_IPN.PaymentVerifiedEventHandler(PaymentVerified);

    should read
    --------------
    objIPN.PaymentVerifiedEvent += new PP_IPN.PaymentVerifiedEventHandler(PaymentVerified);

    else the compiler will throw an exception

    ReplyDelete
  3. Daniel: Try "objIPN.BasicInformation.txn_id"

    ReplyDelete
  4. Hi Norman,
    Thx for identifying.I will modify the code and upload it again.

    ReplyDelete
  5. Very nice ... not only helped with IPN but is also excellent object oriented programming instruction. How well do you think this would port to handle PDT?

    ReplyDelete
  6. Thanks for this wrapper. Paypal keeps hitting my listener. Shouldn't we send a response to the verified/invalid post back to paypal?

    ReplyDelete