php - Basic Stripe 集成无法加载支付页面

标签 php stripe-payments

我正在使用服务器快速入门示例将用户发送到 Stripe 进行付款

https://stripe.com/docs/payments/checkout/server

所需的 session ID 已返回,我将其发送到 Stripe。但是付款页面没有加载。

我已将我的 API key 换成 XXXXX。

设置 PHP 显示所有错误,但不存在。

三次检查我的代码是否与集成示例匹配(具有明显的必要修改)

检查了我的 Stripe 帐户。

在头部

<script src="https://js.stripe.com/v3/"></script>

在 PHP 中

require_once('stripe-php-6.31.5/init.php');

\Stripe\Stripe::setApiKey("pk_test_XXXXXXXXXXXXXXXXXXXXXXX");

$object = \Stripe\Checkout\Session::create([
        'success_url' => 'https://www.example.com/success',
        'cancel_url' => 'https://www.example.com/cancel',
        'payment_method_types' => ['card'],
        'line_items' => [[
        'amount' => 500,
        'currency' => 'gbp',
        'name' => 'T-shirt',
        'description' => 'Comfortable cotton t-shirt',
        'images' => ['https://www.example.com/t-shirt.png'],
        'quantity' => 1,
    ]]
]);

$session_id = $object->id;

if ($session_id) {

    echo "<script>
          var stripe = Stripe('pk_test_XXXXXXXXXXXXXXXXXXXXXXX');
          stripe.redirectToCheckout({
          sessionId: '{{" . $session_id . "}}'
          }).then(function (result) {
          });
          </script>";

} else {

    echo 'No Session ID!';

}

应该加载 Stripe 支付页面。

此外,一旦我有了 session_id,javascript 看起来像这样

stripe.redirectToCheckout({
                sessionId: 
'{{cs_KmeIFgWSfN5GW6tP2e5IQ0Vb9EA0q3pWGHZNoDfFKAdcc6kW7DwsM6dAbhQ30}}'
            }).then(function (result) {
                // If `redirectToCheckout` fails due to a browser or network
                // error, display the localized error message to your customer
                // using `result.error.message`.
            });

然后用户会看到这个页面

<!DOCTYPE html>
<html>
<head>
    <title>KP Balance and Purchase</title>
    <script src="https://js.stripe.com/v3/"></script>

    <script>
        <!--
        function checkout(session_id) {

            var stripe = Stripe('pk_test_yLz5iASFgRnotoAQc79miQGz');

            stripe.redirectToCheckout({
                sessionId: '{{cs_KmeIFgWSfN5GW6tP2e5IQ0Vb9EA0q3pWGHZNoDfFKAdcc6kW7DwsM6dAbhQ30}}'
            }).then(function (result) {
                // If `redirectToCheckout` fails due to a browser or network
                // error, display the localized error message to your customer
                // using `result.error.message`.
            });

            }

        //-->
</script>

</head>
<body>

<form>
    <input type="button" onclick="checkout()" value="Buy Now!" />
</form>

</body>
</html>

最佳答案

Additionally, the javascript once I have the session_id looks like this

stripe.redirectToCheckout({
                sessionId: '{{cs_KmeIFgWSfN5GW6tP2e5IQ0Vb9EA0q3…kW7DwsM6dAbhQ30}}'

{{CHECKOUT_SESSION_ID}} 通常用于在技术文档中表示“这是一个占位符”,但这并不意味着您应该在实际值中包含大括号。

你的 sessionId: '{{cs_KmeI…Q30}}' 实际上应该只是 sessionId: 'cs_KmeI…Q30' 在这里。

关于php - Basic Stripe 集成无法加载支付页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55741449/

相关文章:

php - 在带有复选框的下拉列表中选择多个项目

php - PHP 中的 UTC 偏移量

javascript - 在 Cordova 中使用 Stripe Checkout

ruby-on-rails - stripe中如何处理支付成功?

javascript - 如何在服务器上运行浏览器功能?

php - 多对多关系、IN 运算符和不正确结果的可能性

php - 如何隐藏页面的整个加载过程并向用户显示准备好的页面?

ios - Firebase 和 Stripe : Storing users credit card information

node.js - Stripe 不允许将一笔费用与一次性来源的客户关联起来

ruby-on-rails - 通过电子邮件访问 Paypal 账户