Topics covering webhook creation & management, event handling, Pub/Sub, and Eventbridge, in Shopify apps.
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
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
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.
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
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
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.
oh interesting, ok ill try using webhooks in api.php instead of web.php. thank you, will get back to you on this
use route in web.php file. It will work Route::any('/webhook', 'WebHookController@index');
Thanks