Home > front end >  Checkout Session creation failed! Invalid array Stripe Checkout
Checkout Session creation failed! Invalid array Stripe Checkout

Time:11-02

I am integrating Stripe checkout into my PHP project, I need to create a session for stripe checkout but I am getting an Invalid array error when I click the payment button, it's not creating a session and throwing an error. I don't know where and what I am missing, I am using stripe V3 code. if something I am missing assists me to solve the issue it's urgent, please.

The error I am getting on session creation stripe checkout.

Checkout Session creation failed! Invalid array

Checkout page code

<?php 
// Include configuration file   
require_once 'config.php'; 
 
// Include Stripe PHP library  
require_once 'stripe-php/init.php'; 
 
// Set API key 
$stripe = new \Stripe\StripeClient(STRIPE_API_KEY); 
 
$response = array( 
    'status' => 0, 
    'error' => array( 
        'message' => 'Invalid Request!'    
    ) 
); 
 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $input = file_get_contents('php://input'); 
    $request = json_decode($input);     
} 
 
if (json_last_error() !== JSON_ERROR_NONE) { 
    http_response_code(400); 
    echo json_encode($response); 
    exit; 
} 
 
if(!empty($request->checkoutSession)){ 
    // Create new Checkout Session for the order 
    try { 
        
        
        
        $session =  $stripe->checkout->sessions->create([
            'payment_method_types' => ['card'],
            'line_items' => [[
                'price_data' => [
                    'currency' => $currency,
                    'unit_amount' => $stripeAmount,
                    'product_data' => [
                        'name' => $productName,
                        'images' => $productName,
                    ],
                ],
                'quantity' => 1,
            ]],
            'mode' => 'payment',
         
            'success_url' => STRIPE_SUCCESS_URL.'?session_id={CHECKOUT_SESSION_ID}', 
            'cancel_url' => STRIPE_CANCEL_URL,
        ]);
        
        
        
    }catch(Exception $e) {  
        $api_error = $e->getMessage();  
    } 
     
    if(empty($api_error) && $session){ 
        $response = array( 
            'status' => 1, 
            'message' => 'Checkout Session created successfully!', 
            'sessionId' => $session['id'] 
        ); 
    }else{ 
        $response = array( 
            'status' => 0, 
            'error' => array( 
                'message' => 'Checkout Session creation failed! '.$api_error    
            ) 
        ); 
    } 
} 
 
// Return response 
echo json_encode($response);
?>

CodePudding user response:

I think issue is with "line_items.price_data.product_data.images" as it accepts a list of images and you are passing the product name. it should be look like this

 $session =  $stripe->checkout->sessions->create([
            'payment_method_types' => ['card'],
            'line_items' => [[
                'price_data' => [
                    'currency' => $currency,
                    'unit_amount' => $stripeAmount,
                    'product_data' => [
                        'name' => $productName,
                        'images' => ["image_link1","image_link2"],
                    ],
                ],
                'quantity' => 1,
            ]],
            'mode' => 'payment',
         
            'success_url' => STRIPE_SUCCESS_URL.'?session_id={CHECKOUT_SESSION_ID}', 
            'cancel_url' => STRIPE_CANCEL_URL,
        ]);
  • Related