Newer
Older
gnexus-auth-client-php / examples / plain-php / webhook.php
@Eugene Sukhodolskiy Eugene Sukhodolskiy 12 hours ago 817 bytes Initial auth client package scaffold
<?php

declare(strict_types=1);

use GNexus\GAuth\Exception\WebhookVerificationException;

/** @var \GNexus\GAuth\Client\GAuthClient $client */
$client = require __DIR__ . '/bootstrap.php';

$rawBody = file_get_contents('php://input');
$secret = 'replace-with-client-webhook-secret';

if ($rawBody === false) {
    http_response_code(400);
    echo 'Missing request body.';
    exit;
}

try {
    $event = $client->verifyAndParseWebhook($rawBody, getallheaders(), $secret);
} catch (WebhookVerificationException $exception) {
    http_response_code(401);
    echo 'Invalid webhook signature.';
    exit;
}

http_response_code(202);
header('Content-Type: application/json');
echo json_encode([
    'accepted' => true,
    'event_type' => $event->eventType,
    'event_id' => $event->eventId,
], JSON_THROW_ON_ERROR);