Shopify webhooks and Laravel routes

Barry1
Shopify Expert
11 0 1

I am trying to recieve data from a webhook in Laravel.

When I fire it to a route in Laravel and add the verify code into the route nothing happens. 

Route::post('/webhook', function() {
   define('SHOPIFY_APP_SECRET', '*****************my_shopify_secret**********');

function verify_webhook($data, $hmac_header)
{
    $calculated_hmac = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET, true));
    return hash_equals($hmac_header, $calculated_hmac);
}

$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = verify_webhook($data, $hmac_header);
error_log('Webhook verified: '.var_export($verified, true)); //check error.log to see the result*/
});

 

When I create a php page in the laravel public directory and aim the webhook at that, it works fine.

<?php

define('SHOPIFY_APP_SECRET', '**********my_shopify_secret***********');

function verify_webhook($data, $hmac_header)
{
    $calculated_hmac = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET, true));
    return hash_equals($hmac_header, $calculated_hmac);
}

$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = verify_webhook($data, $hmac_header);
error_log('Webhook verified: '.var_export($verified, true)); //check error.log to see the result*/

?>

I have read that the webhook will not work with redirects, if so how do I use a webhook with Laravel.

I need to use Laravel to use the php wrapper to fire back the returned data from my app.

The working version does not validate but thats a different issue, I need the webhook fired in laravel to create my app

Any help greatly appreciated

Replies 7 (7)

Barry1
Shopify Expert
11 0 1

Finally got it working, I found if I redirected to the controller 

Route::post('/webhook', 'WebHookController@index');

and then put the code in the construct rather than the index 

it worked fine

Rob_Enwoven1
Tourist
3 0 1

Hi Barry,

 

I'm new to getting webhooks to work on my laravel application.

I created an order/create webhook via in the Admin. that route is in my web.php

```

Route::post('/shopify/webhooks', 'ShopifyController@index')

```

all it does is pretty much what you add in above, but what happens is it doesn't even hit my logs. I don't mind it fails but I would like to at least get a response that it hits my server. 

 

Any help of what you did to get it to work would be great.

AvidBrio
Shopify Expert
295 17 29

 

Laravel Router web.php

 

Route::post('/shopify-orders-webhook', [App\Http\Controllers\Admin\OrdersController::class, 'webhooks_recived_order']);

 

php laravel function 

  public function webhooks_recived_order(){

        $webhook_payload_cnt = file_get_contents('php://input');
        Log::info('Log message   Order webhooks recived order', ['context_url' => "shopify webhooks", 'context_response' => $webhook_payload_cnt]);
        
}

 

 

 

when test from shopify order creating but  not getting any log into log file.

Error 

POST /shopify-phorest-sync/shopify-phorest-sync/public/shopify-orders-webhook 419 unknown status    


Local URL map with ngrok other router all working fine.  for webhook only one not working 

If you find our comment helpful, hit the like button and accept it as a solution.
Want us to implement custom changes in your store? Contact us
Email me directly - jim@avidbrio.com

Richard_Gorbutt
New Member
6 0 0

Couple of questions, are you sending all your webhooks to /shopify/webhooks? Reason I ask is that I have each webhook going to its own route. For example,

Route::post('/v1/shopify/order_create', 'ApiController@orderCreate');

Secondly, I have all the API calls in the api.php routes file. I've disabled authentication for that as well. I'm wondering if you have an auth middleware getting in the way. That said, shouldn't be an issue in web.php, I only moved it to that routes file to seperate it from the other logic.

Just some thoughts that may or may not help. Hopefully?

Regards, Richard
 

Richard_Gorbutt
New Member
6 0 0

Just dawned on me, as you're posting to the main web.php it will be expecting the csrf token. Does the log file in /storage/logs show any thing? You can disable that or move it to the api.php. The Laravel docs on routing may help here

When I get a webhook I throw it into a queued job and return 200 immmediately. 

Rob_Enwoven1
Tourist
3 0 1

oh interesting, ok ill try using webhooks in api.php instead of web.php. thank you, will get back to you on this

worldhook_Admin
Shopify Partner
4 0 1
use route in web.php file. It will work

Route::any('/webhook', 'WebHookController@index');

 

Thanks