PHP code example of brandlive / vtex-api

1. Go to this page and download the library: Download brandlive/vtex-api 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/ */

    

brandlive / vtex-api example snippets


use Vtex\Order\OrderClient;
use Vtex\Exception\VtexException;

$credentials = [
    'accountName' => 'myAccount',
    'environment' => 'vtexcommercestable',
    'appKey'      => 'your-app-key',
    'appToken'    => 'your-app-token',
];

$orderClient = new OrderClient([
    'credentials' => $credentials,
]);

try {
    // Example: Retrieve an order
    $order = $orderClient->getOrder([
        'pathParams' => [
            'orderId' => 'v999999-01',
        ],
    ]);

    print_r($order);
} catch (VtexException $e) {
    echo 'Error: ' . $e->getMessage();
}

use Vtex\Order\OrderClient;
use Vtex\Exception\VtexException;

$orderClient = new OrderClient([
    'credentials' => $credentials,
]);

try {
    $order = $orderClient->getOrder([
        'pathParams' => [
            'orderId' => 'v999999-01',
        ],
    ]);
    print_r($order);
} catch (VtexException $exception) {
    // Handle error
}

$orderClient = new OrderClient([
    'credentials' => $credentials,
]);

try {
    $response = $orderClient->retrievePaymentTransaction([
        'pathParams' => [
            'orderId' => 'v999999-01',
        ],
    ]);
    print_r($response);
} catch (VtexException $exception) {
    // Handle error
}

$success = true;
$orderClient = new OrderClient([
    'credentials' => $credentials,
]);

try {
    $orderClient->sendPaymentNotification([
        'pathParams' => [
            'orderId'   => 'v999999-01',
            'paymentId' => 'PAY-1234',
        ],
    ]);
} catch (VtexException $exception) {
    $success = false;
    // Handle error
}

$success = true;
$orderClient = new OrderClient([
    'credentials' => $credentials,
]);

try {
    $orderClient->startHandlingOrder([
        'pathParams' => [
            'orderId' => 'v999999-01',
        ],
    ]);
} catch (VtexException $exception) {
    $success = false;
    // Handle error
}

$orderClient = new OrderClient([
    'credentials' => $credentials,
]);

$body = [
    "invoiceNumber" => "123456",
    "invoiceValue"  => 100000,
    "issuanceDate"  => "2023-01-01T12:00:00Z",
    "invoiceUrl" => "",
    "invoiceKey" => "",
    "trackingNumber" => "",
    "trackingUrl" => "",
    "courier" => "",
    "items" => [
        [
            "id" =>  38,
            "price" =>  50000,
            "quantity" =>  2
        ],
        // More items
    ],
];

try {
    $response = $orderClient->orderInvoiceNotification([
        'pathParams' => [
            'orderId' => 'v999999-01',
        ],
        'body' => $body,
    ]);
    print_r($response);
} catch (VtexException $exception) {
    // Handle error
}

use Vtex\Checkout\CheckoutClient;

$checkoutClient = new CheckoutClient([
    'credentials' => $credentials,
]);

try {
    $cartInformation = $checkoutClient->getCartInformation([
        'pathParams' => [
            'orderFormId' => 'abcdef12345',
        ],
    ]);
    print_r($cartInformation);
} catch (VtexException $exception) {
    // Handle error
}

use Vtex\Catalog\CatalogClient;

$catalogClient = new CatalogClient([
    'credentials' => $credentials,
]);

try {
    $newProduct = $catalogClient->createProduct([
        'body' => [
            'Name'        => 'My Product',
            'DepartmentId' => 8,
            'RefId'       => 'SKU-123',
            'BrandId'     => 38,
            'CategoryId'  => 123,
            'IsActive'    => true,
            'Description' => 'Product description',
            'DescriptionShort' => 'Short product description',
            // Additional fields as needed by the VTEX docs
        ],
    ]);
    print_r($newProduct);
} catch (VtexException $exception) {
    // Handle error
}

try {
    // ...some client method
} catch (VtexException $exception) {
    // Log or handle the error
    echo 'VTEX Error: ' . $exception->getMessage();
}