Created
September 20, 2023 12:41
-
-
Save md-riaz/f3f7ced5e6327aa493b9bc95bb9b43b3 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 | |
$secret = ''; // paste your Secret here | |
//fetch request body | |
$data = file_get_contents('php://input'); | |
$payload = $_SERVER["HTTP_HB_TIMESTAMP"] . $data; | |
$signature = hash_hmac('sha256', $payload, $secret); | |
//compare signature in header with the one computed above | |
if($signature !== $_SERVER["HTTP_HB_SIGNATURE"]) | |
die('invalid signature'); | |
// signature valid, verify timestamp | |
if($_SERVER["HTTP_HB_TIMESTAMP"] < time() - 60) | |
die('timestamp older than 60 sec'); | |
// Decode the JSON data in the request body | |
$payloadData = json_decode($data, true); | |
// Check if the JSON decoding was successful | |
if ($payloadData === null) { | |
die('Invalid JSON data in webhook payload'); | |
} | |
// Define the directory where you want to save the JSON files | |
$saveDirectory = 'webhook_files/'; | |
// Ensure the directory exists; create it if it doesn't | |
if (!file_exists($saveDirectory)) { | |
mkdir($saveDirectory, 0777, true); | |
} | |
// Extract the event name from the payload | |
$event = $payloadData['event']; | |
// Generate a timestamp | |
$timestamp = time(); // You can customize the timestamp format if needed | |
// Create the JSON file name with the event name and timestamp | |
$fileName = $event . '_' . $timestamp . '.json'; | |
// Encode the payload data back to JSON format | |
$jsonPayload = json_encode($payloadData, JSON_PRETTY_PRINT); | |
// Save the JSON data to the file | |
$filePath = $saveDirectory . $fileName; | |
file_put_contents($filePath, $jsonPayload); | |
// Output a success message | |
echo "JSON file saved as: $filePath"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment