Stripe Checkout PHP Integration



我在Stripe集成工作中获得了很多"乐趣"。

我在一个旧的支付页面上构建,但它在传统版本的结账上工作。在升级过程中,我遇到了很多问题,但文档似乎没有帮助。

我在一个单独的index.php文件中运行以下代码

require('../config.php');
<script>
// Set your secret key. Remember to switch to your live secret key in production!
// See your keys here: https://dashboard.stripe.com/account/apikeys
StripeStripe::setApiKey('sk_test_KEY');
$checkout_session = StripeCheckoutSession::create([
'payment_method_types' => ['card'],
'line_items' => [[
'name' => 'T-shirt',
'description' => 'Comfortable cotton t-shirt',
'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',
]); 
</script>
<script src="https://js.stripe.com/v3/"></script>
<?php stripe = Stripe('pk_test_KEY'); ?>
<li> <a href="javascript:void(0)" onclick="myRedirectFN"><button class="btn btn-buy"> Buy image <?php echo htmlspecialchars(" - from £" . $imagePrice); ?></button></a></li>
<script> 
function myRedirectFN() {
stripe.redirectToCheckout({
var stripe = Stripe('pk_test_CXv2tKPq22Bo9o9CaKi6z3nW00ZprVPd08')
// 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: '$checkout_session['id']'
}).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>

这是Stripe文档中的所有功能。

目前我收到一个意外的var错误,但当我删除var时,没有进一步的进展。

我应该如何构造此代码?

嗯,您的javascript似乎不完整,onclick处理程序指向一个不存在的函数。此外,您需要确保从服务器端调用中获取签出会话,以便通过模板或通过对服务器的回调提供给客户端请求。

你可能想做一些类似的事情:onclick="myRedirectFn",然后定义这个函数:

function myRedirectFn() {
stripe.redirectToCheckout({
sessionId: 'cs_123',
});
}

有一个完整的Checkout示例应用程序,包括一个PHP服务器实现,您可以参考并比较它。

最新更新