49 lines
1.5 KiB
PHP
49 lines
1.5 KiB
PHP
<?php
|
|
require_once 'includes/header.php';
|
|
require_once 'db/config.php';
|
|
require_once 'stripe/init.php';
|
|
|
|
session_start();
|
|
|
|
if (!isset($_GET['session_id'])) {
|
|
header('Location: coaches.php');
|
|
exit;
|
|
}
|
|
|
|
$stripe_checkout_session_id = $_GET['session_id'];
|
|
|
|
try {
|
|
$session = \Stripe\Checkout\Session::retrieve($stripe_checkout_session_id);
|
|
} catch (Exception $e) {
|
|
header('Location: coaches.php?error=invalid_session');
|
|
exit;
|
|
}
|
|
|
|
// Check if a gift code has already been created for this session
|
|
$stmt = db()->prepare('SELECT * FROM gift_codes WHERE stripe_checkout_session_id = ?');
|
|
$stmt->execute([$stripe_checkout_session_id]);
|
|
$gift_code_entry = $stmt->fetch();
|
|
|
|
if ($gift_code_entry) {
|
|
$gift_code = $gift_code_entry['code'];
|
|
} else {
|
|
// Generate a unique gift code
|
|
$gift_code = 'GIFT-' . strtoupper(bin2hex(random_bytes(8)));
|
|
$package_id = $session->metadata->package_id;
|
|
|
|
$stmt = db()->prepare('INSERT INTO gift_codes (code, package_id, stripe_checkout_session_id) VALUES (?, ?, ?)');
|
|
$stmt->execute([$gift_code, $package_id, $stripe_checkout_session_id]);
|
|
}
|
|
|
|
?>
|
|
|
|
<div class="container mt-5">
|
|
<div class="alert alert-success">
|
|
<h4>Purchase Successful!</h4>
|
|
<p>Thank you for your purchase. Here is your gift code. Share it with the recipient!</p>
|
|
<h3 class="text-center my-3"><code><?= htmlspecialchars($gift_code) ?></code></h3>
|
|
</div>
|
|
<a href="dashboard.php" class="btn btn-primary">Go to Dashboard</a>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php';
|