highsidelabs/laravel-spapi
Composer 安装命令:
composer require highsidelabs/laravel-spapi
包简介
A Laravel wrapper for Amazon's Selling Partner API (via jlevers/selling-partner-api)
README 文档
README
Selling Partner API wrapper for Laravel
Simplify connecting to the Selling Partner API with Laravel. Uses jlevers/selling-partner-api under the hood.
Note
There is a lot of boilerplate involved in building a Selling Partner API application: setting up credential management, OAuth, infrastructure for handling feeds and reports, and more. I built an SP API Laravel starter kit that comes with all that functionality baked in, so you can focus on writing business logic. The starter kit uses this package, along with jlevers/selling-partner-api, to make developing SP API applications easier. Read the full documentation here.
Related packages
jlevers/selling-partner-api: A PHP library for Amazon's Selling Partner API.highsidelabs/laravel-spapiis a Laravel wrapper aroundjlevers/selling-partner-api.highsidelabs/walmart-api: A PHP library for Walmart's seller and supplier APIs, including the Marketplace, Drop Ship Vendor, Content Provider, and Warehouse Supplier APIs.highsidelabs/amazon-business-api: A PHP library for Amazon's Business API, with a near-identical interface tojlevers/selling-partner-api.
This package is developed and maintained by Highside Labs. If you need support integrating with Amazon's (or any other e-commerce platform's) APIs, we're happy to help! Shoot us an email at hi@highsidelabs.co. We'd love to hear from you :)
If you've found any of our packages useful, please consider becoming a Sponsor, or making a donation via the button below. We appreciate any and all support you can provide!
There is a more in-depth guide to using this package on our blog.
Installation
$ composer require highsidelabs/laravel-spapi
Table of Contents
Overview
This library has two modes:
- Single-seller mode, which you should use if you only plan to make requests to the Selling Partner API with a single set of credentials (most people fall into this category, so if you're not sure, this is probably you).
- Multi-seller mode, which makes it easy to make requests to the Selling Partner API from within Laravel when you have multiple sets of SP API credentials (for instance, if you operate multiple seller accounts, or operate one seller account in multiple regions).
Single-seller mode
Setup
- Publish the config file:
$ php artisan vendor:publish --tag="spapi-config"
- Add these environment variables to your
.env:
SPAPI_LWA_CLIENT_ID= SPAPI_LWA_CLIENT_SECRET= SPAPI_LWA_REFRESH_TOKEN= # Optional # SPAPI_ENDPOINT_REGION=
Set SPAPI_ENDPOINT_REGION to the region code for the endpoint you want to use (EU for Europe, FE for Far East, or NA for North America). The default is North America.
Usage
SellerConnector and VendorConnector can be type-hinted, and the connector classes can be used to create instances of all APIs supported by jlevers/selling-partner-api. This example assumes you have access to the Selling Partner Insights role in your SP API app configuration (so that you can call SellingPartnerApi\Seller\SellersV1\Api::getMarketplaceParticipations()), but the same principle applies to calling any other Selling Partner API endpoint.
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()); } } }
Multi-seller mode
Setup
- Publish the config file:
# Publish config/spapi.php file $ php artisan vendor:publish --provider="HighsideLabs\LaravelSpApi\SellingPartnerApiServiceProvider"
-
Change the
installation_typeinconfig/spapi.phptomulti. -
Publish the multi-seller migrations:
# Publish migrations to database/migrations/ $ php artisan vendor:publish --tag="spapi-multi-seller"
- Run the database migrations to set up the
spapi_sellersandspapi_credentialstables (corresponding to theHighsideLabs\LaravelSpApi\Models\SellerandHighsideLabs\LaravelSpApi\Models\Credentialsmodels, respectively):
$ php artisan migrate
Usage
First you'll need to create a Seller, and some Credentials for that seller. The Seller and Credentials models work just like any other Laravel model.
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|....', ]);
Note
client_id and client_secret are nullable. If you are authorizing multiple sellers on a single SP API application, they will all use the same client ID and client secret. If you leave client_id and client_secret empty, the library will try to load those values from the SPAPI_LWA_CLIENT_ID and SPAPI_LWA_CLIENT_SECRET environment variables that are used in single-seller mode. That means that the single-seller credentials can effectively be used as master credentials in multi-seller mode.
Once you have credentials in the database, you can use them to retrieve a SellerConnector instance, from which you can get an instance of any seller API:
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(); }
The same goes for a VendorConnector instance:
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();
Troubleshooting
If you encounter an error like String data, right truncated: 7 ERROR: value too long for type character varying(255), it's probably because you're using Laravel's database cache, which by default has a 255-character limit on cache keys and values. This library has a migration available to fix this:
$ php artisan vendor:publish --tag="spapi-database-cache"
$ php artisan migrate
highsidelabs/laravel-spapi 适用场景与选型建议
highsidelabs/laravel-spapi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41.55k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2022 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「ecommerce」 「amazon」 「sdk」 「wrapper」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 highsidelabs/laravel-spapi 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 highsidelabs/laravel-spapi 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 highsidelabs/laravel-spapi 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Nevogate Payment Gateway SDK
This package includes a script and fail2ban configuration that allows you to use fail2ban when utilizing AWS elastic load balancer (ELB) and an apache webserver.
Dealing with payments through the Egyptian payment gateway PayMob
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
统计信息
- 总下载量: 41.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 24
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2022-12-09