KeyLimeTie Blog

PayPal API and Checkout Integration

By Brian Pautsch – 1/28/2006. 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.

ASP.NET Custom Events - UserControls and Pages

By Brian Pautsch – 1/14/2006. Posted to Code Snippets.

I have been working on a project that uses a lot of UserControls. Often, multiple UserControls are loaded onto the same Page. And in some cases, what is processed on the PostBack of one userControl has a direct effect on the data displayed on the base page or another UserControl. The problem is that the Page and UserControls have already loaded and run through their Page_Load events. How do you handle this?

Answer: Custom Events
Actually, it's very simple. This article explains the most common scenario.

Users tab:


Tasks tab:


Problem: We have a base Page that holds two UserControls: Users and Tasks (see above). Our Users UserControl allows the users to be added/updated/deleted. The Tasks UserControl allows tasks to be created and assigned to users. When the Page loads for the first time, both UserControls are loaded for the first time and their dropdownlists are also loaded once (including the list of users on the Tasks UserControl).

On subsequent posts, the Page.IsPostBack property is checked and the trips to the database are saved. When a user is added on the Users UserControl, the cmdSave (Save button) event triggered and the user is added to the database...but it isn't reflected in the Tasks UserControl. Here's what needs to be done:

1. Users UserControl code - Declare a custom event (UCUpdated) and a generic method (RaiseUCUpdatedEvent) to call to fire the event. When appropriate, call RaiseUCUpdatedEvent. In the example below, a user is deleted so we must refresh the tasks' users dropdownlist otherwise we'll get referencial integrity errors when trying to assign a task to the deleted user.
1//Declare event2publicevent CommandEventHandler UCUpdated;
34//Method to fire event5privatevoid RaiseUCUpdatedEvent(string strMessage)
6{
7 CommandEventArgs args = new CommandEventArgs("UCUpdated", strMessage);
8 UCUpdated(this, args);
9}
1011//Sample code that triggers the event to fire12objDataSvc.DeleteProjectUsers(intProjectUserID);
13if (objDataSvc.ErrorCode == 0)
14{
15 lblMessage.Text = "Delete successful!";
16 RefreshDataGrid();
17 RaiseUCUpdatedEvent();
18}
19
2. Page code - In the Page_Load event, add an event handler for the Users UserControl UCUpdated event that references a private method. In the example below, we simply call the public method "Load ProjectUsers()" in the Tasks UserControl. This method clears the dropdownlist and loads the current project users (same method called in the Tasks UserControl's Page_Load when Page.IsPostBack is false.
1//Page_Load - add event handler2privatevoid Page_Load(object sender, System.EventArgs e)
3{
4 ucProjectUsers.UCUpdated += new System.Web.UI.WebControls.CommandEventHandler(ucProjectUsers_UCUpdated);
5}
67//Event handler8privatevoid ucProjectUsers_UCUpdated(object sender, System.Web.UI.WebControls.CommandEventArgs e)
9{
10 ucProjectTasks.LoadProjectUsers();
11}
12

Photos on Flickr

More Photos »

Search Blog


Get Email Updates

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

Subscribe