1. Go to this page and download the library: Download wiseadmin/wise-webhook 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/ */
wiseadmin / wise-webhook example snippets
use wise\webhook\server\WebhookServer;
use think\facade\Log;
$result = WebhookServer::create()
->url('https://hooks.slack.com/services/T000/B000/XXXX')
->payload(['text' => 'Order #123 paid!'])
->useSecret('my-signing-secret')
->onSending(function (string $url, array $payload) {
Log::info("Preparing to send to {$url}");
})
->onSent(function (string $url, string $response, int $durationMs) {
Log::info("Webhook sent to {$url} in {$durationMs}ms");
})
->onFailed(function (string $url, string $error, int $attempt) {
Log::error("Webhook failed to {$url} (attempt {$attempt}): {$error}");
})
->dispatch();
use wise\webhook\client\WebhookClient;
use think\Request;
class Webhook extends BaseController
{
public function receive(Request $request, string $endpoint = null)
{
$receiver = WebhookClient::receiver();
$result = $receiver->receive($request, $endpoint);
return json($result);
}
}
use wise\webhook\client\contract\WebhookProcessorInterface;
use wise\webhook\client\model\WebhookReceiveModel;
use think\Request;
use think\facade\Log;
class HandleStripeWebhook implements WebhookProcessorInterface
{
public function handle(WebhookReceiveModel $webhookCall, Request $request): void
{
$type = $webhookCall->payload['type'] ?? '';
match ($type) {
'payment_intent.succeeded' => $this->handlePaymentSucceeded($webhookCall),
'charge.refunded' => $this->handleRefunded($webhookCall),
default => Log::info("Unhandled Stripe event: {$type}"),
};
}
protected function handlePaymentSucceeded(WebhookReceiveModel $webhookCall): void
{
// 执行业务逻辑,例如更新订单为已支付
$data = $webhookCall->payload['data']['object'] ?? [];
Log::info('Payment succeeded', $data);
}
protected function handleRefunded(WebhookReceiveModel $webhookCall): void
{
// ...
}
}
use wise\webhook\server\contract\SignerInterface;
class MySigner implements SignerInterface
{
public function calculateSignature(string $payload, string $secret): string
{
return hash_hmac('sha512', $payload, $secret);
}
public function signatureHeaderName(): string
{
return 'X-My-Signature';
}
}