首页 > 解决方案 > 我从用于 Stripe 事件的 webhook url 收到空白响应

问题描述

我从条纹 webhook url 得到空白响应。

我正在使用以下代码:

$json = file_get_contents('php://input'); 
$action = json_decode($json, true);

但是在$json数组中,我得到了空白响应。

标签: phpwordpressstripe-payments

解决方案


我假设你的意思是$action数组是空的。我的猜测是——而且我从来没有看过那里——请求的主体php://input不是 JSON。

我使用的代码与 Stripe 网站上的示例基本相同,对我来说效果很好:

    $payload = @file_get_contents('php://input');
    $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
    $event = null;
    try {
        $event = Webhook::constructEvent(
            $payload,
            $sig_header,
            $this->stripeApiKey
        );
    } catch (\UnexpectedValueException $e) {
        throw new BadRequestHttpException('Unexpected value error');
    } catch (SignatureVerification $e) {
        throw new BadRequestHttpException('Signature verification error');
    }

在此之后,您应该有一个有效的事件,$event并且可以使用$object = $event->data->object. 该对象将是什么类型取决于您收到 webhook 调用的事件。您可以使用 获取事件类型$event->type

(是的,我知道使用@是不好的做法,但我并不介意那里的失败,因为一切都与是否Webhook::constructEvent()有效有关。)


推荐阅读