Brad
Brad

Reputation: 51

stripe.redirectToCheckout: You must provide one of lineItems, items, or sessionId

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?

Upvotes: 2

Views: 12071

Answers (2)

Carpiee
Carpiee

Reputation: 19

it is because you should only fire this code when you create the session with the php code. So if you move your code to the page where/when you make a session then you won't get the error!!

Upvotes: 0

koopajah
koopajah

Reputation: 25632

For your code to work client-side, you need to pass the Checkout Session id cs_test_123456 client-side in the sessionId parameter.

At the moment, your code is passing sessionId: document.$checkout_session where this variable is likely not initialized. My guess is that you are trying to access the PHP variable in Javascript which doesn't work that way as they are separate environments. Instead, you need to fix your JS code to properly get the value in the PHP variable.

If you are mixing JS and PHP in one file the easiest would be to do this:

sessionId: '<?php echo $checkout_session->id; >?',

If you are not and have separated PHP and JS, then you need to find a way to load the value from the PHP variable, the same way you would load other information from your server.

Upvotes: 1

Related Questions