Assistance Needed with Webhook Integration in Laravel

Assistance Needed with Webhook Integration in Laravel

MeriemSh
Shopify Partner
1 0 0

Dear Shopify Support Team,

I hope this message finds you well. I'm reaching out to seek assistance regarding an issue I've encountered with webhook integration in my Shopify store.

I have implemented a webhook in my Laravel application to receive notifications for new order creations (orders/create). However, despite my best efforts, I'm facing challenges with the webhook setup and processing.

Here's a summary of the problem and steps I've taken to troubleshoot:

Webhook Registration: I've registered the webhook with the correct endpoint URL (myDomain/webhook) through the Shopify API using Laravel's rest() method.
Using this code :
//////////////////////////////////////////////////////////////////////////////////////////////////////////
$response = $shop->api()->rest('POST', '/admin/api/2024-01/webhooks.json', [
"webhook" => [
"topic" => "orders/create",
"address" => "myDomain/webhook",
"format" => "json"
]
]);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
HMAC Validation: I've implemented HMAC validation in my Laravel application to verify the integrity of incoming webhook requests. However, despite correctly calculating the HMAC and comparing it with the header value (X-Shopify-Hmac-Sha256), the validation fails.

Logging and Error Handling: I've implemented logging within my Laravel application to track incoming webhook requests and any errors encountered during processing. From the logs, I can see that HMAC validation fails, but the reason for the failure is not apparent.

This is my code of handling the webhook :
//////////////////////////////////////////////////////////////////////////////////////////////////////////
<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class WebhookController extends Controller
{
public function handleShopifyWebhook(Request $request)
{
// Verify webhook integrity
$hmacHeader = request()->header('X-Shopify-Hmac-Sha256');
$requestBody = file_get_contents('php://input');
$calculatedHmac = base64_encode(hash_hmac('sha256', $requestBody, env('SHOPIFY_WEBHOOK_SECRET'), true));

if ($calculatedHmac !== $hmacHeader) {
Log::error('Webhook HMAC validation failed');
return response('HMAC validation failed', 401);
}

// Process the webhook payload
$payload = json_decode($requestBody, true);
Log::info('Received webhook:', $payload);

// Process the order creation here

return response('Webhook received successfully', 200);
}

}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

Testing with Sample Data: I've sent test orders to my Shopify store to trigger the orders/create webhook, but the webhook requests are not being successfully processed by my Laravel application.

Review of Documentation: I've thoroughly reviewed the Shopify API documentation for webhook integration, including recommendations for security, payload structure, and error handling. However, I haven't been able to identify the root cause of the issue based on the documentation alone.

I would greatly appreciate any assistance or guidance you can provide to help resolve this issue. If there are any specific requirements or best practices I should follow for webhook integration in Laravel, please let me know.

For your reference, I've included relevant snippets of code from my Laravel application below:


Thank you in advance for your help and support. Please let me know if you need any further information from my end to assist with troubleshooting.

Looking forward to your response.

Warm regards,

Reply 1 (1)

jamalali81
Shopify Partner
25 0 4

Hi @MeriemSh, did you resolve this issue?