I've been integrating Stripe checout into my website for some weeks now. 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
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
<?php echo json_encode($checkout_session['id']) ?>
Within the script I have buyingPhoto button that triggers this
$( 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?