Validating Woocommerce webhook using HMAC in PHP

Today, We want to share with you Validating Woocommerce webhook using HMAC in PHP.In this post we will show you wordpress plugin require another plugin, hear for Woocommerce webhook not showing response we will give you demo and example for implement.In this post, we will learn about Send data from Woocommerce to Laravel via webhook with an example.

Validating Woocommerce webhook using HMAC in PHP

There are the Following The simple About Validating Woocommerce webhook using HMAC in PHP Full Information With Example and source code.

As I will cover this Post with live Working example to develop Verify Woocommerce web hook in PHP Laravel, so the How to Authenticate Webhook Requests is used for this example is following below.

Read Also:  Get the full URL in WordPress

setup Verify & Validating woocommerce webhooks

Get the HMAC value from request/header from the Woocommerce request, whatever the hmac value you want is called

Get the signature – your secret

Get the woocommerce URL, They should give you a code or some kind of ID and also a TIMESTAMP (this is important in your HMAC Calculation) in the request, You’d need to figure out this bit

Calculate the HMAC

Read Also:  WordPress AJAX URL GET POST Call PHP Function

encode the calculated HMAC

Check if the HMAC and Calculated HMAC Match, if they do continue

If they don’t stop processing

public function handle(Request $request, Closure $next)
    {
    
    $hmac = $request->get('hmac');

    $signature = Request::header('x-wc-webhook-signature');
    $woocommerceData = $request->get('woocomerceData');
    $get_hmac = hash_hmac('sha256', $woocommerceData, $secret, true);

    $get_hmac = base64_encode($get_hmac)

    if ($hmac == $get_hmac) {
        return $next($request);
    }
    else {
        return false;
    }
}

Verify Woocommerce web hook in PHP Laravel

public function handle($request, Closure $next)
{
    $wp_signature = Request::header('x-wc-webhook-signature');

    $payload = Request::getContent();
    $get_hmac = base64_encode(hash_hmac('sha256', $payload, env('WOOCOMMERCE_WEBHOOK_ITEM_UPDATED'), true));

    if($wp_signature != $get_hmac) {
        return false;
    }

    return $next($request);
}

PHP – Verify Woocommerce web hook in Laravel

VerifyWoocommerce.php

<?php

namespace App\Http\Middleware;

use Closure;
use Request;
use Log;

class VerifyWoocommerce
{

    public function handle($request, Closure $next)
    {
        $wp_signature = Request::header('x-wc-webhook-signature');
        $get_hmac = base64_encode(hash_hmac('sha256', $wp_signature, env('WOOCOMMERCE_WEBHOOK_ITEM_UPDATED'), true));

        Log::debug($wp_signature);
        Log::debug($get_hmac);
        return $next($request);
    }
}

Web Programming Tutorials Example with Demo

Read :

Read Also:  Create Sticky Navigation Bar Using CSS3 and Javascript

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Validating Woocommerce webhook using HMAC in PHP.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Summary
Review Date
Reviewed Item
Article
Author Rating
51star1star1star1star1star

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  WordPress HTTP API Remote Requests with wp_remote_get()
  2. Read Also:  Laravel Autocomplete text field suggestions search using Database
  3. Read Also:  Handling Arrays using jQuery Tutorial with Examples
  4. Read Also:  Show-Hide Password in TextBox with Button using jQuery
  5. Read Also:  install node on windows
  6. Read Also:  Laravel Multiple middleware from controller in constructor
  7. Read Also:  Check if the Uploaded File is an Image in Laravel 6
  8. Read Also:  Create Sticky Navigation Bar Using CSS3 and Javascript
  9. Read Also:  WordPress HTTP API Remote Requests with wp_remote_get()
  10. Read Also:  Show-Hide Password in TextBox with Button using jQuery
CLOSEX