![](/img/trans.png)
[英]Unhandled Rejection (TypeError): stripe.redirectToCheckout is not a function
[英]stripe.redirectToCheckout: You must provide one of lineItems, items, or sessionId
几个星期以来,我一直在将 Stripe checkout 集成到我的网站中。 内容将随着结帐过程中设置的名称/描述和价格而动态变化。
我目前正在运行以下内容:
创建结帐会话.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',
]);
?>
结帐 session ID 可以回显到主页上使用
<?php echo json_encode($checkout_session['id']) ?>
在脚本中我有触发这个的 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`.
});
});
});
但是我得到了参考错误。 除了更改域以获得成功/取消之外,我还缺少什么?
为了让您的代码在客户端工作,您需要在sessionId
参数中传递 Checkout Session
id cs_test_123456
客户端。
目前,您的代码正在传递sessionId: document.$checkout_session
,其中此变量可能未初始化。 我的猜测是您正在尝试访问 Javascript 中的 PHP 变量,因为它们是不同的环境,所以这种方式不起作用。 相反,您需要修复 JS 代码以正确获取 PHP 变量中的值。
如果你在一个文件中混合 JS 和 PHP 最简单的方法是这样做:
sessionId: '<?php echo $checkout_session->id; >?',
如果你不是并且已经将 PHP 和 JS 分开,那么你需要找到一种方法来从 PHP 变量中加载值,就像你从服务器加载其他信息一样。
这是因为您应该只在使用 php 代码创建 session 时触发此代码。 因此,如果您将代码移动到您创建 session 的页面,那么您将不会收到错误!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.