首页 > 解决方案 > Stripe Checkout PHP API 收到 500 内部服务器错误

问题描述

这是我的情况,我正在使用 Stripe PHP Api 实现自定义 Stripe Checkout。

我已经请求了一个使用 jquery 的帖子方法,就像这样 >

var handler = StripeCheckout.configure({
    key: 'pk_test_yGQM97VuEUdttuOOFQcyaPHW',
    image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
    locale: 'auto',
    token: function (token) {
        // You can access the token ID with `token.id`.
        // Get the token ID to your server-side code for use.
        $.post(
                'charge.php',
                {
                    sT: token.id,
                    sE: token.email
                }, function (data) {
                    console.log(data);
        }
        );
    }
});

var thePayment = document.getElementById('pay-amount');

if (thePayment) {
    thePayment.addEventListener('click', function (e) {
        var desc = $('#pay-amount').attr('data-desc');
        var amount = Number($('#pay-amount').attr('data-amount'));
        var email = $('#pay-amount').attr('data-email');
        // Open Checkout with further options:
        handler.open({
            name: 'Test',
            description: desc,
            amount: amount,
            email: email,
            allowRememberMe: false
        });
        e.preventDefault();
    });
}
// Close Checkout on page navigation:
window.addEventListener('popstate', function () {
    handler.close();
});

而PHP端就是这样一个>

require_once('html/includes/vendor/autoload.php');

$stripe = array(
    "secret_key" => "sk_test_nJxSc9Yw716tLBWTa9HHMxhj",
    "publishable_key" => "pk_test_yGQM97VuEUdttuOOFQcyaPHW"
);

$charge_reply = array();

\Stripe\Stripe::setApiKey($stripe['secret_key']);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $token = $_POST['sT'];
    $email = $_POST['sE'];

    $customer = \Stripe\Customer::create(array(
                'email' => $email,
                'source' => $token
    ));
    $charge = \Stripe\Charge::create(array(
                "amount" => 1000,
                "currency" => "usd",
                "source" => $customer->id,
                "email" => $email,
                "description" => "Example charge"
    ));

    $charge_reply[] = [
        'token' => $token,
        'email' => $email
    ];

    sendJson($charge_reply);
    return;
}

我还在 php 中启用了 curl、json、mbstring。但是在向 charge.php 请求 post 方法后接受的函数会打印POST http://example.com/charge.php 500 (Internal Server Error)在控制台日志中。

那么有什么办法可以解决这个问题吗?

标签: phpstripe-payments

解决方案


500(内部服务器错误)是您的代码中有问题,这意味着它们是致命错误。

要查找错误,您应该在页面顶部使用以下代码。

ini_set('display_errors',1);
error_reporting(E_ALL);

它将返回确切的错误,以便修复它。

注意:不要在生产环境中使用它来进行本地开发。


推荐阅读