JezK
Edit File: create-stripe-checkout-session.php
<?php // Ensure this file is not accessed directly if (!defined('ABSPATH')) { exit; // Exit if accessed directly } // Check if session data is available if (!isset($session_data) || empty($session_data)) { header('Content-Type: application/json'); http_response_code(400); echo json_encode(['error' => 'Session data is missing']); exit; } // Log the session data for debugging error_log("Creating checkout session with data: " . json_encode($session_data)); // Include Stripe PHP library require_once(__DIR__ . '/stripe-lib/stripe-php-master/init.php'); // Define Stripe API key if not already defined if (!defined('STRIPE_SECRET_KEY')) { define('STRIPE_SECRET_KEY', 'YOUR_STRIPE_SECRET_KEY'); // Replace with your actual key or better yet, fetch from environment/config } // Set up Stripe API key \Stripe\Stripe::setApiKey(STRIPE_SECRET_KEY); // Prepare line items for the Checkout Session $line_items = [[ 'price_data' => [ 'currency' => 'usd', 'unit_amount' => $session_data['totalPrice'] * 100, // Convert to cents 'product_data' => [ 'name' => 'Routes to Roots Program: ' . $session_data['accommodationText'], 'description' => isset($session_data['roomTypeText']) && !empty($session_data['roomTypeText']) ? 'Room Type: ' . $session_data['roomTypeText'] : '', ], ], 'quantity' => 1, ]]; try { error_log("Creating Stripe customer"); // Create a new Stripe Customer $customer = \Stripe\Customer::create([ 'name' => $session_data['firstName'] . ' ' . $session_data['lastName'], 'email' => $session_data['email'], ]); error_log("Creating Stripe checkout session"); // Create a Stripe Checkout Session $session = \Stripe\Checkout\Session::create([ 'customer' => $customer->id, 'payment_method_types' => ['card'], 'line_items' => $line_items, 'mode' => 'payment', 'success_url' => 'https://authentica.com/r2r-payment-success', // Replace with your actual success URL 'cancel_url' => 'https://authentica.com/r2r-payment-cancel', // Replace with your actual cancel URL 'metadata' => [ // Include metadata 'customer_name' => $session_data['firstName'] . ' ' . $session_data['lastName'], 'customer_email' => $session_data['email'], 'accommodation' => $session_data['accommodationText'], 'room_type' => isset($session_data['roomTypeText']) && !empty($session_data['roomTypeText']) ? $session_data['roomTypeText'] : 'N/A', ], ]); error_log("Checkout session created successfully: " . $session->id); // Return the session URL header('Content-Type: application/json'); echo json_encode([ 'success' => true, 'payment_url' => $session->url, ]); } catch (\Stripe\Exception\ApiErrorException $e) { // Handle Stripe API errors error_log("Stripe API error: " . $e->getMessage()); header('Content-Type: application/json'); http_response_code(500); echo json_encode(['success' => false, 'error' => 'Stripe error: ' . $e->getMessage()]); exit; } catch (\Exception $e) { // Handle other errors error_log("General error creating checkout: " . $e->getMessage()); header('Content-Type: application/json'); http_response_code(500); echo json_encode(['success' => false, 'error' => 'Error creating checkout session: ' . $e->getMessage()]); exit; } ?>