How to verify a webhook in PHP?

How to verify a webhook in PHP?

luismyanez
Shopify Partner
2 0 0
Quizás quisiste decir: 
no trae la informacion de hmac header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256']; , tampoco $data = file_get_contents('php://input'); alguien me puede ayudar?

# The Shopify app's API secret key, viewable from the Partner Dashboard. In a production environment, set the API secret key as an environment variable to prevent exposing it in code.
define('API_SECRET_KEY', 'my_api_secret_key');
function verify_webhook($data, $hmac_header)
{
$calculated_hmac = base64_encode(hash_hmac('sha256', $data, API_SECRET_KEY, true));
return hash_equals($hmac_header, $calculated_hmac);
}
$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = verify_webhook($data, $hmac_header);
error_log('Webhook verified: '.var_export($verified, true)); // Check error.log to see the result
if ($verified) {
# Process webhook payload
# ...
} else {
http_response_code(401);
}
?>

 

 

 

Replies 0 (0)