110 lines
4.9 KiB
PHP
110 lines
4.9 KiB
PHP
<?php
|
|
require_once 'includes/header.php';
|
|
require_once 'db/config.php';
|
|
|
|
session_start();
|
|
|
|
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'];
|
|
$client_id = $_SESSION['user_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 Package</h1>
|
|
<div class="row">
|
|
<div class="col-md-6 offset-md-3">
|
|
<div class="card">
|
|
<div class="card-header"><?= htmlspecialchars($package['name']) ?></div>
|
|
<div class="card-body">
|
|
<p><?= htmlspecialchars($package['description']) ?></p>
|
|
|
|
<form action="create-checkout-session.php" method="POST">
|
|
<input type="hidden" name="package_id" value="<?= $package_id ?>">
|
|
|
|
<?php if ($package['payment_type'] === 'payment_plan'): ?>
|
|
<h4>Payment Options</h4>
|
|
<div class="form-check">
|
|
<?php
|
|
$full_price = $package['price'];
|
|
if ($package['pay_in_full_discount_percentage']) {
|
|
$full_price -= ($full_price * ($package['pay_in_full_discount_percentage'] / 100));
|
|
}
|
|
?>
|
|
<input class="form-check-input" type="radio" name="payment_option" id="pay_in_full" value="full" checked>
|
|
<label class="form-check-label" for="pay_in_full">
|
|
Pay in Full: $<?= number_format($full_price, 2) ?>
|
|
<?php if ($package['pay_in_full_discount_percentage']): ?>
|
|
(<?= $package['pay_in_full_discount_percentage'] ?>% discount)
|
|
<?php endif; ?>
|
|
</label>
|
|
</div>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="radio" name="payment_option" id="payment_plan" value="plan">
|
|
<label class="form-check-label" for="payment_plan">
|
|
Pay Deposit: $<?= number_format($package['deposit_amount'], 2) ?> then <?= $package['installments'] ?> payments of $<?= number_format(($package['price'] - $package['deposit_amount']) / $package['installments'], 2) ?> every <?= $package['installment_interval'] ?>
|
|
</label>
|
|
</div>
|
|
<?php else: ?>
|
|
<p><strong>Price:</strong> $<span id="price"><?= htmlspecialchars($package['price']) ?></span></p>
|
|
<?php endif; ?>
|
|
|
|
<div class="form-group mt-3">
|
|
<label for="coupon_code">Coupon Code</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</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'; ?>
|