I am using Stripe for payment on a page. There are three items on the page and each one has a price that is set dynamically based on inputs on the page. Let's call them plan A, B and C. I have Stripe Checkout working successfully with a hard-coded price but I need to pass the price to Stripe based on which button is clicked and the value of that item.
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',
]);
?>
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 do something like this
stripe.redirectToCheckout({
sessionId: "<?php echo $session->id; ?>"
});
});
</script>
Essentially, I need to create a $post request on button click that hits another file (checkout.php) and in that file, I will create the session after I have set the variable for price using the $post data. Thanks in advance!!!