jdawg
jdawg

Reputation: 558

ebay-sdk-php notification handling

I'm using the https://github.com/davidtsadler/ebay-sdk-php, the unofficial PHP SDK for eBay.

I've been going around in circles with notifications, particularly 'FixedPriceTransaction' notifications. I've managed to subscribe to the notifications and send a request to ensure that the subscription has been correctly created.

Unfortunately I can't work out which method to use when the notification is sent by eBay to handle it.

Upvotes: 0

Views: 1796

Answers (1)

user2066189
user2066189

Reputation:

Full disclosure: I'm the developer of the eBay SDK

eBay's notification service is not something that is officially supported by the SDK as it's an area of the API that I'm not to familiar with, but I will try and answer your question as best as I can.

As I understand it there are two parts to the eBay's Platform Notifications.

  1. You inform eBay which user notifications you are interested in.
  2. eBay's Platform Notifications then asynchronously pushes the notifications to your delivery location (which will typically be a URL under a domain that you control.)

The first part should be possible with the SDK as it just involves sending a request to the SetNotificationPreferences operation. It sounds like that you have already worked out how to do this part but I've included the example below just on the off chance that it helps. I haven't tried the code below but it should give you some idea of what to do.

use \DTS\eBaySDK\Trading\Services;
use \DTS\eBaySDK\Trading\Types;

/**
 * Fill out $request according to your project needs.
 */
$request = new Types\SetNotificationPreferencesRequest();
$request->UserDeliveryPreferenceArray = new Types\NotificationEnableArrayType();
$notification = new Types\NotificationEnableType();
$notification->EventEnable = 'Enable';
$notification->EventType = 'FixedPriceTransaction';
$request->UserDeliveryPreferenceArray->NotificationEnable[] = $notification;

/**
 * Handle response according to your project needs.
 */ 
$response = $service->SetNotificationPreferences($request);
if ($response->Ack !== 'Failure') {

}

The second part may be possible with the SDK but this is one area that I don't have any experience with. As I understand it, eBay will send a POST HTTP request to a URL that you control. It is your responsibility to process the data that is included in the request and to respond with a standard HTTP status 200 OK. I assume that the POST data includes a SOAP body that can be accessed by the PHP script as a string. Inside the SOAP body should be the XML of the notification. As long as you have some way to obtain the actual XML string that eBay sends, you can use the XmlParser class. This class is what the SDK uses to convert the XML response from the API back into a PHP object. This means that you can also do the same.

<?php
require __DIR__.'/vendor/autoload.php';

use DTS\eBaySDK\Parser;

/**
 * This string is not a complete example of what eBay could send.
 * A full example can be found at http://developer.ebay.com/Devzone/guides/ebayfeatures/Notifications/Notif-EndOfAuction.html#Example
 * It assumes that eBay sends a POST request to your sever and 
 * that you can obtain the data as a string, E.g via $_POST[] or some other way.
 */
$soap = <<<EOF_S
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Header>
    <ebl:RequesterCredentials soapenv:mustUnderstand="0" xmlns:ns="urn:ebay:apis:eBLBaseComponents" xmlns:ebl="urn:ebay:apis:eBLBaseComponents">
      <ebl:NotificationSignature xmlns:ebl="urn:ebay:apis:eBLBaseComponents">1w5Fdyr9V9ofTq67etR0lA==</ebl:NotificationSignature>
    </ebl:RequesterCredentials>
  </soapenv:Header>
    <soapenv:Body>
        <GetItemTransactionsResponse xmlns="urn:ebay:apis:eBLBaseComponents">
            <Item>
                <ItemID>123456789</ItemID>
            </Item>
    </GetItemTransactionsResponse>
    </soapenv:Body>
</soapenv:Envelope>
EOF_S;

/** 
 * Very simple method of extracting the XML from the string.
 */
$matches = array();
preg_match('#<soapenv:Body>(.*?)</soapenv:Body>#s', $soap, $matches);

$xml = $matches[1];

/**
 * The parser requires the full namespace and classname of the object that will be built from the XML.
 */
$parser = new Parser\XmlParser('DTS\eBaySDK\Trading\Types\GetItemTransactionsResponseType');
/** 
 * Pass the XML and the parser will return a PHP object.
 */
$response = $parser->parse($xml);
/**
 * Use the object.
 */
echo $response->Item->ItemID;

Upvotes: 6

Related Questions