Home » Validating Woocommerce webhook using HMAC in PHP

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:  Top JavaScript Most Popular Frameworks List

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

Read Also:  Star Rating System using jQuery

Calculate the HMAC

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:  PHP access Global-Superglobals array Variables

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Convert and Loop through JSON with PHP and JavaScript Arrays/Objects | json to array php
  2. Read Also:  jQuery Ajax File upload with Percentage Progress bar using PHP MySQLi
  3. Read Also:  WordPress Remove Yellow BGBOX - donate
  4. Read Also:  PHP Convert stdclass object recursive
  5. Read Also:  Laravel Send Emails Mailgun setup Tutorial
  6. Read Also:  Form Input Bindings in vue.js
  7. Read Also:  Laravel 6 insert or update multiple records
  8. Read Also:  Star Rating System using jQuery
  9. Read Also:  WordPress Remove Yellow BGBOX - donate
  10. Read Also:  jQuery Ajax File upload with Percentage Progress bar using PHP MySQLi

Leave a Comment

Your email address will not be published. Required fields are marked *