我正在创建一个shopify应用程序,目前,我想在我的应用程序中注册webhooks,以便如果创建了客户,则向管理员发送通知/短信。根据我的研究,它可以通过商店设置注册,但在这种情况下,我想通过应用程序注册它。但是没有足够的资源来完成这项工作..下面是我所拥有的,但当我创建客户时,我的sms不会发送给管理员。
我现在还能错过什么呢?
VerifyWebhook
public function handle($request, Closure $next)
{
$hmac = request()->header('x-shopify-hmac-sha256') ?: '';
$shop = request()->header('x-shopify-shop-domain');
$data = request()->getContent();
// From https://help.shopify.com/api/getting-started/webhooks#verify-webhook
$hmacLocal = base64_encode(hash_hmac('sha256', $data, env('SHOPIFY_SECRET'), true));
if (!hash_equals($hmac, $hmacLocal) || empty($shop)) {
// Issue with HMAC or missing shop header
abort(401, 'Invalid webhook signature');
}
return $next($request);
}
路由
Route::post('webhook/shopify/customer-created', function(\Illuminate\Http\Request $request) {
// Handle customer created and sms or notification
})->middleware('webhook');
发布于 2018-10-18 14:21:00
看起来您正在尝试验证webhook,而不是创建一个webhook。请按以下流程操作
使用简单请求创建一个webhook:
POST /admin/webhooks.json
{
"webhook": {
"topic": "customers/create",
"address": "https://whatever.hostname.com/",
"format": "json"
}
}
当然,您必须在头文件中传递shopify auth token。
如果你想使用包来简化这个过程,你可以使用:https://github.com/oseintow/laravel-shopify
你的routes.php/web.php
Route::get('/register-webhook', 'WebhooksController@registerCustomerWebhook')->name('customer');
Route::get('/webhooks/customer-created', 'WebhooksController@customerCreated')->name('customerCreated');
然后导入并使用它-->
use Oseintow\Shopify\Facades\Shopify;
.
.
// create a webhook
public function registerCustomerWebhook(...){
Shopify::setShopUrl($shopUrl)->setAccessToken($accessToken)->post("admin/webhooks.json", ['webhook' =>
['topic' => 'customers/create',
'address' => 'https://whatever.hostname.com/path',
'format' => 'json'
]
]);
。}
正在验证webhook:
public function customerCreated(...) {
if (Shopify::verifyWebHook($data, $hmacHeader)) {
// do your stuffs here in background
return response('Hello World', 200)
->header('Content-Type', 'text/plain');
} else {
return response('UnAuthorized', 401)
->header('Content-Type', 'text/plain');
}
.
.
}
注意:
如果有任何混淆,请告诉我。
https://stackoverflow.com/questions/52853233
复制相似问题