首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >注册Shopify Webhooks - Laravel

注册Shopify Webhooks - Laravel
EN

Stack Overflow用户
提问于 2018-10-17 18:55:23
回答 1查看 1.8K关注 0票数 0

我正在创建一个shopify应用程序,目前,我想在我的应用程序中注册webhooks,以便如果创建了客户,则向管理员发送通知/短信。根据我的研究,它可以通过商店设置注册,但在这种情况下,我想通过应用程序注册它。但是没有足够的资源来完成这项工作..下面是我所拥有的,但当我创建客户时,我的sms不会发送给管理员。

我现在还能错过什么呢?

VerifyWebhook

代码语言:javascript
复制
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);   
    }

路由

代码语言:javascript
复制
 Route::post('webhook/shopify/customer-created', function(\Illuminate\Http\Request $request) {
    // Handle customer created and sms or notification
})->middleware('webhook');
EN

回答 1

Stack Overflow用户

发布于 2018-10-18 14:21:00

看起来您正在尝试验证webhook,而不是创建一个webhook。请按以下流程操作

使用简单请求创建一个webhook:

代码语言:javascript
复制
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

代码语言:javascript
复制
Route::get('/register-webhook', 'WebhooksController@registerCustomerWebhook')->name('customer');

Route::get('/webhooks/customer-created', 'WebhooksController@customerCreated')->name('customerCreated');

然后导入并使用它-->

代码语言:javascript
复制
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:

代码语言:javascript
复制
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');
    }
.
.
}

注意:

  1. 您的端点必须安装了正确的SSL证书
  2. 您需要尽快响应webhook。最好在后台完成任务。

如果有任何混淆,请告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52853233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档