Webhook
Matscode\Paystack\Webhook verifies incoming Paystack webhook signatures, decodes payloads, exposes event and data accessors, generates idempotency keys, and sends JSON responses without framework dependencies.
Initialization
Pass a single secret string or an array of secrets as the first argument. Webhook classifies secrets automatically by prefix:
whsec_...: Signing secrets used to verifyPaystack-Signature(t=...,v1=...).sk_...: Integration secret key used for legacyX-Paystack-Signatureverification when enabled.
use Matscode\Paystack\Webhook;
// Single signing secret
$webhook = new Webhook('whsec_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
// Multiple signing secrets during secret rotation
$webhook = new Webhook([
'whsec_new_secret_here',
'whsec_old_secret_here',
]);
// Custom replay tolerance and optional legacy X-Paystack-Signature fallback
$webhook = new Webhook('sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', [
'toleranceSeconds' => 300,
'supportLegacySignature' => true,
]);
Options
| Option | Type | Default | Description |
|---|---|---|---|
toleranceSeconds | int | 300 | Maximum allowed age or drift between current server time and the t timestamp in Paystack-Signature. Set to 0 to disable the time check. |
supportLegacySignature | bool | false | Enables fallback verification for deprecated X-Paystack-Signature headers using an sk_... secret when no whsec_ secrets are configured. |
Handling Requests
Call handleRequest(?string $rawBody = null, ?array $headers = null) to read and verify the raw request body and signature headers, decode JSON into objects via Helpers::JSONStringToObj(), and return $this.
If $rawBody or $headers are omitted, Webhook reads the raw body from php://input and extracts signature headers from the current request. Passing explicit values is helpful in unit tests or custom entrypoints.
use Matscode\Paystack\Enums\WebhookEvent;
use Matscode\Paystack\Exceptions\InvalidSignatureException;
use Matscode\Paystack\Exceptions\InvalidWebhookPayloadException;
use Matscode\Paystack\Exceptions\JsonException;
use Matscode\Paystack\Webhook;
$webhook = new Webhook($_ENV['PAYSTACK_WEBHOOK_SECRET']);
try {
$webhook->handleRequest();
if ($webhook->event() === WebhookEvent::CHARGE_SUCCESS->value) {
$data = $webhook->data();
// Process $data->reference, $data->amount, $data->customer, etc.
}
return $webhook->success();
} catch (InvalidSignatureException $e) {
return $webhook->failure('Invalid webhook signature', 401);
} catch (InvalidWebhookPayloadException | JsonException $e) {
return $webhook->failure('Invalid webhook payload', 400);
}
Unknown or newly added Paystack event names are not rejected after signature verification succeeds. Your handler decides which events to process.
Payload and Event Accessors
After handleRequest() succeeds, call these methods on $webhook:
event(): string: Returns the event name string (for example,'charge.success').data(): stdClass: Returns the decodeddataobject from the payload.payload(): stdClass: Returns the full decoded top-level payload object.timestamp(): ?int: Returns the verified Unix timestamptfromPaystack-Signature, ornullwhen verified via legacyX-Paystack-Signature.rawBody(): string: Returns the exact raw request body string received.idempotencyKey(): string: Returns a deterministic key built from the event name and a SHA-256 hash of the event name and control-character-cleaned raw body.
Using WebhookEvent
Matscode\Paystack\Enums\WebhookEvent provides backed enum cases for Paystack webhook event names so you can avoid string typos in match or switch blocks:
use Matscode\Paystack\Enums\WebhookEvent;
match ($webhook->event()) {
WebhookEvent::CHARGE_SUCCESS->value => handleChargeSuccess($webhook->data()),
WebhookEvent::SUBSCRIPTION_DISABLE->value => handleSubscriptionDisable($webhook->data()),
WebhookEvent::TRANSFER_FAILED->value => handleTransferFailed($webhook->data()),
default => null,
};
Responses
Webhook provides two terminating helpers (success() and failure()) that set the HTTP status code and Content-Type: application/json header when headers have not already been sent, echo the JSON response, and return the JSON string:
// 200 OK by default
return $webhook->success();
// Custom message, status, extra fields, or headers
return $webhook->success('Event processed', 200, [
'idempotency_key' => $webhook->idempotencyKey(),
]);
// Failure response (400 by default)
return $webhook->failure('Invalid webhook signature', 401);