Wednesday, January 9, 2008

ASP.NET Implementation for Paypal Website Payments Standard

An ASP.NET Implementation of Paypal Website Payments Standard integration for single payments & subscriptions.

Paypal's Website Payments Standard is, in my opinion, a very very awesome way for small to mediums size businesses to accept & process credit card payments on their site. It is also a very nice and easy way for the general masses to accept donations on their website for their charity or as a thank you for their hard work posted on the internet. It gets a very very awesome rating from me for the following reasons:
- The fees are relatively low
- Its easy to integrate with
- Paypal handles the security

The last line I highlighted and italic'd is the key. Properly securing a website to handle accepting credit cards can be a hugely expensive and time consuming task if you want to do it right. Paypal has done it right, they maintain all of the infrastructure to keep your users safe and they have written all of the code to take care of the processing. Why re-invent the wheel when Paypal already has such a dandy solution for you to take advantage of?

While performing some analysis on Paypal Website Payments Standard I decided to go ahead and write the code in ASP.NET 2.0 to perform the integration. I first off went out to the web to see if anyone had already posted implementation code, but I didn't find much. So, I figured why not write it in such a way that I can share it with the world at large afterwords. So here you have it, an ASP.NET 2.0 implementation of Paypal Website Standard Payments for single payments, donations and subscriptions.

The Code
First off, you can download the full source code here.
PaypalIntegration.zip

You need Visual Studio 2005 or 2008 (any version, including the free Visual Studio Web Developer Express) and ASP.NET AJAX (1.x).


You will also need Paypal Website Payments Standard account (and I recommend a Sandbox account for testing.)


Paypal Website Payments Standard



Paypal Sandbox Accounts



Subscription & Product Data
To make the subscriptions & single item payments easy to update and not require any re-compiling of the code, put everything into an XML file in the project. You can find it in the App_Data directory in the Products.xml file.





Inside the XML file, you will find monthly, yearly & single item definitions. Editing these will effect the choices presented in the different respective implementations.



And my final note about the subscription data is that all of the data is referenced using the XMLDataSource object. Note that I used an XPath expression to filter what XML section is displayed for each XMLDataSource.

Globals

The Globals.cs class in the App_Code folder defines a bunch of the standard Paypal variables that will be used for all of the transactions. This is where you set post url (live vs. sandbox) and your Paypal email address (so the money goes into your account.)

Subscription Implementation
https://www.paypal.com/IntegrationCenter/ic_std-variable-ref-subscribe.html


The subscription implementation is currently configured to handle monthly or yearly subscriptions. You can change this behaviour by editing the XML file and the implementation a bit. However, by default this should get you most, if not all, of the way there.

Single Payment Implementation
https://www.paypal.com/IntegrationCenter/ic_std-variable-ref-buy-now.html


The single payment implementation allows you to add/edit items via the XML file and then allows the user to select an item, enter a quantity and then head to Paypal to purchase. Shipping, if necessary, would need to be managed by Paypal or added to the implementation.

Donation Implementation
https://www.paypal.com/IntegrationCenter/ic_std-variable-ref-donate.html


This is a pretty straight forward implementation. It accepts the donation amount, sets the name of the donation and then submits.


Customization

I put a lot of the commonly used Paypal HTML variables in the code and commented them out. If you want to use those variables, just uncomment out the code and assign a value to the variable. The implementations are very generic, so most people will need to customize what I have done to some extent, but just about everyone should be able to use what I have included.

Running the project will launch the Default.aspx page, which contains an example instance of all three implementations. You can customize each implementation to meet your needs, but I believe this will take care of the basics for you.

Let me know if you find any bugs or problems and I will update the implementations.

-Aaron

kick it on DotNetKicks.com

8 comments:

Anonymous said...

Great article and code sample!! Very clear and easy to understand.

Ira Newman said...
This comment has been removed by the author.
Ira Newman said...

I loaded the PayPal code into Visual Studio and it failed. Some type of compilation error. I'm somewhat new to this but I'm looking for a PayPal implementation for a subscription/membership website. Also, I've been coding in VB so it's a little difficult to troubleshoot. Any suggestions. Thanks

Anonymous said...

Excellent, well-laid out example. There is very limited resources on the web for asp.net 2.0 paypal subscriptions integration. Thank you! Would be brilliant to include a simple IPN implementation in your next version!

Anonymous said...

Hey!

Great post!

All I'm missing is a simple IPN, do you have a snippet you think?

Have a great day!

Emran said...

A great explanation indeed. I just wanted to share a Control I found for PayPal Website Payments Standard. Here it is :

http://www.spicelogic.com/Products/ASP-NET-PayPal-Control-for-Website-Payments-Standard-4

I hope that will help someone who wants to get rapid integration with PayPal.

Anonymous said...

This post is dated, but it helped me out tremendously! Thank you so much for posting this solution. I have searched hi and low with much frustration and wasted hours. Thank goodness that I came across this post, and for people like you that help other people out.

Mohan's Blog said...

Thanks for writing this article. Please keep writing this types of articles so that other people can take advantages of this.
Thank you very much again.