如何从Webhook响应中获取Order_ID-Shopify和Laravel

时间:2018-10-31 09:48:59

标签: php laravel shopify

我已经在shopify应用程序中注册了Webhooks,并且能够接收来自Webhook到我指定地址的回复。

但是我现在想做的是,当我创建订单并将响应发送到指定的地址时,我如何get the ID of the order created

我想在发送给客户的通知中加入order id吗?

PS:我对laravel和在shopify中创建应用程序不熟悉。

控制器

  public function orderCreateWebhook(Request $request)
    {              
        //get order_id here and send notification
        notification = "A new order with id [order_id] has been 
                       created";
        SendSMS(notification); 
    }

   public function registerOrderCreateWebhook(Request $request)
    {
            $shop = "example.myshopify.com";
            $token = "xxxxxxxxxxxxxxxxxxxxxx";
            $shopify = Shopify::setShopUrl($shop)->setAccessToken($token);
            Shopify::setShopUrl($shop)->setAccessToken($token)->post("admin/webhooks.json", ['webhook' => 
             ['topic' => 'orders/create',
             'address' => 'https://shop.domain.com/order-create-webhook',
             'format' => 'json'
             ]
            ]);
    }

网络

//register order create
Route::get('/register-order-create-webhook', 'AppController@registerOrderCreateWebhook');  
Route::post('order-create-webhook', 'AppController@orderCreateWebhook');

1 个答案:

答案 0 :(得分:0)

// get the content of request body    
$order = $request->getContent();

// decode json and covert to array
$order = json_decode($order, true);

// get the 'id' key which is your order id    
$order_id = $order['id'];