<?php
session_start();
require_once 'vendor/autoload.php';
require_once 'db/config.php';
require_once 'includes/api_keys.php';

\Stripe\Stripe::setApiKey($stripeSecretKey);

$total_price = $_SESSION['total_price'] ?? 0;
$coupon_id = $_SESSION['coupon_id'] ?? null;
$user_id = $_SESSION['user_id'] ?? null;

if ($total_price <= 0) {
    header("Location: cart.php");
    exit();
}

$checkout_session = \Stripe\Checkout\Session::create([
    'payment_method_types' => ['card'],
    'line_items' => [[
        'price_data' => [
            'currency' => 'usd',
            'product_data' => [
                'name' => 'Total Order Amount',
            ],
            'unit_amount' => $total_price * 100, // Amount in cents
        ],
        'quantity' => 1,
    ]],
    'mode' => 'payment',
    'success_url' => 'http://localhost:8080/payment-success.php?session_id={CHECKOUT_SESSION_ID}',
    'cancel_url' => 'http://localhost:8080/payment-cancel.php',
    'metadata' => [
        'user_id' => $user_id,
        'coupon_id' => $coupon_id
    ]
]);

header("Location: " . $checkout_session->url);