PHP code example of christianjbrown / ebay-sell-fulfillment-api-sdk

1. Go to this page and download the library: Download christianjbrown/ebay-sell-fulfillment-api-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?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));

$order = $orderApi->getOrder('03-06614-05610', OrderApiInterface::FIELD_GROUPS_TAX_BREAKDOWN);

printf("%s — %s\n", $order->getOrderId(), $order->getOrderFulfillmentStatus() ?? 'unknown');

$total = $order->getPricingSummary()?->getTotal();
if ($total !== null) {
    printf("  Total: %s %s\n", $total->getValue(), $total->getCurrency());
}

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');

$paymentDisputeApi = $sellFulfillment->getPaymentDisputeApi();

$summaries = $paymentDisputeApi->getPaymentDisputeSummaries();
foreach ($summaries->getPaymentDisputeSummaries() as $summary) {
    printf("%s — %s\n", $summary->getPaymentDisputeId(), $summary->getPaymentDisputeStatus() ?? 'unknown');
}

$dispute = $paymentDisputeApi->getPaymentDispute('5000005000');

use ChristianBrown\EBay\SellFulfillment\Api\PaymentDisputeEvidenceApiInterface;
use ChristianBrown\EBay\SellFulfillment\Model\AddEvidencePaymentDisputeRequest;
use ChristianBrown\EBay\SellFulfillment\Model\FileEvidence;

$evidenceApi = $sellFulfillment->getPaymentDisputeEvidenceApi();

$file = $evidenceApi->uploadEvidenceFile(
    '5000005000',
    'proof-of-delivery.png',
    PaymentDisputeEvidenceApiInterface::CONTENT_TYPE_PNG,
    file_get_contents('proof-of-delivery.png')
);

$request = (new AddEvidencePaymentDisputeRequest())
    ->setEvidenceType('PROOF_OF_DELIVERY')
    ->setFiles([(new FileEvidence())->setFileId($file->getFileId())]);

$evidence = $evidenceApi->addEvidence('5000005000', $request);

use ChristianBrown\EBay\SellFulfillment\Exception\ExceptionInterface;

try {
    $orders = $orderApi->getOrders();
} catch (ExceptionInterface $exception) {
    // Anything this library throws lands here.
}