如何自动运行路线-Laravel

时间:2018-11-09 05:31:17

标签: php laravel

我为我的应用程序创建了我的网络挂钩,它们可以正常运行。但是我注意到,在我的网络钩子工作之前,我必须在注册它们之前通过在浏览器中运行路由来对其进行注册。

因此,如果我的路线是https://example.domain/order-create-webhook,则必须运行此路线,才能调用函数registerOrderCreateWebhook()在应用程序中注册我的Web挂钩,然后我才能开始从Shopify接收响应。

现在我的问题是,有没有一种方法可以在用户安装应用程序时自动运行此路由,这样我就不必从浏览器手动注册我的网络挂钩

注意:我已经添加了auth(),它正在为该应用程序注册/安装新用户

控制器

public function auth(Request $request)
{



 $shared_secret = env('SHARED_SECRET');
    $params = $_GET; 
    $shop_name = $request->shop;
    $shopUrl = $request->domain;
    $hmac = $request->hmac;
    $code = $request->code;
    $params = array_diff_key($params, array('hmac' => ''));
    ksort($params); 


    $computed_hmac = hash_hmac('sha256', http_build_query($params), $shared_secret);


        $api_key = "*****";
        $shared_secret = "****";
        $query = array(
        "client_id" => $api_key, // Your API key
        "client_secret" => $shared_secret, // Your app credentials (secret key)
        "code" => $request->code // Grab the access key from the URL
        );

        $shopUrl = $shop_name;
        $access_token_url = "https://" . $shopUrl . "/admin/oauth/access_token";


        // Store the access token
        $result = json_decode($result, true);
        $access_token = $result['access_token'];
        // dd($access_token);



    // Valicate Shopify's request
    if (hash_equals($hmac, $computed_hmac)) {
        if (! User::where('site', '=', $shop_name)->exists()) {

            // Create a new user account                 
            $new_user = new User;
            $new_user->site = $shop_name;
            $new_user->access_token = $access_token;
            $new_user->save();
        }

        // Get current user, login and redirect to dashboard
        $user = User::where('site', '=', $shop_name)->firstOrFail();
        Auth::login($user);
        return redirect('/home');

    } 
    else 
    {
        abort(401, 'Unauthorized action.');
    }
}

  public function registerOrderCreateWebhook()
            {
                    $shop = Auth::user()->site;
                    $token = Auth::user()->access_token;
                    $shopify = Shopify::setShopUrl($shop)->setAccessToken($token);
                    Shopify::setShopUrl($shop)->setAccessToken($token)->post("admin/webhooks.json", ['webhook' => 
                     ['topic' => 'orders/updated',
                     'address' => 'https://example.domain.com/order-update-webhook',
                     'format' => 'json'
                     ]
                    ]);
            }

路线

Route::get('order-create-webhook', Controller@registerOrderCreateWebhook);

1 个答案:

答案 0 :(得分:0)

当商人点击Shopify中的安装按钮并重定向回您的应用程序时,您应该首先获取AccessToken,将其存储在localDB中,然后创建webhooks。我正在使用粘贴我的代码的相同程序包来管理所有这些东西。

Basic Shopify App built with laravel

在商家点击Shopify中的安装按钮后调用身份验证功能的地方。