bryanyeh/shopify-laravel

使用 Guzzle 的 Shopify API for Laravel 5.4

1.10 2017-09-02 02:40 UTC

This package is not auto-updated.

Last update: 2024-09-23 22:02:19 UTC


README

此包仅供 Laravel 与 Shopify API 集成,用于创建公开应用

Latest Stable Version Total Downloads License

要求

  • Laravel 5.4
  • PHP 7+

安装

使用命令行

composer require bryanyeh/shopify-laravel 1.10

编辑您的 .env 文件,添加并替换,不要使用 < >

SHOPIFY_API_KEY=<replace with api key>
SHOPIFY_API_SECRET=<replace with api secret>
SHOIFY_API_SCOPES=<replace with scopes seperated by commas like: read_products,write_products,read_draft_orders>

config/app.php 中添加 providers 数组

Bryanyeh\Shopify\Providers\ShopifyServiceProvider::class,

config/app.php 中添加 aliases 数组

'Shopify' => Bryanyeh\Shopify\Facades\Shopify::class,

app/http/Kernel.php 中添加 routeMiddleware 数组

'shopifyrequest' => \Bryanyeh\Shopify\Middleware\VerifyRequest::class,
'shopifynonce' => \Bryanyeh\Shopify\Middleware\VerifyNonce::class,
'shopifywebhook' => \Bryanyeh\Shopify\Middleware\VerifyWebHook::class,
'shopifyproxy' => \Bryanyeh\Shopify\Middleware\VerifyProxy::class,

在命令行中

php artisan vendor:publish --provider="Bryanyeh\Shopify\Providers\ShopifyServiceProvider" 

用法

请确保在计划使用此包的地方使用 use Bryanyeh\Shopify\Facades\Shopify;

请求安装应用的权限

请确保使用中间件 shopifyrequest

use Bryanyeh\Shopify\Facades\Shopify;

Route::get('install', function(Request $request){
    return redirect(Shopify::init($request->input('shop'))->install('https://example.com/confirm'));
})->middleware('shopifyrequest');

获取访问令牌

为此,您需要使用中间件 shopifynonceshopifyrequest

use Bryanyeh\Shopify\Facades\Shopify;

Route::get('confirm', function(Request $request){
    $access_token = Shopify::init($request->input('shop'))->getAccessToken($request->input('code'))['access_token'];

    //save the access_token for later use

    //redirect to success or billing page

})->middleware('shopifynonce', 'shopifyrequest');

验证 webhooks

只需使用中间件 shopifywebhook

验证代理请求

只需使用中间件 shopifyproxy

访问 API 资源

use Bryanyeh\Shopify\Facades\Shopify;

$geturi = '/admin/products/count.json' ;
$posturi = '/admin/products.json';
$puturi = '/admin/products/632910392.json';
$deleteuri = '/admin/products/632910392.json';

$shop = Shopify::init($my_shopify_store,$access_token);
$shop->get($geturi, [if any]);
$shop->post($posturi, [data]);
$shop->put($puturi, [data]);
$shop->delete($deleteuri);

所有获取的内容都将以数组的形式包含

statusCode   :  200 or 400 or status code
reasonPhrase :  just a phrase that goes with the status code
callLimit    :  HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT
????         :  expected response data shopify returns

中间件

  • shopifywebhook 当 hmac 无效时返回 401 错误
  • shopifyproxy 当签名或域无效时返回 401 错误
  • shopifynonceshopifyrequest 将重定向到名为 re-auth 的路由,这样您可以决定当 nonce 或请求无效时该做什么

错误

此包只抛出一个异常。

  • InvalidMethodRequestException

注意

  • nonce 会自动为您处理
  • 请确保将您的传入 webhook 路由添加到 app/Http/Middleware/VerifyCsrfToken.php 中的 except 数组

许可证

Laravel Shopify 在 MIT 许可证 (MIT) 下授权。