39496-vm/checkout.php
2026-04-06 04:21:26 +00:00

147 lines
8.5 KiB
PHP

<?php
require_once __DIR__ . '/includes/app.php';
$plans = plans_catalog();
$planKey = (string) ($_GET['plan'] ?? $_POST['plan_key'] ?? 'plus');
$plan = get_plan($planKey) ?? get_plan('plus');
$cycle = (string) ($_GET['cycle'] ?? $_POST['billing_cycle'] ?? 'monthly');
$cycle = in_array($cycle, ['monthly', 'yearly'], true) ? $cycle : 'monthly';
$errors = [];
$form = [
'full_name' => trim((string) ($_POST['full_name'] ?? '')),
'email' => trim((string) ($_POST['email'] ?? '')),
'whatsapp' => trim((string) ($_POST['whatsapp'] ?? '')),
'preferred_language' => (string) ($_POST['preferred_language'] ?? current_lang()),
'billing_cycle' => $cycle,
'plan_key' => $plan['key'],
'wablas_opt_in' => isset($_POST['wablas_opt_in']) ? 1 : 0,
];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($form['full_name'] === '' || strlen($form['full_name']) < 2) {
$errors[] = t('Please enter a valid full name.', 'يرجى إدخال اسم كامل صحيح.');
}
if (!filter_var($form['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = t('Please enter a valid email address.', 'يرجى إدخال بريد إلكتروني صحيح.');
}
if (!preg_match('/^[0-9+\-\s]{8,20}$/', $form['whatsapp'])) {
$errors[] = t('Please enter a valid WhatsApp number.', 'يرجى إدخال رقم واتساب صحيح.');
}
if (!in_array($form['preferred_language'], ['en', 'ar'], true)) {
$errors[] = t('Please select a supported language.', 'يرجى اختيار لغة مدعومة.');
}
if (!$errors) {
$reference = 'THW-' . date('YmdHis') . '-' . random_int(100, 999);
$id = create_subscription([
'full_name' => $form['full_name'],
'email' => $form['email'],
'whatsapp' => $form['whatsapp'],
'preferred_language' => $form['preferred_language'],
'plan_key' => $plan['key'],
'billing_cycle' => $form['billing_cycle'],
'payment_status' => 'active',
'payment_gateway' => 'Thawani',
'thawani_reference' => $reference,
'wablas_opt_in' => $form['wablas_opt_in'],
]);
$_SESSION['subscription_id'] = $id;
$_SESSION['student_email'] = $form['email'];
header('Location: ' . app_url('subscription.php', ['id' => $id, 'created' => 1]));
exit;
}
}
render_head(
t('Checkout', 'الدفع'),
t('Capture a student subscription with bilingual preferences, WhatsApp opt-in, and a Thawani-ready payment reference.', 'التقط اشتراك طالب مع تفضيلات ثنائية اللغة وخيار واتساب ومرجع دفع جاهز لثواني.')
);
render_nav('pricing.php');
?>
<main class="py-5">
<div class="container">
<div class="row g-4">
<div class="col-lg-7">
<div class="panel-card">
<span class="eyebrow"><?= h(t('Checkout flow', 'مسار الدفع')) ?></span>
<h1 class="section-title mb-2"><?= h(t('Create a subscription and unlock the student dashboard', 'أنشئ اشتراكاً وافتح لوحة الطالب')) ?></h1>
<p class="text-secondary mb-4"><?= h(t('This first slice records a real subscription row locally, reserves a Thawani reference, and prepares Wablas reminders.', 'تسجل هذه الشريحة الأولى صف اشتراك حقيقياً محلياً وتحجز مرجع Thawani وتجهز تذكيرات Wablas.')) ?></p>
<?php if ($errors): ?>
<div class="alert alert-danger border">
<ul class="mb-0 ps-3">
<?php foreach ($errors as $error): ?>
<li><?= h($error) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<form method="post" class="row g-3">
<input type="hidden" name="plan_key" value="<?= h($plan['key']) ?>">
<div class="col-md-6">
<label class="form-label" for="full_name"><?= h(t('Full name', 'الاسم الكامل')) ?></label>
<input class="form-control" id="full_name" name="full_name" required value="<?= h($form['full_name']) ?>">
</div>
<div class="col-md-6">
<label class="form-label" for="email"><?= h(t('Email', 'البريد الإلكتروني')) ?></label>
<input class="form-control" id="email" type="email" name="email" required value="<?= h($form['email']) ?>">
</div>
<div class="col-md-6">
<label class="form-label" for="whatsapp"><?= h(t('WhatsApp', 'واتساب')) ?></label>
<input class="form-control" id="whatsapp" name="whatsapp" placeholder="+968 9000 0000" required value="<?= h($form['whatsapp']) ?>">
</div>
<div class="col-md-3">
<label class="form-label" for="preferred_language"><?= h(t('Language', 'اللغة')) ?></label>
<select class="form-select" id="preferred_language" name="preferred_language">
<option value="en" <?= $form['preferred_language'] === 'en' ? 'selected' : '' ?>>English</option>
<option value="ar" <?= $form['preferred_language'] === 'ar' ? 'selected' : '' ?>>العربية</option>
</select>
</div>
<div class="col-md-3">
<label class="form-label" for="billing_cycle"><?= h(t('Billing', 'الدفع الدوري')) ?></label>
<select class="form-select" id="billing_cycle" name="billing_cycle">
<option value="monthly" <?= $form['billing_cycle'] === 'monthly' ? 'selected' : '' ?>><?= h(t('Monthly', 'شهري')) ?></option>
<option value="yearly" <?= $form['billing_cycle'] === 'yearly' ? 'selected' : '' ?>><?= h(t('Yearly', 'سنوي')) ?></option>
</select>
</div>
<div class="col-12">
<div class="form-check border rounded-3 p-3">
<input class="form-check-input" type="checkbox" id="wablas_opt_in" name="wablas_opt_in" value="1" <?= $form['wablas_opt_in'] ? 'checked' : '' ?>>
<label class="form-check-label" for="wablas_opt_in">
<?= h(t('Send payment success and class reminders through Wablas WhatsApp gateway.', 'أرسل نجاح الدفع وتذكيرات الحصص عبر بوابة Wablas لواتساب.')) ?>
</label>
</div>
</div>
<div class="col-12 d-flex flex-wrap gap-2 pt-2">
<button class="btn btn-dark btn-lg" type="submit"><?= h(t('Confirm subscription', 'تأكيد الاشتراك')) ?></button>
<a class="btn btn-outline-dark btn-lg" href="<?= h(app_url('pricing.php')) ?>"><?= h(t('Back to plans', 'العودة إلى الخطط')) ?></a>
</div>
</form>
</div>
</div>
<div class="col-lg-5">
<aside class="panel-card sticky-card">
<div class="d-flex justify-content-between align-items-start mb-3">
<div>
<span class="eyebrow"><?= h(t('Order summary', 'ملخص الطلب')) ?></span>
<h2 class="h5 mb-1"><?= h(plan_name($plan)) ?></h2>
<p class="small text-secondary mb-0"><?= h(t('Single-platform access', 'وصول إلى منصة موحدة')) ?></p>
</div>
<span class="badge bg-dark-subtle text-dark-emphasis border"><?= h(t('Step 1 MVP', 'النسخة الأولى')) ?></span>
</div>
<div class="summary-row"><span><?= h(t('Billing cycle', 'دورة الدفع')) ?></span><strong><?= h($cycle === 'yearly' ? t('Yearly', 'سنوي') : t('Monthly', 'شهري')) ?></strong></div>
<div class="summary-row"><span><?= h(t('Price', 'السعر')) ?></span><strong data-cycle-price><?= h(price_label($plan, $cycle)) ?></strong></div>
<div class="summary-row"><span><?= h(t('Gateway', 'البوابة')) ?></span><strong>Thawani</strong></div>
<div class="summary-row"><span><?= h(t('Notifications', 'الإشعارات')) ?></span><strong>Wablas</strong></div>
<div class="summary-row"><span><?= h(t('Live rooms', 'الغرف المباشرة')) ?></span><strong>Google Meet</strong></div>
<hr>
<ul class="list-unstyled compact-list compact-list-tight mb-0">
<?php foreach (current_lang() === 'ar' ? $plan['features_ar'] : $plan['features_en'] as $feature): ?>
<li><?= h($feature) ?></li>
<?php endforeach; ?>
</ul>
</aside>
</div>
</div>
</div>
</main>
<?php render_footer(); ?>