36716-vm/purchase-gift.php
2025-12-07 05:00:42 +00:00

84 lines
3.0 KiB
PHP

<?php
require_once 'includes/header.php';
require_once 'db/config.php';
session_start();
if (!isset($_SESSION['user_id'])) { // Any logged in user can buy a gift
header('Location: login.php');
exit;
}
if (!isset($_GET['package_id'])) {
header('Location: coaches.php?error=missing_package');
exit;
}
$package_id = $_GET['package_id'];
// Fetch package details
$stmt = db()->prepare('SELECT * FROM service_packages WHERE id = ?');
$stmt->execute([$package_id]);
$package = $stmt->fetch();
if (!$package) {
header('Location: coaches.php?error=invalid_package');
exit;
}
?>
<div class="container">
<h1 class="text-center my-4">Purchase Gift</h1>
<div class="row">
<div class="col-md-6 offset-md-3">
<div class="card">
<div class="card-header">Package: <?= htmlspecialchars($package['name']) ?></div>
<div class="card-body">
<p><?= htmlspecialchars($package['description']) ?></p>
<p><strong>Price:</strong> $<span id="price"><?= htmlspecialchars($package['price']) ?></span></p>
<form action="create-checkout-session.php" method="POST">
<input type="hidden" name="package_id" value="<?= $package_id ?>">
<input type="hidden" name="is_gift" value="true">
<div class="form-group">
<label for="coupon_code">Coupon Code (Optional)</label>
<input type="text" id="coupon_code" name="coupon_code" class="form-control">
<button type="button" id="apply_coupon" class="btn btn-secondary mt-2">Apply Coupon</button>
</div>
<div id="coupon_result"></div>
<button type="submit" class="btn btn-primary">Purchase Gift</button>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
document.getElementById('apply_coupon').addEventListener('click', function() {
const coupon_code = document.getElementById('coupon_code').value;
const package_id = <?= $package_id ?>;
const coupon_result = document.getElementById('coupon_result');
const price_span = document.getElementById('price');
const original_price = <?= $package['price'] ?>;
fetch('/api/validate_coupon.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ coupon_code: coupon_code, package_id: package_id })
})
.then(response => response.json())
.then(data => {
if (data.success) {
price_span.textContent = data.discounted_price.toFixed(2);
coupon_result.innerHTML = '<div class="alert alert-success">Coupon applied!</div>';
} else {
price_span.textContent = original_price;
coupon_result.innerHTML = '<div class="alert alert-danger">' + data.error + '</div>';
}
});
});
</script>
<?php require_once 'includes/footer.php'; ?>