php - Stripe webhook 测试返回 http 500 错误

标签 php stripe-payments

这是我的代码:

<?php

$payload = @file_get_contents('php://input');
$event = null;

try {
    $event = \Stripe\Event::constructFrom(
        json_decode($payload, true)
    );
} catch(\UnexpectedValueException $e) {
    // Invalid payload
    http_response_code(400);
    exit();
}

// Handle the event
switch ($event->type) {
    case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object;
        $paymentintentid = $paymentIntent['id'];
        break;

    default:
        // Unexpected event type
        http_response_code(400);
        exit();
}
http_response_code(200);
?>

这基本上是这里的示例代码:https://stripe.com/docs/webhooks/build

我想做的是通过 $ paymentIntent['id']; 获取 PaymentIntent ID;

但是当我运行测试 webhook 时,Stripe 说它返回 Http 错误 500。

我对 PHP 的了解并不像他们的脚本需要的那么高,所以我不确定我是否丢失了任何数据或代码。看起来我可能需要包含一些文件,因为 Stripe 希望我使用他们自己的类?

这是 PaymentIntent 文档:https://stripe.com/docs/api/payment_intents/create

最佳答案

您是否使用 Composer 添加了 Stripe 依赖项?如果是这样,您是否包含来自 Composer 的自动加载文件? Stripe 类需要以某种方式加载或包含。这是 500 错误的一部分。

我的测试的完整工作代码:

<?php

require '../vendor/autoload.php'; \\ replace with the actual location of your autoload file

$payload = @file_get_contents('php://input');
$event = null;

try {
    $event = \Stripe\Event::constructFrom(
        json_decode($payload, true)
    );
} catch(\UnexpectedValueException $e) {
    // Invalid payload
    http_response_code(400);
    exit();
}

// Handle the event
switch ($event->type) {
    case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object;
        $paymentintentid = $paymentIntent['id']; \\ $paymentIntent->id works as well. 
        echo $paymentintentid;
        break;

    default:
        // Unexpected event type
        http_response_code(400);
        exit();
}
http_response_code(200);
?>

如果您想更进一步,我建议将响应发送回 Stripe,以便您在 UI 中测试时可以看到它们。

响应示例:

// Handle the event
switch ($event->type) {
    case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object;
        
        // Store the payment ID from the webhook
        $paymentintentid = $paymentIntent['id'];
        
        // Set array for successful response
        $response = array(
            'http_code' => 200,
            'payment_id' => $paymentintentid,
            'response' => 'webhook caught successfully'
        );

        // Set status code and echo back response
        http_response_code(200);
        echo json_encode($response, true);

        break;

    default:
        // Unexpected event type, set array for this case
        $response = array(
            'http_code' => 400,
            'response' => 'Unexpected event type'
        );

        // Set status code and echo back response
        http_response_code(400);
        echo json_encode($response, true);
        exit();
}

strip UI 中显示的 200 响应的示例测试:

Stripe 200 response sample in Stripe UI showing response from my server

strip UI 中显示的 400 响应的示例测试:

Stripe 400 response sample in Stripe UI showing response from my server

关于php - Stripe webhook 测试返回 http 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61332564/

相关文章:

php - 我如何否定这个 if/else 与 just if 的比较?

php - 抽象 try/catch PHP - strip 错误处理

api - 如何在 Stripe 中更改结帐表单的语言?

javascript - 单击按钮时的调用功能不起作用

php - 与 echo 一起使用时在表数据上放置一个超链接

javascript - 无法使用 Nodejs 将元数据添加到 strip 客户订阅创建中?

java - 如何使用 Java(Android) 在 Stripe 中为卡充值

php - 在 PHP 中执行 Curl 以进行 Stripe 订阅

php - 基于输入的正则表达式创建

php - 将多个维度传递给Google Analytics API