Skip to content

Instantly share code, notes, and snippets.

@steveneaston
Created September 15, 2015 17:21
Show Gist options
  • Save steveneaston/469191d0df7b17d9521e to your computer and use it in GitHub Desktop.
Save steveneaston/469191d0df7b17d9521e to your computer and use it in GitHub Desktop.

Revisions

  1. steveneaston created this gist Sep 15, 2015.
    35 changes: 35 additions & 0 deletions VerifyShopifyWebhook.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <?php

    namespace App\Http\Middleware;

    use Closure;

    class VerifyShopifyWebhook
    {
    /**
    * Handle an incoming request.
    *
    * @param \Illuminate\Http\Request $request
    * @param \Closure $next
    * @return mixed
    */
    public function handle($request, Closure $next)
    {
    if (! $this->verifyShopifySignature($request)) {
    abort(401);
    }

    return $next($request);
    }

    private function verifyShopifySignature($request)
    {
    return ($request->header('x-shopify-hmac-sha256') == $this->calculateHmac());
    }

    private function calculateHmac()
    {
    $data = file_get_contents('php://input');
    return base64_encode(hash_hmac('sha256', $data, env('SHOPIFY_APP_SECRET'), true));
    }
    }