1. Go to this page and download the library: Download rasuvaeff/yii3-webhooks 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/ */
rasuvaeff / yii3-webhooks example snippets
use Rasuvaeff\Yii3Webhooks\HmacSha256Signer;
use Rasuvaeff\Yii3Webhooks\WebhookEndpoint;
use Rasuvaeff\Yii3Webhooks\WebhookEvent;
$signer = new HmacSha256Signer();
$endpoint = new WebhookEndpoint(
url: 'https://partner.example.com/webhook',
secret: 'whsec_...',
);
$event = WebhookEvent::create(
type: 'order.created',
payload: json_encode(['orderId' => 42]),
);
$timestamp = $clock->now()->getTimestamp();
$signature = $signer->sign(
payload: $event->getPayload(),
secret: $endpoint->getSecret(),
timestamp: $timestamp,
eventId: $event->getId(),
);
// Add to outgoing request:
// X-Webhook-Id: <event_id>
// X-Webhook-Signature: t=1717228800,v1=<hmac_hex>
$header = $signature->toHeaderValue();
final readonly class EventFactory
{
public function __construct(private ClockInterface $clock) {}
public function create(string $type, string $payload): WebhookEvent
{
return WebhookEvent::create(
type: $type,
payload: $payload,
occurredAt: $this->clock->now(),
id: Uuid::v7()->toRfc4122(), // symfony/uid or ramsey/uuid, your call
);
}
}
use Rasuvaeff\Yii3Webhooks\HmacSha256Signer;
use Rasuvaeff\Yii3Webhooks\WebhookSignature;
use Rasuvaeff\Yii3Webhooks\WebhookVerifier;
$verifier = new WebhookVerifier(
signer: new HmacSha256Signer(),
clock: $clock,
toleranceSeconds: 300,
);
$signature = WebhookSignature::fromHeaderValue(
$request->getHeaderLine('X-Webhook-Signature'),
);
$eventId = $request->getHeaderLine('X-Webhook-Id');
$valid = $verifier->verify(
payload: (string) $request->getBody(),
secret: 'whsec_...',
signature: $signature,
eventId: $eventId,
);
use Rasuvaeff\Yii3Webhooks\InMemoryNonceStorage;
use Rasuvaeff\Yii3Webhooks\ReplayGuard;
$guard = new ReplayGuard(new InMemoryNonceStorage());
// $eventId = $request->getHeaderLine('X-Webhook-Id');
if ($valid) {
$guard->accept($eventId); // throws RuntimeException if already seen
// process the webhook...
}
use Rasuvaeff\Yii3Webhooks\InMemoryDeliveryStorage;
use Rasuvaeff\Yii3Webhooks\WebhookDelivery;
use Rasuvaeff\Yii3Webhooks\WebhookRetryPolicy;
$storage = new InMemoryDeliveryStorage();
$policy = WebhookRetryPolicy::fixed(maxAttempts: 3, delaySeconds: 60);
// or: WebhookRetryPolicy::exponential(maxAttempts: 5, baseSeconds: 10, cap: 3600)
$delivery = WebhookDelivery::create(event: $event, endpoint: $endpoint);
$storage->save($delivery);
// After attempt:
$delivery = $delivery->withAttempt($clock->now(), error: 'Connection refused');
$storage->save($delivery);
if ($policy->isReadyForRetry($delivery, $clock->now())) {
// retry...
}
use Rasuvaeff\Yii3Webhooks\ClaimingDeliveryStorage;
$now = $clock->now();
if (!$storage instanceof ClaimingDeliveryStorage) {
// There is no safe fallback here. findPending() would hand this batch to
// every other worker as well, and each of them would POST it — so a
// storage that cannot claim is a configuration error for this worker, not
// a degraded mode. Run one worker (see "Tracking deliveries" above) or
// switch to a storage that claims.
throw new \RuntimeException(sprintf(
'%s cannot claim deliveries; running more than one worker on it delivers every webhook twice',
$storage::class,
));
}
$batch = $storage->claimReady(
now: $now,
readyThresholds: $policy->readyThresholds($now),
maxAttempts: $policy->getMaxAttempts(),
leaseSeconds: 300,
limit: 100,
);
foreach ($batch as $delivery) {
// ... deliver, then move it out of the claim:
// $storage->markDelivered($delivery->withAttempt($now));
// $storage->markFailed($delivery->withAttempt($now, error: $error));
// or, for a retryable failure:
// $storage->save($delivery->withAttempt($now, error: $error));
// $storage->releaseClaim($delivery);
}