zeeshanali / laravel-shipstation
ShipStation API 包 Laravel 10。
Requires
- guzzlehttp/guzzle: ^7.2
Requires (Dev)
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-09-08 16:51:00 UTC
README
为 Laravel 10.* 构建的 ShipStation 简单 PHP API 包装器。
安装
composer require zeeshanali/laravel-shipstation
其次,将 LaravelShipStation 服务提供者添加到位于 config/app.php
的提供者数组中
Zeeshan\LaravelShipStation\ShipStationServiceProvider::class,
通过 composer 安装后,您需要发布配置
php artisan vendor:publish --tag=shipstation
这将在 config/shipstation.php
创建 API 密钥和 API 密钥的配置文件。您需要从 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 = '');
获取订单 ID 为 1 的示例。
use rajarizwan2007\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 rajarizwan2007\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->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 次调用时暂停来防止任何调用被速率限制。
测试
可以使用 phpunit
运行测试。请注意,测试将在您的生产环境中创建订单、检查订单和删除订单。默认情况下,测试是禁用的。如果您想运行测试,请编辑 phpunit.xml
文件,将环境变量 SHIPSTATION_TESTING
设置为 true
并设置您的 API 密钥 & 密钥。
贡献
欢迎提交拉取请求!这是一个正在进行中的项目。
许可证
MIT 许可证(MIT)。更多信息请参阅许可证文件。