简体   繁体   English

stripe.redirectToCheckout:您必须提供 lineItems、items 或 sessionId 之一

[英]stripe.redirectToCheckout: You must provide one of lineItems, items, or sessionId

I've been integrating Stripe checout into my website for some weeks now.几个星期以来,我一直在将 Stripe checkout 集成到我的网站中。 The content will be dynamic with the name/description & price set during the checkout process.内容将随着结帐过程中设置的名称/描述和价格而动态变化。

I am currently running the following:我目前正在运行以下内容:

Create-checkout-session.php创建结帐会话.php

<?php
require('../config.php');
require_once('../includes/stripe-php/init.php');

// Set your secret key. Remember to switch to your live secret key in production!
// See your keys here: https://dashboard.stripe.com/account/apikeys
 \Stripe\Stripe::setApiKey('sk_test_lF...');

 $checkout_session = \Stripe\Checkout\Session::create([
 'payment_method_types' => ['card'],
 'line_items' => [[
'name' => 'T-shirt',
'description' => 'Description of item',
'images' => ['https://example.com/t-shirt.png'],
'amount' => 500,
'currency' => 'gbp',
'quantity' => 1,
]],
'success_url' => 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => 'https://example.com/cancel',
]);


?>

The checkout session ID can be echo'd onto the main page using结帐 session ID 可以回显到主页上使用

    <?php echo json_encode($checkout_session['id']) ?>

Within the script I have buyingPhoto button that triggers this在脚本中我有触发这个的 buyingPhoto 按钮

$( document ).ready(function() {
$('#buyingPhoto').on('click', function() {

    var stripe = Stripe('pk_test_CX...');
         console.log("Can a button be depressed?")
            stripe.redirectToCheckout({
                // Make the id field from the Checkout Session creation API response
                // available to this file, so you can provide it as parameter here
                // instead of the {{CHECKOUT_SESSION_ID}} placeholder.
                sessionId: document.$checkout_session
            }).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`.
            });
});
});

However I get the reference error.但是我得到了参考错误。 Except for changing the domain for success/cancel what am I missing please?除了更改域以获得成功/取消之外,我还缺少什么?

For your code to work client-side, you need to pass the Checkout Session id cs_test_123456 client-side in the sessionId parameter.为了让您的代码在客户端工作,您需要在sessionId参数中传递 Checkout Session id cs_test_123456客户端。

At the moment, your code is passing sessionId: document.$checkout_session where this variable is likely not initialized.目前,您的代码正在传递sessionId: document.$checkout_session ,其中此变量可能未初始化。 My guess is that you are trying to access the PHP variable in Javascript which doesn't work that way as they are separate environments.我的猜测是您正在尝试访问 Javascript 中的 PHP 变量,因为它们是不同的环境,所以这种方式不起作用。 Instead, you need to fix your JS code to properly get the value in the PHP variable.相反,您需要修复 JS 代码以正确获取 PHP 变量中的值。

If you are mixing JS and PHP in one file the easiest would be to do this:如果你在一个文件中混合 JS 和 PHP 最简单的方法是这样做:

sessionId: '<?php echo $checkout_session->id; >?',

If you are not and have separated PHP and JS, then you need to find a way to load the value from the PHP variable, the same way you would load other information from your server.如果你不是并且已经将 PHP 和 JS 分开,那么你需要找到一种方法来从 PHP 变量中加载值,就像你从服务器加载其他信息一样。

it is because you should only fire this code when you create the session with the php code.这是因为您应该只在使用 php 代码创建 session 时触发此代码。 So if you move your code to the page where/when you make a session then you won't get the error!!因此,如果您将代码移动到您创建 session 的页面,那么您将不会收到错误!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM