clarity-tech / laravel-shopify
Laravel Shopify 包
Requires
- php: >=7.3.0|8.0
- laravel/framework: ^7.0|^8.0
- osiset/basic-shopify-api: ^10.0
- psr/http-message: ^1.0
Suggests
- guzzlehttp/guzzle: Must Required to use the HTTP Client.
README
Laravel Shopify 是一个简单的包,可以帮助您构建强大的 Shopify 集成。
安装
将包添加到 composer.json
composer require clarity-tech/laravel-shopify
Laravel 5.5+
包自动发现将为您设置别名和外观
Laravel 5.4 <
将服务提供者添加到 config/app.php 中的 providers 数组。
<?php 'providers' => [ ... ClarityTech\Shopify\ShopifyServiceProvider::class, ],
设置外观别名
<?php 'aliases' => [ ... 'Shopify' => ClarityTech\Shopify\Facades\Shopify::class, ],
设置 Shopify 应用认证
更新 config/app.php 并在路由中添加 auth:shopify
作为中间件以下代码
'guards' => [ ... 'shopify' => [ 'driver' => 'shopify-auth', 'provider' => 'shops', ], ], 'providers' => [ ... 'shops' => [ 'driver' => 'eloquent', 'model' => App\Models\Shop::class, ] ],
设置凭证
在您的 .env
文件中设置以下值从您的应用
SHOPIFY_APIKEY=您的-api-密钥
SHOPIFY_SECRET=您的-密钥
SHOPIFY_VERSION=admin-api-version
如果应用是私有的
API_PASSWORD=private-app-password
可选配置(发布)
Laravel Shopify 需要 api 密钥配置。您需要发布配置资产
php artisan vendor:publish --tag=shopify-config
这将在配置目录中创建一个 shopify.php 文件。您需要设置您的 API_KEY 和 SECRET
'key' => env("SHOPIFY_APIKEY", null),
'secret' => env("SHOPIFY_SECRET", null)
用法
前端
使用 Shopify 前端 SDK 获取 Shopify 会话令牌,并在每个请求中添加 Shopify-Token
标头或发送初始 URL 参数附加 Shopify
后端
要安装/集成商店,您需要通过 Shopify API 初始化 OAuth 认证,这需要三个组件。
它们是
1. Shop URL (eg. example.myshopify.com)
2. Scope (eg. write_products, read_orders, etc)
3. Redirect URL (eg. http://mydomain.com/authorize)
此过程将使我们能够获得商店的访问令牌
use ClarityTech\Shopify\Facades\Shopify; Route::get("shop/install", function(\Illuminate\Http\Request $request) { //$redirectUrl = route('shop.authorize'); $redirectUrl = "http://mydomain.com/shop/authorize"; $myShopifyDomain = $request->shop; $scope = ["write_products","read_orders"]; $authorizeUrl = Shopify::getAuthorizeUrl($myShopifyDomain, $scopes, $redirectUrl); return redirect()->to($authorizeUrl); });
让我们获取访问令牌
Route::get("process_oauth_result",function(\Illuminate\Http\Request $request) { $shopifyApi = resolve('shopify'); $myShopifyDomain = $request->shop; $code = $request->code; $token = $shopifyApi ->setShopDomain($myShopifyDomain) ->getAccessToken($code); //this gets access token from shopify and set it to the current instance which will be passed in further api calls dd($accessToken); //store the access token for future api calls on behalf of the shop // redirect to success page or billing etc. });
为了使代码更简洁,我们添加了一个应用卸载作业,可以通过 Shopify 的应用卸载 webhook 订阅,该 webhook 可以从您的商店自动配置
安装后,调度此作业
SubscribeAppUninstalledWebhookJob::dispatch($shop);
它将订阅 app/uninstalled
webhook 在 /webhooks/shopify/uninstalled
路由下,并将
验证请求(hmac)
use ClarityTech\Shopify\Facades\Shopify; public function verifyRequest(Request $request) { $params = $request->all(); if (Shopify::verifyRequest($params)){ logger("verification passed"); }else{ logger("verification failed"); } }
验证 webhook(hmac)
use ClarityTech\Shopify\Facades\Shopify; public function verifyWebhook(Request $request) { $data = $request->getContent(); $hmacHeader = $request->header('x-shopify-hmac-sha256'); if (Shopify::verifyWebHook($data, $hmacHeader)) { logger("verification passed"); } else { logger("verification failed"); } }
使用以下方式访问 Admin API
$myshopify_domain = "example.myshopify.com"; $access_token = "xxxxxxxxxxxxxxxxxxxxx"; $api = Shopify::setShop($myshopify_domain, $access_token)->basicApi(); // For sync rest api $res = $api->rest('GET', '/admin/shop.json') // For sync graphql api $res = $api->graph('{ products(first: 1) { edges { node { handle, id } } } }', [])
使用以下方式访问 API 资源
Shopify::get("resource uri", ["query string params"]); Shopify::post("resource uri", ["post body"]); Shopify::put("resource uri", ["put body"]); Shopify::delete("resource uri");
让我们使用我们的访问令牌从 Shopify 获取产品。
注意:您可以使用此方法访问 Shopify 上的任何资源(无论是产品、商店、订单等)
use ClarityTech\Shopify\Facades\Shopify; $shopUrl = "example.myshopify.com"; $accessToken = "xxxxxxxxxxxxxxxxxxxxx"; //retrieve from your storage(db) $products = Shopify::setShop($myShopifyDomain, $accessToken)->get("admin/products.json");
传递查询参数
// returns Collection Shopify::setShop($myShopifyDomain, $accessToken); $products = Shopify::get('admin/products.json', ["limit"=>20, "page" => 1]);
控制器示例
如果您像我一样更喜欢使用依赖注入而不是外观,则可以注入类
use Illuminate\Http\Request; use ClarityTech\Shopify\Shopify; class Foo { protected $shopify; public function __construct(Shopify $shopify) { $this->shopify = $shopify; } /* * returns products */ public function getProducts(Request $request) { $accessToken = 'xxxxxxxxxxxxxxxxxxxxx';//retrieve from your storage(db) $products = $this->shopify->setShop($request->shop, $accessToken) ->get('admin/products.json'); dump($products); } }
杂项
获取响应头
Shopify::getHeaders();
获取特定头
Shopify::getHeader("Content-Type");
检查是否存在头
if(Shopify::hasHeader("Content-Type")){ echo "Yes header exist"; }
获取响应状态码或状态消息
Shopify::getStatusCode(); // 200 Shopify::getReasonPhrase(); // ok
可选功能
我们还提供了一个用于验证 webhook 的中间件,您可以直接使用名为 verify.webhook
的中间件在您的 webhook 中使用它
我们还添加了一个自动应用卸载作业调度,当通过订阅 webhook 主题 app/uninstalled
从应用卸载时。要配置此,您需要在您的商店模型中实现接口 Shopify/Contracts/ShopifyShop
,然后
SubscribeAppUninstalledWebhookJob::dispatch($shop);
通过 php artisan vendor:publish --tag=shopify-jobs
发布 AppUninstalled 作业以自定义它
您可能不需要此功能。我们还会为 webhook 调度事件,如果它不是用于卸载主题的同一 webhook,则为 ClarityTech\Shopify\Events\ShopifyWebhookRecieved