103 lines
3.5 KiB
PHP
103 lines
3.5 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
require_once 'vendor/autoload.php';
|
|
$stripe_config = require_once 'config/stripe.php';
|
|
|
|
session_start();
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
$session_id = $_GET['session_id'] ?? null;
|
|
|
|
if (!$session_id) {
|
|
header('Location: pricing.php');
|
|
exit;
|
|
}
|
|
|
|
if ($stripe_config['stripe']['secret_key'] === 'YOUR_STRIPE_SECRET_KEY') {
|
|
die('Error: Stripe secret key is not configured. Please update config/stripe.php with your actual Stripe keys.');
|
|
}
|
|
|
|
\Stripe\Stripe::setApiKey($stripe_config['stripe']['secret_key']);
|
|
|
|
try {
|
|
$checkout_session = \Stripe\Checkout\Session::retrieve($session_id);
|
|
$stripe_subscription_id = $checkout_session->subscription;
|
|
$user_id = $checkout_session->metadata->user_id;
|
|
|
|
// Double-check that the user ID from the session matches the one in Stripe metadata
|
|
if ($user_id != $_SESSION['user_id']) {
|
|
throw new Exception("User ID mismatch.");
|
|
}
|
|
|
|
// Reconstruct plan name from metadata
|
|
$people = $checkout_session->metadata->people;
|
|
$meals = $checkout_session->metadata->meals_per_week;
|
|
$plan_name = sprintf("Weekly plan for %d %s, %d %s per week",
|
|
$people, ($people > 1 ? 'people' : 'person'),
|
|
$meals, ($meals > 1 ? 'meals' : 'meal')
|
|
);
|
|
|
|
$product_ids = json_decode($checkout_session->metadata->product_ids ?? '[]');
|
|
if (!empty($product_ids)) {
|
|
$plan_name .= " with add-ons";
|
|
}
|
|
|
|
$pdo = db();
|
|
$pdo->beginTransaction();
|
|
|
|
// Save the main subscription
|
|
$stmt = $pdo->prepare("INSERT INTO subscriptions (user_id, plan_id, stripe_subscription_id, status, plan_name) VALUES (?, ?, ?, 'active', ?)");
|
|
$stmt->execute([$user_id, 'custom', $stripe_subscription_id, $plan_name]);
|
|
|
|
// Save the subscribed products
|
|
if (!empty($product_ids)) {
|
|
$product_stmt = $pdo->prepare("INSERT INTO user_subscription_products (user_id, product_id, stripe_subscription_id, quantity) VALUES (?, ?, ?, 1)");
|
|
foreach ($product_ids as $product_id) {
|
|
$product_stmt->execute([$user_id, $product_id, $stripe_subscription_id]);
|
|
}
|
|
}
|
|
|
|
$pdo->commit();
|
|
|
|
unset($_SESSION['intended_plan']);
|
|
|
|
} catch (\Stripe\Exception\ApiErrorException $e) {
|
|
if (isset($pdo) && $pdo->inTransaction()) {
|
|
$pdo->rollBack();
|
|
}
|
|
$_SESSION['flash_message'] = ['type' => 'danger', 'message' => 'Error: ' . $e->getMessage()];
|
|
header('Location: pricing.php');
|
|
exit;
|
|
} catch (Exception $e) {
|
|
if (isset($pdo) && $pdo->inTransaction()) {
|
|
$pdo->rollBack();
|
|
}
|
|
$_SESSION['flash_message'] = ['type' => 'danger', 'message' => 'Error: ' . $e->getMessage()];
|
|
header('Location: pricing.php');
|
|
exit;
|
|
}
|
|
|
|
include 'admin/templates/header.php';
|
|
?>
|
|
|
|
<div class="container py-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-8 text-center">
|
|
<div class="auth-container">
|
|
<h2 class="text-success">Payment Successful!</h2>
|
|
<p class="lead">Thank you for subscribing. Your plan and selected products are now active.</p>
|
|
<p>You will be redirected to your profile page shortly.</p>
|
|
<div class="spinner-border text-primary mt-3" role="status">
|
|
<span class="visually-hidden">Loading...</span>
|
|
</div>
|
|
<meta http-equiv="refresh" content="5;url=profile.php">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php include 'admin/templates/footer.php'; ?>
|