campo / laravel-shipstation
Laravel 的 ShipStation API 封装器
Requires
- guzzlehttp/guzzle: ^6.5|^7.4
Requires (Dev)
- phpunit/phpunit: ^5.3
README
这是一个为 Laravel 构建的简单 PHP API 封装器,用于 ShipStation。
安装
可以通过在项目中的 composer.json
中要求 campo/laravel-shipstation
包来通过 Composer 安装此包。
{ "require": { "campo/laravel-shipstation": "^5.0" } }
然后在 Laravel 项目的根目录下运行
composer update
其次,将 LaravelShipStation 服务提供者添加到位于 config/app.php
中的提供者数组中
LaravelShipStation\ShipStationServiceProvider::class
通过 composer 安装后,您需要发布配置
php artisan vendor:publish
这将创建 API 密钥和 API 密钥的配置文件,位于 config/shipstation.php
。您需要从 ShipStation 获取您的 API & 密钥:[如何获取 ShipStation 的 API 访问权限?](https://help.shipstation.com/hc/en-us/articles/206638917-How-can-I-get-access-to-ShipStation-s-API-)
如果 ShipStation 提供了您合作伙伴 API 密钥,请将其设置在配置文件中。
依赖项
LaravelShipStation 使用 GuzzleHttp\Guzzle
端点
API 端点可以通过属性访问(例如,$shipStation->orders->get($options)
将对 /orders/{$options}
发送 GET 请求)。默认端点是 /orders/。有效的端点包括
- accounts
- carriers
- customers
- fulfillments
- orders
- products
- shipments
- stores
- users
- warehouses
- webhooks
方法
GET
$shipStation->{$endpoint}->get($options = [], $endpoint = '');
获取订单的示例,订单 ID(ShipStation 的内部订单 ID)为 123,以及通过订单号获取 ShipStation 的内部订单 ID。
$shipStation = $this->app['LaravelShipStation\ShipStation']; // Fetch an order by orderId == 123, orderId is defined by ShipStation $order = $shipStation->orders->get([], $endpoint = 123); // returns \stdClass // Fetch an orderId by the orderNumber, which may be user defined $order = $shipStation->orders->getOrderId('ORD-789'); // returns integer
POST
$shipStation->{$endpoint}->post($options = [], $endpoint = '');
第二个参数($endpoint)用于添加任何额外的端点。例如,要创建订单,POST 请求将发送到 /orders/createorder。“createorder” 是附加端点,因为我们指定根端点为属性:$shipstation->orders->post($options, 'createorders')
存在包含通过 API 可用所有属性的模型。这些模型将在传递到 API 时转换为数组。
创建新订单以进行发货的示例
$shipStation = $this->app['LaravelShipStation\ShipStation']; $address = new LaravelShipStation\Models\Address(); $address->name = "Joe Campo"; $address->street1 = "123 Main St"; $address->city = "Cleveland"; $address->state = "OH"; $address->postalCode = "44127"; $address->country = "US"; $address->phone = "2165555555"; $item = new LaravelShipStation\Models\OrderItem(); $item->lineItemKey = '1'; $item->sku = '580123456'; $item->name = "Awesome sweater."; $item->quantity = '1'; $item->unitPrice = '29.99'; $item->warehouseLocation = 'Warehouse A'; $order = new LaravelShipStation\Models\Order(); $order->orderNumber = '1'; $order->orderDate = '2016-05-09'; $order->orderStatus = 'awaiting_shipment'; $order->amountPaid = '29.99'; $order->taxAmount = '0.00'; $order->shippingAmount = '0.00'; $order->internalNotes = 'A note about my order.'; $order->billTo = $address; $order->shipTo = $address; $order->items[] = $item; // This will var_dump the newly created order, and order should be wrapped in an array. var_dump($shipStation->orders->post($order, 'createorder')); // or with the helper: $shipStation->orders->create($order); would be the same.
DELETE
$shipStation->{$endpoint}->delete($resourceEndPoint);
按订单 ID 删除订单的示例
$shipStation->orders->delete($orderId);
UPDATE
$shipStation->{$endpoint}->update($query = [], $resourceEndPoint);
简单的包装助手
助手位于 /src/Helpers
,并按端点命名。目前只有 /orders 端点和 /shipments 端点的助手。我还会添加更多;请随时发送 PR 以包含您使用的任何助手。
通过订单号检查 ShipStation 中是否已存在订单
$orderExists = $shipStation->orders->existsByOrderNumber($orderNumber) // returns bool
注意:当使用 orderNumber 查询参数时,ShipStation 将返回包含搜索词的任何订单。例如,orderNumber = 1 将返回任何包含 1 的订单,而不是与查询完全匹配的精确匹配。如果您在 ShipStation 中有两个订单 123 和 1234,并调用 $shipStation->orders->get(['orderNumber' => 123]);您将返回两个订单。
检查有多少订单处于 awaiting_fulfillment
状态
$count = $shipStation->orders->awaitingShipmentCount(); // returns int
在 ShipStation 中创建订单
$newOrder = $shipStation->orders->create($order);
获取特定订单号的发货
$shipments = $shipStation->shipments->forOrderNumber($orderNumber);
ShipStation API 速率限制
ShipStation只允许每60秒(或每1.5秒1次)进行40次API调用。默认情况下,LaravelShipStation将防止任何调用超出限制,当平均每1.5秒超过1次调用时,将暂停。
一旦发出请求,可以使用以下方法访问当前速率限制的值
获取每窗口可以发送的最大请求数
// integer $shipStation->getMaxAllowedRequests()
获取当前窗口剩余可以发送的请求数
// integer $shipStation->getRemainingRequests()
获取下一个窗口开始前的剩余秒数
// integer $shipStation->getSecondsUntilReset()
检查请求是否正在被速率限制
// boolean $shipStation->isRateLimited()
测试
可以使用phpunit
运行测试。请注意,测试将在生产环境中创建订单、检查订单和删除订单。默认情况下,测试是禁用的。如果您想运行测试,请编辑phpunit.xml
文件,将环境变量SHIPSTATION_TESTING
设置为true
,并设置您的API密钥和密钥。
贡献
欢迎提交拉取请求!这是一个工作进展中(WIP)的项目。
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。