hkonnet / laravel-shipstation
ShipStation API 包,适用于 Laravel 8+。
Requires
- guzzlehttp/guzzle: ^6.2
Requires (Dev)
- phpunit/phpunit: ^5.3
README
这是一个用于 Laravel 5.* 的 ShipStation API 简单 PHP 包装器。
安装
您可以通过 Composer 安装此包,在项目的 composer.json
文件中添加 campo/laravel-shipstation
包。
composer require hkonnet/laravel-shipstation
其次,将 LaravelShipStation 服务提供者添加到 config/app.php
文件中的 providers 数组。
LaravelShipStation\ShipStationServiceProvider::class
通过 Composer 安装后,您需要发布配置
php artisan vendor:publish hkonnet/laravel-shipstatio
这将创建 API 密钥和 API 密钥的配置文件,位于 config/shipstation.php
。您需要从 ShipStation 获取 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 = '');
获取具有 orderId 为 1 的订单的示例。
use Hkonnet\LaravelShipStation\ShipStation; $shipStation = new 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 时,这些模型将转换为数组。
创建要发货的新订单的示例
use Hkonnet\LaravelShipStation\ShipStation; $shipStation = new 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 的任何订单,而不是与查询精确匹配。如果您有两个订单 123 和 1234 在 ShipStation 中,并调用 $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 秒进行 40 次API调用,每 1.5 秒调用 1 次。默认情况下,LaravelShipStation 将通过在平均每 1.5 秒调用 1 次时暂停来防止任何调用被速率限制。
测试
可以使用 phpunit
运行测试。请注意,测试将在您的生产环境中创建订单、检查订单并删除订单。默认情况下,测试是禁用的。如果您想运行测试,请编辑 phpunit.xml
文件,将环境变量 SHIPSTATION_TESTING
设置为 true
并设置您的 API 密钥和密钥。
贡献
我们非常欢迎拉取请求!这是一个工作进展(WIP)。
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。