之前一直无法下载成功, 原因是版本问题
composer.json文件中:
1.下载失败的版本
"jlevers/selling-partner-api": "^5.8"
2.下载成功的版本
"jlevers/selling-partner-api": "dev-main"
如何切换版本
1.手动修改composer.json为:
"jlevers/selling-partner-api": "dev-main"
2.更新composer
composer update jlevers/selling-partner-api
测试用例:
namespace app\admin\controller;
use app\common\controller\Backend;
use app\admin\model\Store as StoreModel;
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 Store extends Backend
{
protected $model = null;
protected $preExcludeFields = ['id', 'create_time', 'update_time'];
protected $quickSearchField = ['id', 'title'];
public function initialize()
{
parent::initialize();
$this->model = new \app\admin\model\Store;
}
public function getOrder()
{
$storeName = '店铺名';
$storeModel = new StoreModel();
$store = $storeModel->where("title", $storeName)->find()->toArray();
$conf = [
'access_key' => $store['spapi_amazon_access_key'],
'access_secret' => $store['spapi_amazon_access_secret'],
'role_arn' => $store['spapi_amazon_role_arn'],
'refresh_token' => $store['spapi_amazon_refresh_token'],
'client_id' => $store['spapi_amazon_client_id'],
'client_secret' => $store['spapi_amazon_client_secret'],
'endpoint' => $store['spapi_amazon_endpoint'],
'marketplaceId' => $store['api_marketplaceid'],
'asin' => '',
'order_id' => '',
];
switch ($conf['endpoint']) {
case 'NA':
$endpoint = Endpoint::NA;
break;
case 'EU':
$endpoint = Endpoint::EU;
break;
case 'FE':
$endpoint = Endpoint::FE;
break;
}
$connect = 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'],
]);
$list = $this->getOrders($connect, $conf, false, '2023-01-01 00:00:00');
}
// 2.获取订单列表
public function getOrders($connect, $conf, $is_fbm = true, $stime)
{
$apiInstance = new OrdersV0Api($connect);
// $apiInstance = new \SellingPartnerApi\Api\OrdersApi($connect);
$marketplace_ids = [$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';
}
// $data_elements = ['shippingAddress'];
// $fulfillment_channels = 'MFN';
$limit = 5;
$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);
dd($list);
foreach ($list['payload']['Orders'] as $item) {
echo $item['AmazonOrderId'] . PHP_EOL;
}
$list = $result['payload']['Orders'];
foreach ($list as $key=>$item) {
echo $item['AmazonOrderId'] . PHP_EOL;
$result = $apiInstance->getOrderItems($item['AmazonOrderId']);
$list[$key]['order_goods'] = $result['payload']['order_items'];
// 可以遍历商品
foreach ($list[$key]['order_goods'] as $k=>$v) {
//dd($v['item_tax']['currency_code']);
}
}
echo '<pre>';
var_dump(json_encode($list));
exit;
} catch (Exception $e) {
echo 'Exception when calling OrdersV0Api->getOrders: ', $e->getMessage(), PHP_EOL;
}
}
}