Yii2 Stripe Webhook 测试:“[ERROR] 无法 Post”

Yii2 Stripe Webhook testing: "[ERROR] Failed to Post"

我在 Yii2 控制器中设置了一个基本的 Webhook 端点,只是为了测试连接:

class CreditCardController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                     'webhook' => ['post'],
                ],
            ],
        ];
    }

    public function beforeAction($action)
    {
        if ($action->id == 'webhook')
            $this->enableCsrfValidation = false;
        return parent::beforeAction($action);
    }

...我只想转储有效负载和 return HTTP 200(大致基于我遵循的示例 here

    public function actionWebhook()
    {
        $payload = file_get_contents('php://input');
        ob_start();
        var_dump($payload);
        error_log(ob_get_clean(), 4);

        echo json_encode(['status' => 'success']);
    }

我安装了 Stripe CLI 并将 Webhook 转发到我的本地测试服务器:

stripe listen -f https://testsite.office/credit-card/webhook

当我触发一个包含我正在收听的内容的事件时:

stripe trigger invoice.payment_succeeded

我收到这条消息:

[错误] 无法 POST:Post“https://testsite.office/credit-card/webhook”:超出上下文截止时间(Client.Timeout 在等待 headers)

如果我删除操作的 POST 规则并在浏览器中编码 URL,它工作正常。

有什么想法吗?

对于遇到同样问题的任何人(我在网上看不到很多 Stripe/Yii2 webhook 聊天),这对我有用。

我注意到 Yii 在 Apache error_log 中抛出了 HeadersAlreadySentException。所以我不得不将响应格式指定为 JSON:

return new Response([
    'format' => Response::FORMAT_JSON,
    'statusCode' => 200,
    'statusText' => 'Webhook Handled',
]);

可能不是这个问题的结果,但我也将访问负载更改为“Yii 方式”:

$payload = json_decode(Yii::$app->request->getRawBody(), true);