Integrating eBay sales with a Google App Engine shopping cart - Part 1

By Peter Georgeson

Overview

When selling online, eBay represents a significant opportunity for increasing sales. It has millions of users and is a well-known, trusted brand world-wide.

One problem associated with listing items on eBay is the integration of items sold via eBay with an existing sales process. For instance, if you already manage an online store, how are inventory levels managed when another sales channel - eBay - is added to the mix? How does the online store know when something sells on eBay? Or, if the last item in stock is sold on the online store, how can that item be removed for sale from eBay?

Fortunately, eBay provide a comprehensive API that not only enables the automated addition and removal of items for sale, it also provides a notification mechanism for sold items.

This series of articles demonstrates how to create a simple shopping cart on Google App Engine, then describes how to integrate this shopping cart with eBay.

Sign up to the PayPal sandbox

To test the shopping cart application, PayPal credentials are required. PayPal provide a sandbox that is specifically for testing applications without having to use real money.

If you don't already have one, create a PayPal developer account at https://developer.paypal.com/ for testing. After creating your main developer account, seller and buyer test accounts need to be created.

Install the shopping cart

A simple shopping cart developed for Google App Engine can be downloaded from GitHub.

To configure this application to run locally:

  1. Sign up for Google App Engine;
  2. Download and install the AppEngine SDK for Python. Note that Python version 2.5 or newer is required. App Engine is not compatible with Python 3;
  3. Configure your PayPal credentials in settings.py. To retrieve your credentials, log in to the PayPal sandbox and click on "API Credentials". More detailed instructions are provided at https://www.x.com/docs/DOC-3266

To run the sample application locally:

  1. Start up the Google App Engine Launcher: Windows and Mac come with an easy to use GUI;
  2. Add an existing application: File→Add Existing Application;
  3. Browse to the "app" directory in your local GitHub repository;
  4. Click "Run";
  5. Direct your web browser to http://127.0.0.1:8080/.
All going well, a basic front page will be displayed:

To add an item to sell, browse to http://127.0.0.1:8080/sell.

The following section uses PayPal's Instant Payment Notification (IPN) to manage inventory levels. For IPN to work properly, the application must be deployed to a public web address, so that PayPal can send notifications. To deploy the application:

  1. Browse to https://appengine.google.com/;
  2. Click "Create an Application" and find an unused application ID;
  3. Edit app.yaml in the source code to reflect the same application ID;
  4. Open Google App Engine Launcher, select the application, and click Deploy; and
  5. Browse to http://applicationID.appspot.com/ to verify that your application is live.

IPN Integration

IPN is PayPal's recommended technology for integrating with your own administrative functions, such as order fulfilment and inventory management.

When IPN is enabled, PayPal connects to a URL that you provide, with updates to the status of a transaction. IPN is robust and PayPal guarantees its delivery. If PayPal cannot reach your site due to downtime or connectivity issues, PayPal will continue trying to connect until your site has been successfully notified.

To enable IPN notifications in the sample application, edit settings.py and ensure that USE_IPN is True.

With IPN enabled, a URL will be included in the initial purchase request to PayPal, which will enable PayPal to call back to the application when the customer completes payment:


if ipn_url != None:
  data['ipnNotificationUrl'] = ipn_url

The PayPal IPN request is handled by the main.IPN class which initially passes the request to paypal.IPN for verification.

The first verification step is ensuring that the request comes from PayPal. The entire request is posted to PayPal and if it originated from PayPal, then the response code is 200 and the response body is VERIFIED:


verify_response = url_request( "%s?cmd=_notify-validate" % settings.PAYPAL_PAYMENT_HOST, data=urllib.urlencode( request.POST.copy() ) )
# check code
if verify_response.code() != 200:
  self.error = 'PayPal response code was %i' % verify_response.code()
  return
# check response
raw_response = verify_response.content()
if raw_response != 'VERIFIED':
  self.error = 'PayPal response was "%s"' % raw_response
  return

Next, the currency, amount and transaction status are checked.


# check payment status
if request.get('status') != 'COMPLETED':
  self.error = 'PayPal status was "%s"' % request.get('status')
  return

(currency, amount) = request.get( "transaction[0].amount" ).split(' ')
if currency != 'USD':
  self.error = 'Incorrect currency %s' % currency
  return

Once verified, the item is considered to have been successfully purchased. Now inventory levels can be updated and order fulfilment initiated:


# success
purchase.status = 'COMPLETED'
purchase.put()
item = purchase.item
item.available -= 1 # update inventory
item.put()

The item inventory has been reduced and once the value of available reaches zero, the item will no longer be displayed as being available for purchase on the store.

Integrating with eBay

The world's largest online market place, eBay has over 94 million active users world wide. With this huge user base, it makes sense to consider having a presence on eBay when selling goods. One issue that might discourage listing items on eBay is the process of integrating an eBay sale into an existing workflow.

Fortunately, eBay's developer API enables items to be programmatically added and removed from eBay. eBay also provide a notification API that is equivalent to PayPal's IPN, enabling inventory levels and order fulfilment to be managed using the same backend process.

Getting started with the eBay API

The first step to using the eBay API is to sign up to the service. Visit https://developer.ebay.com/ and Sign up.

The next step is to generate the necessary application keys. Under My Account, generate both Sandbox and Production keys.

You can verify that your account and keys are configured correctly by clicking Run the sample application within the eBay developer portal. The sample uses your application keys to retrieve some items for sale via the API.

Using eBay's trading API

For some of the eBay APIs, application keys are not sufficient - a user token is required. The API call that lists an item on eBay uses the trading API, which requires an application to be configured and a user token must be obtained.

A user token enables an application to perform operations on behalf of a user - such as listing an item for sale or removing an item from eBay.

A user token is created when an eBay application is linked to an eBay user. This user must explicitly approve an application's access and can also revoke access at any time. In this article the application is linked to a known user, however eBay also provide the capability to create public applications. This enables anyone with an eBay account to use the functionality that you provide in your application.

Initially the sample application is linked to a test user on the eBay sandbox. Once the application has been adequately tested, it can be linked to a live eBay account using a similar process. This eBay user can then be used to list real items for sale on eBay's live auction site, via the functionality provided by this eBay application.

To link a sandbox user to your eBay application, first select Application Settings. Set the environment to Sandbox and the keyset to Key Set 1 as shown.

For a live deployment you would instead set the environment to Production. Otherwise all other steps are identical.

Scroll down to the user tokens section and click on Generate Token.

The next step asks you to link your application to an eBay user. Since this is the sandbox environment, the application must be linked to a sandbox user. If you haven't already created a sandbox user, click "Register" and create a sandbox eBay user, then return to this page to link the application to your newly created sandbox user account.

Once the application is linked to the user account, a token will be provided. Ensure you click "Save Token", otherwise the token will not be valid.

Now go to the App Engine web application and set the appropriate values in settings.py:


# ebay settings
USE_EBAY = True
EBAY_AUTHTOKEN = '*** YOUR EBAY TOKEN ***'

Success! You now have an application that is configured to list items on behalf of the linked sandbox user account, using eBay's developer API.

Conclusion

One problem with including eBay as a sales channel is integrating eBay with an existing process - such as an existing online shopping cart. This article first described how to configure and deploy a simple shopping cart on Google's App Engine, before demonstrating how to use PayPal's IPN to manage inventory levels. The steps involved in configuring an eBay application were described in preparation for integrating eBay with this existing process.

The next part in the series will build on this framework and demonstrate how to add and remove items for sale using the eBay developer API. eBay notifications will be utilized to manage inventory levels.

Links and Further Information