Storing Stripe Checkout Session ID in $_SESSION and checking on Success

Viewed 37

I am Storing Stripe Checkout Session ID in $_SESSION and checking on Success.php if the $_GET['session_id'] matches the $_SESSION['checkout_id'] I created at checkout but it's not always matching.

Am i approaching this right? For some reason it never fails on my local machine but only on the production CPanel instance.

CreateCheckoutSession.php snippet:

$_SESSION['checkout_id'] = "";
    $checkout_session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],
  'line_items' => $line_items,
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN . '/checkout/success.php?session_id={CHECKOUT_SESSION_ID}&php_session_id='.$session_id.'',
  'cancel_url' => $YOUR_DOMAIN . '/checkout/canceled.php?php_session_id='.$session_id.'',
]);
    $_SESSION['checkout_id'] = $checkout_session->id;

success.php snippet:

<?php

session_start();
$session_id=session_id();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require '../php/vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
//wipe basket if not already
include_once("../php/connect.php");
//check basket exists




//if ($_SESSION['checkout_id'] != $_GET['session_id']) {
if ($_SESSION['checkout_id'] != $_GET['session_id']) {

    //echo $_SESSION['checkout_id'];
    //echo $session_id;
    //echo $_GET['php_session_id'];
    
    //echo $_GET['session_id'];
    //header('Location: ../index.php');

Error Message: Warning: Undefined array key "checkout_id" in home/public_html/checkout/success.php on line 21

Redirect URL Example: success.php?session_id=cs_test_a1zoOWt5bwr7t3xtk67waAbS9Q7xyDp6GlLbuneNnfmp7SC4XaJWI4CgWD&php_session_id=fa3b43e0b3a5e2da9485ae1f6c97a1b0

0 Answers
Related