KeyLimeTie Blog

PayPal API and Checkout Integration

By Brian Pautsch – 1/28/2006 12:08:12 PM. Posted to Applications.

While building websites over the past few years, many customers have asked for a seamless PayPal integration. Until a couple years ago, this wasn't very easy. Now that PayPal has their library of Web Controls and API interfaces, integrating an ecommerce website with PayPal is very easy. This example website shows you how to do it.

Getting started:
1. Download and read the PayPal API Integration Guide
2. Create a PayPal Developer account at http://www.paypal.com/cgi-bin/webscr?cmd=p/pdn/intro-outside
3. Create a PayPal account (if you don't have one already) and get your account Verified.
4. In your PayPal account, under 'Profile -> API Access', create an API certificate and convert it to a PDT key at http://www.paypaltech.com

Download code

1. Webpage interface and code behind
I created a simple DataGrid that binds to a DataTable. To allow for RadioButtons in a datagrid, I chose to use the MetaBuilders custom RadioButton control.


Lines 57-75: On Page_Load, not IsPostBack, I create a DataTable of IPod products, bind them to the DataGrid and Save them to Session State. Also, we assign the PayPal email address to the checkout button on Page_Load.
Lines 115-141: This event fires by the PayPal UploadCompleteCartButton button. To pass shopping cart items to PayPal, create and populate a "CartItem" object for each product and add them to the UploadCompleteCartButton button. Then, populate the "CustomerInfo" object in the UploadCompleteCartButton. And finally, populate the "Custom" property with the order number key from your system and set the return URL to "PDTHandler.aspx". PDTHandler.aspx is a page we will create to read PayPal's response to our transaction.

Upon clicking the "PayPal Checkout" button...
The user is taken to PayPal.com where they can login, create an account or simply checkout (PayPal no longer requires an account to make a credit card purchase!).


Upon logging in...
The user will see the product they selected and can checkout (Note: change code to send description only!).


2. Upon Checkout...PDTHandler.aspx
When the user completes the transaction (or cancels), PayPal will automatically reroute them to the "ReturnUrl" specified in the UploadCompleteCartButton's click event.
Lines 37-53: On Page_Load, we check to see if a transaction was returned (if not, the user canceled the PayPal transaction). If there is a transaction, we call "GetPDT" to get the transaction details.
Lines 57-88: GetPDT makes an HttpWebRequest out to PayPal and receives a stream of text back. This text string is delimited with a newline and carriage return. The first line returns "SUCCESS" or "FAIL" and the remaining lines are the details. Each detail line is a named value pair collection delimited with an equal sign. Details on the response can be found on page 101 of the PayPal API Integration Guide.
Lines 92-13: GetPDTValue is a method I included to help you find specific transaction return values. Just pass in the PDT return string and the key and the value will be returned.

Comments

On 6/25/2006 ashok said:
thanks for the code i was able to complete my task on time.

Leave a Comment

Name:
Email:
URL:
Comment:
Security Code:
Type Security Code:

Photos on Flickr

More Photos »

Search Blog


Get Email Updates

Like what you read here at KeyLimeTie? Sign up for our email list!

Subscribe