php Stripe CLI上的签名无效

xoshrz7s  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(145)

我正在本地环境中测试Stripe webhooks。我正在使用:

./stripe listen --forward-to http://localhost/cb/stripe
./stripe trigger payment_intent.succeeded

下面是我的webhook中的代码:

$stripe = new \Stripe\StripeClient(STRIPE_SECRET_KEY);

    // This is your Stripe CLI webhook secret for testing your endpoint locally.
    $endpoint_secret = 'whsec...';

    $payload = @file_get_contents('php://input');
    $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
    $event = null;

    try {
        $event = \Stripe\Webhook::constructEvent(
            $payload, $sig_header, $endpoint_secret
        );
    } catch(\UnexpectedValueException $e) {
        write_log('invalid payload');
        // Invalid payload
        http_response_code(400);
        exit();
    } catch(\Stripe\Exception\SignatureVerificationException $e) {
        // Invalid signature
        write_log('invalid signature');
        http_response_code(400);
        exit();
    }

    // Handle the event
    switch ($event->type) {
        case 'payment_intent.succeeded':
            $charge = $event->data->object;
            write_log($charge);
        case 'payment_intent.payment_failed':
            $charge = $event->data->object;
            write_log($charge);
        default:
            write_log('Received unknown event type ' . $event->type);
    }

    http_response_code(200);
}

当我运行我的触发器时,我会记录“无效签名”。

[400] POST http://localhost/cb/stripe [evt_3NzOP4Ivmq1M9dbl01GdMEIc]
[400] POST http://localhost/cb/stripe [evt_3NzOP4Ivmq1M9dbl0Q2mju6H]
[400] POST http://localhost/cb/stripe [evt_3NzOP4Ivmq1M9dbl0T2nZWRQ]
dvtswwa3

dvtswwa31#

当你在本地测试这些时,你必须使用通过命令行输出获得的Webhook Secret。因此,当您运行./stripe listen --forward-to http://localhost/cb/stripe时,它将给予一个Webhook Secret以插入代码。
它看起来像这样--> whsec_abc123

相关问题