PHP code example of olmed / databus-webhook-helpers

1. Go to this page and download the library: Download olmed/databus-webhook-helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

olmed / databus-webhook-helpers example snippets




lmed\DataBus\Webhooks\SecureWebhookHelper;

// Initialize with your encryption and HMAC keys (both must be 32 bytes)
$encryptionKey = 'your-32-byte-encryption-key!!';
$hmacKey = 'your-32-byte-hmac-secret-key!!';

$helper = new SecureWebhookHelper($encryptionKey, $hmacKey);

// Webhook data received from request
$guid = $_POST['guid'];
$webhookType = $_POST['webhookType'];
$encryptedPayload = $_POST['payload']; // Base64 encoded with IV prefix
$signature = $_POST['signature']; // HMAC-SHA256 signature

// Decrypt and verify
$decryptedJson = '';
$isValid = $helper->tryDecryptAndVerifyWithIvPrefix(
    $guid,
    $webhookType,
    $encryptedPayload,
    $signature,
    $decryptedJson
);

if ($isValid) {
    // Successfully decrypted and verified
    $webhookData = json_decode($decryptedJson, true);
    echo "Webhook verified successfully!\n";
    print_r($webhookData);
} else {
    // Failed to decrypt or verify signature
    http_response_code(401);
    echo "Invalid webhook signature or corrupted payload";
}



lmed\DataBus\Webhooks\SecureWebhookHelper;

// Your secret keys (store these securely, e.g., in environment variables)
$encryptionKey = getenv('WEBHOOK_ENCRYPTION_KEY');
$hmacKey = getenv('WEBHOOK_HMAC_KEY');

try {
    $helper = new SecureWebhookHelper($encryptionKey, $hmacKey);
    
    // Get POST data
    $postData = json_decode(file_get_contents('php://input'), true);
    
    $guid = $postData['guid'] ?? '';
    $webhookType = $postData['webhookType'] ?? '';
    $payload = $postData['payload'] ?? '';
    $signature = $postData['signature'] ?? '';
    
    $decryptedJson = '';
    $isValid = $helper->tryDecryptAndVerifyWithIvPrefix(
        $guid,
        $webhookType,
        $payload,
        $signature,
        $decryptedJson
    );
    
    if ($isValid) {
        $data = json_decode($decryptedJson, true);
        
        // Process your webhook data here
        processWebhook($webhookType, $data);
        
        http_response_code(200);
        echo json_encode(['status' => 'success']);
    } else {
        http_response_code(401);
        echo json_encode(['status' => 'error', 'message' => 'Invalid signature']);
    }
    
} catch (InvalidArgumentException $e) {
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => 'Configuration error']);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => 'Server error']);
}

function processWebhook($type, $data) {
    // Your webhook processing logic here
    error_log("Processing webhook type: {$type}");
}


// Generate encryption key
$encryptionKey = bin2hex(random_bytes(16)); // 32 characters
echo "Encryption Key: " . $encryptionKey . "\n";

// Generate HMAC key
$hmacKey = bin2hex(random_bytes(16)); // 32 characters
echo "HMAC Key: " . $hmacKey . "\n";

public function __construct(string $encryptionKey, string $hmacKey)

public function tryDecryptAndVerifyWithIvPrefix(
    string $guid,
    string $webhookType,
    string $base64PayloadWithIv,
    string $signature,
    string &$decryptedJson
): bool



use Olmed\DataBus\Webhooks\SecureWebhookHelper;

$helper = new SecureWebhookHelper(
    config('webhooks.encryption_key'),
    config('webhooks.hmac_key')
);
bash
composer dump-autoload