【发布时间】:2021-06-02 02:54:32
【问题描述】:
我按照 stripe 的建议设置了一个带有签名验证的 Stripe webhook:
<?php
logme("Secure connection: ".isSecure());
logme("I was called at:".time());
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_ff...');
$endpoint_secret = 'whsec_Ky...';
$payload = @file_get_contents('php://input');
if($payload) {
//request body is set
} else {
//request body is not set
exit();
}
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
// Invalid signature
http_response_code(400);
exit();
}
http_response_code(200);
// Handle the event
switch ($event->type) {
case 'event1':
// do something
break;
// ... handle other event types
default:
echo 'Received unknown event type ' . $event->type;
logme('Received unknown event type ' . $event->type);
}
function logme($msg){
$log_file = "error.txt";
// logging error message to given log file
error_log($msg."\n-\n", 3, $log_file);
}
function isSecure() {
return
(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|| $_SERVER['SERVER_PORT'] == 443;
}
在条纹仪表板上,我使用 https://example.org/path/to/webhook.php 创建了一个 webhook,但如果我在测试模式下触发 invoice.paid webhook,我会收到以下错误:
Test-Webhook-Error: 503
Invalid encoding: ISO-8859-1
有人熟悉这种错误吗?
更新
它似乎取决于触发的事件类型。例如。 plan.deleted 有效,而 payment_intent.succeeded 无效
【问题讨论】:
标签: encoding stripe-payments webhooks