The Problem
Over the years I’ve worked with numerous merchants using PayPal for their payment processing needs. Many of these merchants are selling merchandise on their own website as well as utilizing the eBay platform because, let’s face it, if you’ve got product, you need to have at least some of it on eBay.
These days, sellers seem to understand that fact; however, many of them neglect to utilize both sources because of extra time and overhead involved in managing inventory in both places. When I’m supporting a client and I mention the fact that they should definitely post their product on both eBay and their own web site I almost always get a response somewhere along the lines of…
When I have my items posted in both places I end up overselling because somebody will buy it off the website and off eBay before I can remove it from one place or the other.
When this happens, of course, it can be a big headache. The seller must refund one of the buyers and explain that they oversold the item, which is never fun. Then you have to deal with final value fee refunding and potential for bad feedback on eBay at times. The time and headache involved often keeps people from using eBay, but then they become frustrated when their sales drop as they try and use nothing but their own fresh web site that hasn’t had much time for any SEO or social network marketing to kick yet.
Ass-umptions
The good news is that there is a simple solution to this problem. By utilizing the notification systems provided by PayPal (IPN) and eBay (Platform Notifications) you can automatically remove an item from one source if it were to sell at another. Having said that, we’ll need to make some assumptions for the information I’m going to discuss here to make sense.
If you want to remove items from your website when it sells on eBay you will need to be working with a database system that provides internal product ID’s as well as their related eBay auction data (ie. Item numbers, current status, etc.) If you don’t have something like this already there are ways to automate this procedure over the API’s as well, however, that’s outside of the scope of this particular article so we’ll need to visit that another day.
The Solution
First, let’s take a look at how to handle the automation of removing items from eBay that sell on your web site. You may have seen a previous article of mine regarding PayPal Instant Payment Notification. That’s exactly what we’re going to use here, so if you’re unfamiliar with it you should read that article first and then come back and pick up where you left off. We’ll move forward as if we’ve got a working PayPal IPN solution up-and-running that we can tie in to.
Consider a scenario in which you’ve got a Widget for sale on both your own web site and on eBay. Great news! The widget has sold from your website! The payment for the item on your website will trigger an IPN from PayPal which will include all of the order and item information. Part of the magic of notification systems is that we can do whatever we want with this data, including make calls to third party databases. In this particular case we’ll be making a call to eBay’s EndItem API to complete our goal of removing that item from eBay so that it does not sell before we have a chance to remove it manually.
The eBay API
Here at X.com, the focus has generally been around PayPal and their payment platforms. From the looks of things that is about to change, but unless you’re already familiar with the eBay Developer Program you may not know that they have a vast array of API methods you can utilize within your applications and they work in essentially the same exact way that PayPal’s API’s do.
You can find all the information necessary to get up-and-running with eBay API’s in their Developer Center. Much like PayPal, you’ll need to create an eBay developer account as well as sandbox accounts for testing purposes. You can then you can use the eBay sandbox to post items and remove them (and anything else eBay would allow you to do, of course) while building your solution.
With all of that out of the way, let’s focus on the GetItem and EndItem API calls. We’ll look at the raw XML data throughout this guide. If we study the API reference provided by eBay we find that the GetItem and EndItem calls are very simple and that only require the standard credentials for an API call with eBay and a couple of additional basic parameters.
NOTE: If you click on any of the XML elements in the API documentation it will provide more details on that element, the type of data you can send with it, and any particular values that can be included.
The following PHP sample would first use the GetItem API to retrieve some info about the current item and then either display a warning message or go ahead and remove item number 123456789 from eBay because it was already sold elsewhere. Generally speaking, you’ll probably be using some sort of class library to handle much of the code involved in that sample; however, this provides a good overview of exactly what is happening and how it would be put together.
// What eBay item do we want to remove? $eBayItemID = '123456789'; // Build HTTP Headers for GetItem call $GetItemHeaders = array ( 'X-EBAY-API-COMPATIBILITY-LEVEL: 721', 'X-EBAY-API-DEV-NAME: ' . $API_DEV_NAME, 'X-EBAY-API-APP-NAME: ' . $API_APP_NAME, 'X-EBAY-API-CERT-NAME: ' . $API_CERT_NAME, 'X-EBAY-API-CALL-NAME: GetItem', 'X-EBAY-API-SITEID: 0' ); // Build eBay GetItem request $GetItemRequest = '<?xml version="1.0" encoding="utf-8" ?>'; $GetItemRequest .= '<GetItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">'; $GetItemRequest .= '<RequesterCredentials>'; $GetItemRequest .= '<eBayAuthToken>'.$API_AUTH_TOKEN.'</eBayAuthToken>'; $GetItemRequest .= '</RequesterCredentials>'; $GetItemRequest .= '<DetailLevel>ReturnAll</DetailLevel>'; $GetItemRequest .= '<ItemID>'.$eBayItemID.'</ItemID>'; $GetItemRequest .= '</GetItemRequest>'; // Use CURL to POST XML request to eBay and store response to $eBayResponse $Connection = curl_init(); curl_setopt($connection, CURLOPT_URL, 'https://api.sandbox.ebay.com/ws/api.dll'); curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($connection, CURLOPT_HTTPHEADER, $GetItemHeaders); curl_setopt($connection, CURLOPT_POST, 1); curl_setopt($connection, CURLOPT_POSTFIELDS, $GetItemRequest); curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1); // Store the XML response from eBay in $response $GetItemResponse = curl_exec($Connection); // Close CURL curl_close($Connection); // Use DOM to parse the response accordingly $DOM = new DOMDocument(); $DOM->loadXML($GetItemResponse); $Ack = $DOM->getElementsByTagName("Ack")->item(0)->nodeValue; $Errors = $DOM->getElementsByTagName("Errors"); if($Errors->length > 0) { // Handle errors accordingly } $ItemStatus = $DOM->getElementsByTagName("ListingStatus")->length > 0 ? $DOM->getElementsByTagName("ListingStatus")-> item(0)->nodeValue : ”; $BidCount = $DOM->getElementsByTagName("BidCount")->length > 0 ? $DOM->getElementsByTagName("BidCount")->item(0)-> nodeValue : ”; if($BidCount > 0) exit('You must cancel all bids on the auction before you can kill it.'); elseif($ItemStatus != 'Active') exit('The listing is not currently active and cannot be killed.'); // All go to remove item! // Build HTTP Headers for EndItem call $EndItemHeaders = array ( 'X-EBAY-API-COMPATIBILITY-LEVEL: 721', 'X-EBAY-API-DEV-NAME: ' . $API_DEV_NAME, 'X-EBAY-API-APP-NAME: ' . $API_APP_NAME, 'X-EBAY-API-CERT-NAME: ' . $API_CERT_NAME, 'X-EBAY-API-CALL-NAME: EndItem', 'X-EBAY-API-SITEID: 0' ); // Build eBay EndItem request $EndItemRequest = '<?xml version="1.0" encoding="utf-8"?>'; $EndItemRequest .= '<EndItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">'; $EndItemRequest .= '<RequesterCredentials>'; $EndItemRequest .= '<eBayAuthToken>'.$API_AUTH_TOKEN.'</eBayAuthToken>'; $EndItemRequest .= '</RequesterCredentials>'; $EndItemRequest .= '<ItemID>'.$eBayItemID.'</ItemID>'; $EndItemRequest .= '<EndingReason>Sold</EndingReason>'; $EndItemRequest .= '</EndItemRequest>'; // Use CURL to POST XML request to eBay and store response to $eBayResponse $Connection = curl_init(); curl_setopt($connection, CURLOPT_URL, 'https://api.sandbox.ebay.com/ws/api.dll'); curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($connection, CURLOPT_HTTPHEADER, $EndItemHeaders); curl_setopt($connection, CURLOPT_POST, 1); curl_setopt($connection, CURLOPT_POSTFIELDS, $EndItemRequest); curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1); // Store the XML response from eBay in $response $eBayResponse = curl_exec($Connection); // Close CURL curl_close($Connection);
The Other Way Around
There will be times as well that an item sells on eBay and you need to have it removed from your own website. Essentially, the same procedure would take place, but it would be the other way around and you’d be interacting with your own database a little bit more.
When an item sells on eBay you have a couple of options for how to handle it. Any payment that comes through via PayPal will trigger a PayPal IPN the same way your web site sales would. In this case, though, we’ll see a parameter included in the IPN called for_auction and we’ll know that this particular IPN is from an eBay sale. As such, we can simply take the product ID out of the IPN data and query our own database to remove the item completely, or possibly just update the QTY based on the number of items sold on the order.
eBay also provides a notification system called Platform Notifications which will trigger a data notification exactly the same way that PayPal’s IPN system works. This can be useful because the eBay notification system has a wider variety of events that you can handle within the eBay platform.
For example, you may not always receive payments via PayPal from an eBay sale and you’ll still need to handle those orders correctly. eBay’s notification system includes events for an item selling (prior to payment), the actual checkout taking place, and a whole lot more so you can easily trigger updates of your own database at any point along the way and sync things up exactly the way you need it.
Conclusion
While the notification systems can be a little bit daunting and confusing at first, once you get the hang of it you’ll see that it’s nothing more than receiving data like a form POST and processing it accordingly. Stick with it and you’ll love it!
Both the PayPal and eBay platforms are essential pieces of eCommerce solutions today and use of their API’s together can be a true benefit to online sellers.
Happy Integration and Coding!