Ad

Why Does My Second Function Always Return False? Also, Why Does Code In My If Statement Run Regardless?

SOLVED!! Sorry for wasting your time.

Problems: Second function "verify_webhook_2" always returns false. Code in if statement runs whether tests return true or not.

I copied and pasted the first function, then made (what I would think to be) appropriate changes, so I can verify webhooks coming from two different Shopify stores. I'm sure it's something simple that I am just oblivious to, as I'm still fairly new to all of this. If I change $verify to the secret for $verify2 then webhooks received from that shop will verify true.

And I cannot for the life of me understand why the code in the if statement runs even when both requirements test false. There's no way I can think of that either could prove true when the receiving a webhook from the shop related to the $verify2 secret. Probably a rookie mistake?

$verify = "xxxxsecretxxxx";
$verify2 = "xxxxsecretxxxx";

define('SHOPIFY_APP_SECRET', $verify);
define('SHOPIFY_APP_SECRET_2', $verify2);

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);
}

function verify_webhook_2($data, $hmac_header)
{
  $calculated_hmac_2 = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET_2, true));
  return hash_equals($hmac_header, $calculated_hmac_2);
}

$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = verify_webhook($data, $hmac_header);
$verified_2 = verify_webhook_2($data, $hmac_header);
error_log('Webhook verified: '.var_export($verified, true)); //check error.log to see the result

if ($verified == true || $verified_2 == true){
  header("HTTP/1.1 200 OK"); //respond with success
  http_response_code(201);  //respond with success
  file_put_contents('/var/www/html/temp/webhook.json', $data);


  $POST = json_decode(file_get_contents('/var/www/html/temp/webhook.json'), true);
  //$POST = $POST['id'];
  $report = "id: " . $POST['id'] . " - email: " . $POST['email'] . " - name: " . $POST['customer']['first_name'] . " " . $POST['customer']['last_name'] ;
}else{

}
Ad

Answer

Of course, right after posting the question, I realized my failure. I had only written to the error log for the first function's comparison, so when I kept seeing "webhook verified: false" in the error logs, I assumed that was regardless of the shop I was sending data from.

I added:

error_log('Webhook verified_2: '.var_export($verified_2, true)); //check error.log to see the result

just below the first error_log call, then added another error log into the else section of my if statement, and all is working correctly, and responding correctly. It was a lack of understanding on my part that led to me believing it was not working correctly, when in fact, everything was, but I was missing information.

Ad
source: stackoverflow.com
Ad