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

100 lines
3.7 KiB
PHP

<?php
require_once 'includes/header.php';
require_once 'db/config.php';
if (!isset($_SESSION['user_id']) || $_SESSION['user_type'] !== 'client') {
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 mt-5">
<h2>Checkout</h2>
<div class="row">
<div class="col-md-6">
<div class="card">
<div class="card-header">
Package Details
</div>
<div class="card-body">
<h5 class="card-title"><?php echo htmlspecialchars($package['name']); ?></h5>
<p class="card-text"><?php echo htmlspecialchars($package['description']); ?></p>
<p class="card-text"><strong>Price:</strong> <span id="price">$<?php echo htmlspecialchars(number_format($package['price'], 2)); ?></span></p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header">
Payment
</div>
<div class="card-body">
<form id="checkout-form">
<div class="form-group">
<label for="coupon">Coupon Code</label>
<input type="text" class="form-control" id="coupon" name="coupon">
</div>
<div class="form-check mt-2">
<input type="checkbox" class="form-check-input" id="is_gift" name="is_gift">
<label class="form-check-label" for="is_gift">Is this a gift?</label>
</div>
<button type="button" id="apply-coupon" class="btn btn-secondary mt-2">Apply Coupon</button>
<hr>
<p>Total: <span id="total-price">$<?php echo htmlspecialchars(number_format($package['price'], 2)); ?></span></p>
<button type="submit" class="btn btn-primary">Pay with Stripe</button>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
document.getElementById('apply-coupon').addEventListener('click', function() {
const couponCode = document.getElementById('coupon').value;
const packageId = <?php echo $package_id; ?>;
fetch('api/validate_coupon.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ coupon_code: couponCode, package_id: packageId })
})
.then(response => response.json())
.then(data => {
if (data.success) {
document.getElementById('total-price').innerText = '$' + data.discounted_price.toFixed(2);
} else {
alert(data.error);
}
});
});
document.getElementById('checkout-form').addEventListener('submit', function(e) {
e.preventDefault();
const couponCode = document.getElementById('coupon').value;
const is_gift = document.getElementById('is_gift').checked;
window.location.href = 'create-checkout-session.php?package_id=<?php echo $package_id; ?>&coupon=' + couponCode + '&is_gift=' + is_gift;
});
</script>
<?php require_once 'includes/footer.php'; ?>