Created
November 17, 2021 19:22
-
-
Save javimata/9fbfd293dcb8cc598e9c9b1a0481684d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Llamaría al SDK de Shopify si se requiere | |
*/ | |
// require "vendor/autoload.php"; | |
// Esta llave la genera Shopify si el webhook se genera desde el administrador | |
define('SHOPIFY_APP_SECRET', 'a9f210ba885cf3cef74fe4b562a1c037f2e9f5XXXXXXXX'); | |
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); | |
} | |
if ( isset( $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'] ) ) { | |
$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256']; | |
$data = file_get_contents('php://input'); | |
$verified = verify_webhook($data, $hmac_header); | |
// $action = $_GET["query"]; | |
if ( var_export($verified, true) ) { | |
$webhook_content = ''; | |
$webhook = fopen('php://input', 'rb'); | |
while(!feof($webhook)){ //loop through the input stream while the end of file is not reached | |
$webhook_content .= fread($webhook, 4096); //append the content on the current iteration | |
} | |
fclose($webhook); //close the resource | |
/* | |
if ( $action == "create_cart" ) { | |
$file_name = "create_cart.txt"; | |
} elseif ( $action = "stock" ) { | |
$file_name = "update_stock.txt"; | |
} | |
*/ | |
$file_name = "create_cart.txt"; | |
$myfile = fopen($file_name, "w") or die("Unable to open file!"); | |
fwrite($myfile, $webhook_content); | |
fclose($myfile); | |
// $datos = json_decode($webhook_content, true); //convert the json to array | |
// echo file_put_contents("test.txt",$webhook); | |
// error_log('Webhook verified: '.var_export($verified, true)); //check error.log to see the result | |
} else { | |
$file_name = "error_cart.txt"; | |
$myfile = fopen($file_name, "w") or die("Unable to open file!"); | |
$webhook_content = "Error: No se verifico el webhook"; | |
fwrite($myfile, $webhook_content); | |
fclose($myfile); | |
echo "No se verifico el webhook"; | |
} | |
} else { | |
echo "No viene de un webhook"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment