aerni / snipcart-api
Snipcart API 的 Laravel 封装
v1.2.0
2023-05-19 21:51 UTC
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.5
- illuminate/support: ^10.0
Requires (Dev)
- nunomaduro/collision: ^7.0
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
README
此包使您在 Laravel 应用程序中与 Snipcart API 一起工作变得非常简单。
安装
使用 Composer 安装此包。
composer require aerni/snipcart-api
在您的 .env
中设置您的 Snipcart Live Secret
和 Test Secret
。您可以在Snipcart 控制台中找到它们。
SNIPCART_LIVE_SECRET=******************************** SNIPCART_TEST_SECRET=********************************
您还可以发布此包的配置。
php artisan vendor:publish --provider="Aerni\SnipcartApi\SnipcartApiServiceProvider"
以下配置将被发布到 config/snipcart-api.php
。
<?php return [ /* |-------------------------------------------------------------------------- | Snipcart API Keys |-------------------------------------------------------------------------- | | Your secret Snipcart API Keys for the Live and Test Environment. | */ 'live_secret' => env('SNIPCART_LIVE_SECRET'), 'test_secret' => env('SNIPCART_TEST_SECRET'), /* |-------------------------------------------------------------------------- | Test Mode |-------------------------------------------------------------------------- | | Set this to 'false' to authenticate using the 'live_secret'. | You probably want to do this in production only. | */ 'test_mode' => env('SNIPCART_TEST_MODE', true), ];
使用示例
在文件顶部导入此包。以下所有示例都使用了外观。
use Aerni\SnipcartApi\Facades\SnipcartApi;
SnipcartApi
接口由四个部分组成。从定义 HTTP 方法到将请求发送到 Snipcart。
// 1. Set the HTTP method to be used for the API request. SnipcartApi::get() ... // 2. Call the main method for the request. SnipcartApi::get()->product('product_id') ... // 3. Add optional parameter methods. SnipcartApi::get()->product('product_id')->limit(10)->offset(10) ... // 4. Send the request off to Snipcart. SnipcartApi::get()->product('product_id')->limit(10)->offset(10)->send();
通过 ID 获取 Snipcart 产品。
$product = SnipcartApi::get()->product('product_id')->send();
所有响应都封装在Laravel 集合中,以便于使用。
$product->get('stock');
Snipcart API 参考
产品
// Get all products. SnipcartApi::get()->products()->send(); // Post all products found on the URL. SnipcartApi::post()->products('fetch_url')->send(); // Get a product by ID. SnipcartApi::get()->product('product_id')->send(); // Update a product by ID. This requires additional parameter methods. SnipcartApi::put()->product('product_id')->send(); // Delete a product by ID. SnipcartApi::delete()->product('product_id')->send();
订单
// Get all orders. SnipcartApi::get()->orders()->send(); // Get an order by token. SnipcartApi::get()->order('order_token')->send(); // Update an order by token. This requires additional parameter methods. SnipcartApi::put()->order('order_token')->send();
通知
// Get all notifications of an order. SnipcartApi::get()->notifications('order_token')->send(); // Post a notification to an order. This requires additional parameter methods. SnipcartApi::post()->notification('order_token')->send();
退款
// Get all refunds of an order. SnipcartApi::get()->refunds('order_token')->send(); // Get a specific refund from an order. SnipcartApi::get()->refund('order_token', 'refund_id')->send(); // Post a refund to an order. This requires additional parameter methods. SnipcartApi::post()->refund('order_token')->send();
参数方法
使用此包提供的流畅接口将必需或可选参数传递给您的请求。一个常见的用例是为您的请求设置 limit
和 offset
。
SnipcartApi::get()->products()->limit(10)->offset(10)->send();
参数方法 API 参考
查阅Snipcart API 参考文档,以检查哪些参数可用以及哪些端点。
// The maximum number of items returned by the request. limit(int $limit); // The number of items that will be skipped. offset(int $offset); // The product ID defined by the user. userDefinedId(string $id); // The product ID defined by the user. productId(string $id); // Filter products to return those that have been bought from specified date. from(string $from); // Filter products to return those that have been bought until specified date. to(string $to); // The URL where we will find product details. fetchUrl(string $url); // Specifies how inventory should be tracked for this product. // Can be 'Single' or 'Variant. // Variant can be used when a product has some dropdown custom fields. inventoryManagementMethod(string $method); // Allows to set stock per product variant. variants(array $variants); // The number of items in stock. // Will be used when 'inventoryManagementMethod' is 'Single'. stock(int $stock = null); // If true a customer will be able to buy the product even if it's out of stock. // The stock level might be negative. // If false it will be impossible to buy the product. allowOutOfStockPurchases(bool $bool) // A status criteria for your order collection. // Possible values: InProgress, Processed, Disputed, Shipped, Delivered, Pending, Cancelled status(string $status) // The order payment status. // Possible values: Paid, Deferred, PaidDeferred, ChargedBack, Refunded, Paidout, // Failed, Pending, Expired, Cancelled, Open, Authorized. paymentStatus(string $status) // The invoice number of the order to retrieve. invoiceNumber(string $invoiceNumber) // The name of the person who made the purchase. placedBy(string $name) // Returns only the orders that are recurring or not. isRecurringOrder(bool $bool) // The tracking number associated to the order. trackingNumber(string $number) // The URL where the customer will be able to track its order. trackingUrl(string $url) // A simple array that can hold any data associated to this order. metadata(array $metadata) // The type of notification. // Possible values: Comment, OrderStatusChanged, OrderShipped, TrackingNumber, Invoice type(string $type) // The delivery method of the notification. // Possible values: Email, None deliveryMethod(string $method) // The message of the notification. // Possible values: Email, None message(string $message) // The amount of the refund. amount(string $amount) // The reason for the refund. comment(string $comment)
测试
按以下方式运行测试
vendor/bin/phpunit