我在页面上使用 Stripe 进行支付。该页面上有三个项目,每个项目都有一个根据页面上的输入动态设置的价格。我们将它们称为计划 A、B 和 C。我的 Stripe Checkout 成功地使用了硬编码的价格,但我需要根据单击的按钮和该商品的价值将价格传递给 Stripe。
PHP:
<?php require_once('/stripe/init.php');
\Stripe\Stripe::setApiKey('TEST KEY');
$session = new \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'product_data' => [
'name' => 'Insurance Plan',
],
'unit_amount' => 4000, //need to set this dynamically
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => 'https://example.com/success',
'cancel_url' => 'https://example.com/cancel',
]);
?>
Run Code Online (Sandbox Code Playgroud)
JS:
<script>
var stripe = Stripe('PUB KEY');
jQuery('.checkout-button').on('click', function(e) {
e.preventDefault();
var price = jQuery(this).find('.price').val(); //need to …
Run Code Online (Sandbox Code Playgroud)