簡體   English   中英

Stripe webhook 測試返回 http 500 錯誤

[英]Stripe webhook test returns http 500 error

這是我的代碼:

<?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 依賴項? 如果是這樣,您是否包含來自作曲家的自動加載文件? 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();
}

200 響應的條帶 UI 中顯示的示例測試:

Stripe UI 中的 Stripe 200 響應示例顯示來自我的服務器的響應

條帶 UI 中顯示的 400 響應示例測試:

Stripe UI 中的 Stripe 400 響應示例顯示來自我的服務器的響應

我也很新,如果這有幫助,請告訴我:)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM