PHP code example of highsidelabs / laravel-spapi

1. Go to this page and download the library: Download highsidelabs/laravel-spapi 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/ */

    

highsidelabs / laravel-spapi example snippets


use Illuminate\Http\JsonResponse;
use Saloon\Exceptions\Request\RequestException;
use SellingPartnerApi\Seller\SellerConnector;

class SpApiController extends Controller
{
    public function index(SellerConnector $connector): JsonResponse
    {
        try {
            $api = $connector->sellersV1();
            $result = $api->getMarketplaceParticipations();
            return response()->json($result->json());
        } catch (RequestException $e) {
            $response = $e->getResponse();
            return response()->json($response->json(), $e->getStatus());
        }
    }
}

use HighsideLabs\LaravelSpApi\Models\Credentials;
use HighsideLabs\LaravelSpApi\Models\Seller;

$seller = Seller::create(['name' => 'My Seller']);
$credentials = Credentials::create([
    'seller_id' => $seller->id,
    // You can find your selling partner ID/merchant ID by going to
    // https://<regional-seller-central-domain>/sw/AccountInfo/MerchantToken/step/MerchantToken
    'selling_partner_id' => '<AMAZON SELLER ID>',
    // Can be NA, EU, or FE
    'region' => 'NA',
    // The LWA client ID and client secret for the SP API application these credentials were created with
    'client_id' => 'amzn....',
    'client_secret' => 'fec9/aw....',
    // The LWA refresh token for this seller
    'refresh_token' => 'IWeB|....',
]);

use HighsideLabs\LaravelSpApi\Models\Credentials;
use Illuminate\Http\JsonResponse;
use Saloon\Exceptions\Request\RequestException;

$creds = Credentials::first();
/** @var SellingPartnerApi\Seller\SellersV1\Api $api */
$api = $creds->sellerConnector()->sellersV1();

try {
    $result = $api->getMarketplaceParticipations();
    $dto = $result->dto();
} catch (RequestException $e) {
    $responseBody = $e->getResponse()->json();
}

use HighsideLabs\LaravelSpApi\Models\Credentials;
use Illuminate\Http\JsonResponse;
use Saloon\Exceptions\Request\RequestException;

$creds = Credentials::first();
/** @var SellingPartnerApi\Vendor\DirectFulfillmentShippingV1\Api $api */
$api = $creds->vendorConnector()->directFulfillmentShippingV1();
bash
$ php artisan vendor:publish --tag="spapi-config"
bash
# Publish config/spapi.php file
$ php artisan vendor:publish --provider="HighsideLabs\LaravelSpApi\SellingPartnerApiServiceProvider"
bash
$ php artisan migrate
bash
$ php artisan vendor:publish --tag="spapi-database-cache"
$ php artisan migrate