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

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

As usual the code is here