<?php
require 'vendor/autoload.php';

\Stripe\Stripe::setApiKey('sk_test_51TC4TjL1qy8feMVjlcd1d4Ofocjii873PehJzmRMMOjDjvHiuYBgiLihGT1XCqshGVzA4AUQEc75iGDmRw9Wc1SD003OlAenED');

header('Content-Type: application/json');

$input = json_decode(file_get_contents("php://input"), true);
$amount = isset($input['amount']) ? floatval($input['amount']) : 0;

// Stripe expects amount in **cents**
$amountInCents = intval(round($amount * 100));

try {
    $paymentIntent = \Stripe\PaymentIntent::create([
        'amount' => $amountInCents,
        'currency' => 'usd',
        'automatic_payment_methods' => [
            'enabled' => true,
        ],
    ]);

    echo json_encode([
        'clientSecret' => $paymentIntent->client_secret,
    ]);
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
