<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
christianjbrown / ebay-sell-fulfillment-api-sdk example snippets
use ChristianBrown\EBay\SellFulfillment\Auth\ApplicationCredentials;
use ChristianBrown\EBay\SellFulfillment\Auth\CredentialsInterface;
use ChristianBrown\EBay\SellFulfillment\SellFulfillmentFactory;
use ChristianBrown\KeyValueStore\MemoryKeyValueStore;
use ChristianBrown\OAuth2Client\Lock\NullLock;
use Symfony\Component\Clock\NativeClock;
// Access token: transient, an in-memory (TTL-aware) store is fine.
$accessTokenStore = new MemoryKeyValueStore(new NativeClock());
// Refresh token: must persist and already hold a valid refresh token.
// Any KeyValueStoreInterface works (DatabaseKeyValueStore, GoogleSecretKeyValueStore, ...).
$refreshTokenStore = new MemoryKeyValueStore(new NativeClock());
$refreshTokenStore->setValue('your-seed-refresh-token');
$sellFulfillment = (new SellFulfillmentFactory())->create(
new ApplicationCredentials('your-app-id', 'your-cert-id', CredentialsInterface::MARKETPLACE_ID_EBAY_GB),
$accessTokenStore,
$refreshTokenStore,
new NullLock()
);
$orderApi = $sellFulfillment->getOrderApi(); // OrderApiInterface
use ChristianBrown\EBay\SellFulfillment\Http\ApiHost;
$sandboxHost = new ApiHost(
'https://api.sandbox.ebay.com',
'https://apiz.sandbox.ebay.com',
'https://api.sandbox.ebay.com/identity/v1/oauth2/token'
);
$sellFulfillment = (new SellFulfillmentFactory())->createForHost(
new ApplicationCredentials('your-sandbox-app-id', 'your-sandbox-cert-id', CredentialsInterface::MARKETPLACE_ID_EBAY_GB),
$accessTokenStore,
$refreshTokenStore,
new NullLock(),
$sandboxHost
);
// Before (1.x)
$sellFulfillment = new SellFulfillment('app-id', 'cert-id', CredentialsInterface::MARKETPLACE_ID_EBAY_GB, $accessTokenStore, $refreshTokenStore, $lock, $apiHost);
// After (2.0)
$credentials = new ApplicationCredentials('app-id', 'cert-id', CredentialsInterface::MARKETPLACE_ID_EBAY_GB);
$factory = new SellFulfillmentFactory();
$sellFulfillment = $factory->create($credentials, $accessTokenStore, $refreshTokenStore, $lock); // $lock is
use ChristianBrown\EBay\SellFulfillment\Api\OrderApiInterface;
use ChristianBrown\EBay\SellFulfillment\Filter\OrderFilter;
// eBay retains roughly two years of order history; a lower bound earlier than
// OrderFilterInterface::MAX_HISTORY_YEARS ago returns nothing extra.
$filter = (new OrderFilter())
->setCreationDateFrom(new DateTimeImmutable('-2 years'));
$soldByLegacyItemId = [];
$offset = 0;
do {
$page = $orderApi->getOrders($filter->toFilterString(), null, null, OrderApiInterface::MAX_LIMIT, $offset);
foreach ($page->getOrders() as $order) {
foreach ($order->getLineItems() as $lineItem) {
$legacyItemId = $lineItem->getLegacyItemId();
if ($legacyItemId === null) {
continue;
}
$soldByLegacyItemId[$legacyItemId] ??= 0;
$soldByLegacyItemId[$legacyItemId] += $lineItem->getQuantity() ?? 0;
}
}
$offset += OrderApiInterface::MAX_LIMIT;
} while ($offset < ($page->getTotal() ?? 0));
use ChristianBrown\EBay\SellFulfillment\Model\LineItemReference;
use ChristianBrown\EBay\SellFulfillment\Model\ShippingFulfillmentDetails;
$shippingFulfillmentApi = $sellFulfillment->getShippingFulfillmentApi();
$details = (new ShippingFulfillmentDetails())
->setLineItems([(new LineItemReference())->setLineItemId('1234567890')->setQuantity(1)])
->setShippingCarrierCode('ROYALMAIL')
->setTrackingNumber('AB123456789GB');
// eBay answers 201 with the new id in the Location header and an empty body, so
// nothing is returned; read the fulfillment back to see it.
$shippingFulfillmentApi->createShippingFulfillment('03-06614-05610', $details);
$fulfillments = $shippingFulfillmentApi->getShippingFulfillments('03-06614-05610');