Stripe CLI webhook 测试返回 "invalid signature" 异常
Stripe CLI webhook test is returning an "invalid signature" exception
public function handleWebhook(Request $request) {
\Stripe\Stripe::setApiKey(env("STRIPE_SK"));
$payload = $request->json;
$sigHeader = $request->header("HTTP_STRIPE_SIGNATURE");
$endpointSecret = env("STRIPE_ENDPOINT_SECRET");
try {
$event = \Stripe\Webhook::constructEvent(
$payload,
$sigHeader,
$endpointSecret
);
} catch (\UnexpectedValueException $e) {
// invalid payload
http_response_code(400);
die;
} catch (\Stripe\Exception\SignatureVerificationException $e) {
// invalid signature
http_response_code(400);
die;
}
我正在使用 Stripe CLI 转发到我的本地服务器并触发事件。但是我得到上面代码中的第二个 http_response_code 返回。什么可能导致签名无效?
您确定负载应该是 $request->json
吗?您可能需要 $request->getContent()
来代替。这是我过去使用过的对我有用的代码。
try {
$signature = $request->header('Stripe-Signature');
$secret = config('services.stripe.webhook_secret');
Webhook::constructEvent($request->getContent(), $signature, $secret);
} catch (Exception $e) {
//
}
public function handleWebhook(Request $request) {
\Stripe\Stripe::setApiKey(env("STRIPE_SK"));
$payload = $request->json;
$sigHeader = $request->header("HTTP_STRIPE_SIGNATURE");
$endpointSecret = env("STRIPE_ENDPOINT_SECRET");
try {
$event = \Stripe\Webhook::constructEvent(
$payload,
$sigHeader,
$endpointSecret
);
} catch (\UnexpectedValueException $e) {
// invalid payload
http_response_code(400);
die;
} catch (\Stripe\Exception\SignatureVerificationException $e) {
// invalid signature
http_response_code(400);
die;
}
我正在使用 Stripe CLI 转发到我的本地服务器并触发事件。但是我得到上面代码中的第二个 http_response_code 返回。什么可能导致签名无效?
您确定负载应该是 $request->json
吗?您可能需要 $request->getContent()
来代替。这是我过去使用过的对我有用的代码。
try {
$signature = $request->header('Stripe-Signature');
$secret = config('services.stripe.webhook_secret');
Webhook::constructEvent($request->getContent(), $signature, $secret);
} catch (Exception $e) {
//
}