[SPAPI] 亚马逊SPAPI快速测试脚本

197 阅读1分钟

使用的包:

composer require jlevers/selling-partner-api
<?php

namespace App\Console\Commands\SPAPI;
use Illuminate\Console\Command;
use DB;
use SellingPartnerApi\Api\CatalogItemsV0Api;
use SellingPartnerApi\Configuration;
use SellingPartnerApi\Endpoint;
use SellingPartnerApi\Api\SellersV1Api as SellersApi;
use SellingPartnerApi\Api\OrdersV0Api;
use SellingPartnerApi\Api\TokensV20210301Api;
use SellingPartnerApi\Model\TokensV20210301\CreateRestrictedDataTokenRequest;

class ClousaleApi extends Command
{
    protected $signature = 'command:test';
    protected $description = 'API测试';

    protected $api;
    protected $conf;

    public $null =   [
        'access_key'    => '',  // 1.来自IAM控制台,用户-安全凭证
        'access_secret' => '',  // 2.来自IAM控制台,用户-安全凭证
        'role_arn'      => '',  // 3.来自IAM控制台, 创建角色获得
        'refresh_token' => '',  // 4.来自亚马逊后台,用户权限-开发人员凭证-添加APP授权获得
        'client_id'     => '',  // 5.来自亚马逊后台,同上, 点击查看LWA凭证获得
        'client_secret' => '',  // 6.来自亚马逊后台,同上, 点击查看LWA凭证获得,每隔一定时间更新
        
        'endpoint'      => '',  // 地区,NA北美,EU:欧洲, FE:远东(日本新加坡) 
        'marketplaceId' => '',  // 市场ID,百度查询
        'asin'          => '',  // 商品唯一标识
        'order_id'      => '',  // 订单号
    ];


    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $conf = $this->null;
        $this->conf = $conf;
        switch ($conf['endpoint']) {
            case 'NA':
                $endpoint = Endpoint::NA;
                break;
            case 'EU':
                $endpoint = Endpoint::EU;
                break;
            case 'FE':
                $endpoint = Endpoint::FE;
                break;
        }

        $config = new Configuration([
            "lwaClientId" => $conf['client_id'],
            "lwaClientSecret" => $conf['client_secret'],
            "lwaRefreshToken" => $conf['refresh_token'],
            "awsAccessKeyId" => $conf['access_key'],
            "awsSecretAccessKey" => $conf['access_secret'],
            "endpoint" => $endpoint,
            "roleArn" => $conf['role_arn'],
        ]);

        #$this->getOrderByOrderSn($config); // 获取订单信息
        #$this->getAsin($config); //获取asin信息 
        $this->getOrders($config); // 获取订单列表, 第二次参数false表示获取自发货
    }


    // 1/获取单个订单信息
    public function getOrderByOrderSn($config, $is_fbm = true)
    {
        $apiInstance = new OrdersV0Api($config);
        $order_id = $this->conf['order_id'];
        $data_elements = null;
        if ($is_fbm) {
            $data_elements = ['buyerInfo', 'shippingAddress']; // 自发货, 获取PII才有的信息
        }

        try {
            $result = $apiInstance->getOrder($order_id, $data_elements);
            $result = json_decode($result, true);

        } catch (Exception $e) {
            echo 'Exception when calling OrdersV0Api->getOrder: ', $e->getMessage(), PHP_EOL;
        }
    }


    // 2.获取订单列表
    public function getOrders($config, $is_fbm = true, $limit = 2, $stime = '2023-05-01 00:00:00')
    {
        $apiInstance = new OrdersV0Api($config);
        $marketplace_ids = [$this->conf['marketplaceId']]; 
        $format = "Y-m-d\TH:i:s.\\0\\0\\0\\Z";
        $created_after = gmdate($format, strtotime($stime));

        try {
            if ($is_fbm) {
                $data_elements = ['buyerInfo', 'shippingAddress'];
                $fulfillment_channels = 'MFN';
            } else {
                $data_elements = null;
                $fulfillment_channels = 'AFN';
            }
            
            $result = $apiInstance->getOrders($marketplace_ids, $created_after, null, null, null, null, $fulfillment_channels, null, null, null, $limit, null, null, null, null, null, null, $data_elements);
            
            $list = json_decode($result, true);
            foreach ($list['payload']['Orders'] as $item) {
                echo $item['AmazonOrderId'] . PHP_EOL;
            }
            dd($list);
        } catch (Exception $e) {
            echo 'Exception when calling OrdersV0Api->getOrders: ', $e->getMessage(), PHP_EOL;
        }
    }

    public function getAsin($config)
    {
        $apiInstance = new CatalogItemsV0Api($config);
        $marketplace_id = $this->conf['marketplaceId'];
        $result = $apiInstance->getCatalogItem($marketplace_id, $this->conf['asin']);
        $result = json_decode($result, true);
        dd($result);
    }

}