<?php
// Example: Receiving a Webhook Notification

require 'FamGateway.php';

// 1. Initialize the Gateway with your API Key
$fam = new FamGateway("YOUR_API_KEY_HERE");

// 2. Read the raw POST data and the signature header
$rawPostData = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_FAMGATEWAY_SIGNATURE'] ?? '';

// 3. Verify the webhook
$data = $fam->verifyWebhook($rawPostData, $signature);

if ($data !== false) {
    // The webhook is authentic!
    $event = $data['event'];
    
    if ($event === 'payment.success') {
        $orderId = $data['data']['order_id'];
        $amount = $data['data']['amount'];
        $transactionId = $data['data']['transaction_id'];
        
        // TODO: Mark this order as PAID in your database!
        // Example: $db->query("UPDATE orders SET status='paid' WHERE id='$orderId'");
        
        http_response_code(200);
        echo "Webhook processed successfully.";
    } else {
        // Unknown event type
        http_response_code(200);
        echo "Event ignored.";
    }
} else {
    // The signature didn't match, this might be a fake request!
    http_response_code(403);
    echo "Invalid signature.";
}
?>
