PHP code example of malpka32 / inpost-buy-sdk

1. Go to this page and download the library: Download malpka32/inpost-buy-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/ */

    

malpka32 / inpost-buy-sdk example snippets




use malpka32\InPostBuySdk\Client\InPostBuyClient;
use malpka32\InPostBuySdk\Dto\Common\ListSort;
use malpka32\InPostBuySdk\Dto\Offer\OfferStatus;
use malpka32\InPostBuySdk\Dto\Order\OrderStatus;
use Symfony\Component\HttpClient\HttpClient;

$client = new InPostBuyClient(
    httpClient: HttpClient::create(),
    clientId: 'your-client-id',
    clientSecret: 'your-client-secret',
    organizationId: 'your-org-uuid',
    sandbox: true,  // use false for production
);

// Fetch categories
$categories = $client->getCategories();
foreach ($categories as $category) {
    echo $category->name . " (" . $category->id . ")\n";
}

// Fetch offers
$offers = $client->getOffers(offerStatus: [OfferStatus::PUBLISHED], limit: 20);

// Fetch orders
$orders = $client->getOrders(status: OrderStatus::CREATED, sort: [ListSort::CREATED_AT_DESC]);

use malpka32\InPostBuySdk\Config\Language;

// Polish (default)
$client = new InPostBuyClient(..., language: Language::Polish);

// English
$client = InPostBuyClient::createWithTokenProvider(..., language: Language::English);

$categories = $client->getCategories();

foreach ($categories as $node) {
    printf(
        "ID: %s | Name: %s | Parent: %s | Children: %d\n",
        $node->id,
        $node->name,
        $node->parentId ?? '-',
        count($node->children)
    );
}

$tree = $client->getCategories();  // one API call, returns CategoryTreeCollection

foreach ($tree as $root) {
    echo $root->name . "\n";
    foreach ($root->children as $child) {
        echo "  " . $child->name . "\n";
    }
}

use malpka32\InPostBuySdk\Client\InPostBuyClient;
use malpka32\InPostBuySdk\Dto\Offer\OfferDto;
use malpka32\InPostBuySdk\Dto\Offer\PriceDto;
use malpka32\InPostBuySdk\Dto\Offer\Product\DimensionDto;
use malpka32\InPostBuySdk\Dto\Offer\Product\ProductDto;
use malpka32\InPostBuySdk\Dto\Offer\StockDto;
use malpka32\InPostBuySdk\Collection\AttributeValueCollection;
use malpka32\InPostBuySdk\Dto\Attribute\AttributeValueDto;

$product = new ProductDto(
    name: 'Cool T-Shirt',
    description: 'Comfortable cotton t-shirt in various sizes.',
    brand: 'MyBrand',
    categoryId: '67909821-cc25-45ec-80ce-5ac4f2f01032',  // from getCategories()
    sku: 'TSHIRT-001',
    ean: '5901234567890',
    attributes: AttributeValueCollection::fromAttributes(
        new AttributeValueDto('attr-color-uuid', ['Red'], 'en'),
        new AttributeValueDto('attr-size-uuid', ['M', 'L'])
    ),
    dimension: new DimensionDto(width: 200, height: 50, length: 300, weight: 200)  // mm, g
);

$offer = new OfferDto(
    externalId: 'SKU-TSHIRT-001',
    product: $product,
    stock: new StockDto(quantity: 10, unit: 'UNIT'),
    price: new PriceDto(amount: 99.99, currency: 'PLN', taxRateInfo: '23%')
);

$result = $client->putOffer($offer);
echo "Created offer ID: {$result->offerId}\n";

use malpka32\InPostBuySdk\Collection\OfferCollection;

$offers = OfferCollection::fromOffers($offer1, $offer2, $offer3);
$ids = $client->putOffers($offers);

foreach ($ids as $id) {
    echo "Created: $id\n";
}

use malpka32\InPostBuySdk\Dto\Common\ListSort;
use malpka32\InPostBuySdk\Dto\Offer\OfferStatus;

$offers = $client->getOffers(
    offerStatus: [OfferStatus::PENDING, OfferStatus::PUBLISHED],
    limit: 50,
    offset: 0,
    sort: [ListSort::UPDATED_AT_DESC]
);

foreach ($offers as $offer) {
    echo $offer->externalId . " – " . $offer->product->name . "\n";
}

use malpka32\InPostBuySdk\Auth\PkceOAuth2Client;
use malpka32\InPostBuySdk\Auth\PkceTokenProvider;
use malpka32\InPostBuySdk\Client\InPostBuyClient;
use malpka32\InPostBuySdk\Config\InPostBuyEndpoints;
use malpka32\InPostBuySdk\Config\Language;

// 1. Initiate authorization – redirect merchant to $result['authorize_url']
$pkceClient = new PkceOAuth2Client($httpClient);
$result = $pkceClient->initiateAuthorization(
    redirectUri: 'https://your-shop.com/module/callback',
    clientId: $clientId,
    sandbox: true,
    stateStorage: $yourPkceStateStorage,  // implement PkceStateStorageInterface
);

// 2. On callback – exchange code for tokens
$tokens = $pkceClient->exchangeCodeForTokens(
    code: $_GET['code'],
    redirectUri: $redirectUri,
    clientId: $clientId,
    clientSecret: $clientSecret,
    state: $_GET['state'],
    tokenUrl: InPostBuyEndpoints::tokenUrl($sandbox),
    stateStorage: $yourPkceStateStorage,
    tokenStorage: $yourTokenStorage,  // implement TokenStorageInterface
);

// 3. Create client with token provider
$tokenProvider = new PkceTokenProvider(
    $yourTokenStorage,
    $pkceClient,
    $clientId,
    $clientSecret,
    InPostBuyEndpoints::tokenUrl($sandbox),
);

$client = InPostBuyClient::createWithTokenProvider(
    $httpClient,
    $tokenProvider,
    $organizationId,
    sandbox: true,
    language: Language::English,  // optional: pl (default) or en
);

use malpka32\InPostBuySdk\Dto\Common\ListSort;
use malpka32\InPostBuySdk\Dto\Order\OrderPaymentStatus;
use malpka32\InPostBuySdk\Dto\Order\OrderStatus;
use malpka32\InPostBuySdk\Dto\Order\OrderStatusDto;
use malpka32\InPostBuySdk\Dto\Order\OrderUpdateStatus;

// List orders (optionally filter by status/payment status/sort)
$orders = $client->getOrders(
    status: OrderStatus::CREATED,
    paymentStatus: OrderPaymentStatus::PAID,
    sort: [ListSort::CREATED_AT_DESC],
);

foreach ($orders as $order) {
    echo $order->inpostOrderId . " – " . ($order->reference ?? 'no ref') . "\n";
}

// Fetch single order
$order = $client->getOrder('order-uuid-from-inpost');
if ($order !== null) {
    var_dump($order->status, $order->orderLines);
}

// Accept order
$client->updateOrderStatus('order-uuid', new OrderStatusDto(status: OrderUpdateStatus::ACCEPTED));

// Refuse with reason
$client->updateOrderStatus('order-uuid', new OrderStatusDto(
    status: OrderUpdateStatus::REFUSED,
    comment: 'Out of stock'
));

use malpka32\InPostBuySdk\Exception\NotFoundException;
use malpka32\InPostBuySdk\Exception\ApiException;

try {
    $order = $client->getOrder('non-existent');
} catch (NotFoundException $e) {
    echo "Order not found: " . $e->getMessage();
} catch (ApiException $e) {
    echo "API error: " . $e->getStatusCode();
    if ($e->isRetryable()) {
        echo " – retry after " . ($e->getRetryAfterSeconds() ?? '?') . " seconds";
    }
}