Files
actiewebsitedinges/form.php
T
2026-10-10 21:32:00 +02:00

234 lines
9.2 KiB
PHP

<?php
require_once 'config.php';
require_once 'vendor/autoload.php';
// ── Controleer of de actie verlopen is ──
$verlopen = strtotime(ACTIE_EINDDATUM) < time();
$errors = [];
if (!$verlopen && $_SERVER['REQUEST_METHOD'] === 'POST') {
$groep = trim($_POST['groep'] ?? '');
$naam_verkoper = trim($_POST['naam_verkoper'] ?? '');
$naam_ouder = trim($_POST['naam_ouder'] ?? '');
$aantal = (int)($_POST['aantal'] ?? 0);
if (!in_array($groep, GROEPEN)) $errors[] = 'Kies een geldige groep.';
if (empty($naam_verkoper)) $errors[] = 'Naam verkoper is verplicht.';
if ($aantal < 1 || $aantal > 10000) $errors[] = 'Aantal moet tussen 1 en 10000 liggen.';
if (empty($errors)) {
$totaal = round($aantal * PRIJS_PER_STUK, 2);
$_SESSION['order'] = [
'groep' => $groep,
'naam_verkoper' => $naam_verkoper,
'naam_ouder' => $naam_ouder,
'aantal' => $aantal,
'totaal' => $totaal,
'datum' => date('Y-m-d H:i:s'),
];
try {
$mollie = new \Mollie\Api\MollieApiClient();
$mollie->setApiKey(MOLLIE_API_KEY);
$payment = $mollie->payments->create([
'amount' => [
'currency' => 'EUR',
'value' => number_format($totaal, 2, '.', ''),
],
'description' => ACTIE_NAAM . ' — ' . $aantal . 'x ' . PRODUCT_NAAM . ' (' . $naam_verkoper . ')',
'redirectUrl' => RETURN_URL,
// 'webhookUrl' => WEBHOOK_URL,
'metadata' => [
'groep' => $groep,
'naam_verkoper' => $naam_verkoper,
'naam_ouder' => $naam_ouder,
'aantal' => $aantal,
'datum' => $_SESSION['order']['datum'],
],
]);
$_SESSION['order']['payment_id'] = $payment->id;
header('Location: ' . $payment->getCheckoutUrl());
exit;
} catch (\Mollie\Api\Exceptions\ApiException $e) {
$errors[] = 'Betaling kon niet worden aangemaakt: ' . htmlspecialchars($e->getMessage());
}
}
}
?>
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bestelling — <?= htmlspecialchars(ACTIE_NAAM) ?></title>
<link rel="stylesheet" href="https://unpkg.com/bootstrap@5.3.3/dist/css/bootstrap.min.css">
</head>
<body class="bg-light">
<section class="bg-light p-3 p-md-4 p-xl-5">
<div class="container">
<div class="row justify-content-center">
<div class="col-12 col-xxl-11">
<div class="card border-light-subtle shadow-sm">
<div class="row g-0">
<!-- Afbeelding links -->
<div class="col-12 col-md-6">
<img class="img-fluid rounded-start w-100 h-100 object-fit-cover"
loading="lazy"
src="/assets/markus.jpg"
alt="<?= htmlspecialchars(ACTIE_NAAM) ?>">
</div>
<!-- Rechterkolom -->
<div class="col-12 col-md-6 d-flex align-items-center justify-content-center">
<div class="col-12 col-lg-11 col-xl-10">
<div class="card-body p-3 p-md-4 p-xl-5">
<div class="mb-4">
<a href="index.php" class="text-secondary text-decoration-none small">← Terug</a>
<h1 class="fs-4 fw-bold mt-2 mb-0">Bestelling plaatsen</h1>
<p class="text-secondary small mt-1">
<?= htmlspecialchars(ACTIE_NAAM) ?> &middot;
€<?= number_format(PRIJS_PER_STUK, 2, ',', '.') ?> per stuk
</p>
</div>
<?php if ($verlopen): ?>
<!-- ── VERLOPEN MELDING ── -->
<div class="text-center py-4">
<div style="font-size:3rem;">⏰</div>
<h2 class="fs-5 fw-bold mt-3 mb-2">De besteltermijn is verlopen</h2>
<p class="text-secondary small">
Bestellingen konden worden geplaatst tot<br>
<strong><?= date('j F Y \o\m H:i', strtotime(ACTIE_EINDDATUM)) ?></strong>.
</p>
<p class="text-secondary small mt-2">
Heb je vragen? Neem contact op met de leiding.
</p>
<a href="index.php" class="btn btn-outline-secondary mt-3">← Terug naar de actiepagina</a>
</div>
<?php else: ?>
<!-- ── NORMAAL FORMULIER ── -->
<?php if (!empty($errors)): ?>
<div class="alert alert-danger py-2">
<ul class="mb-0 ps-3">
<?php foreach ($errors as $err): ?>
<li><?= htmlspecialchars($err) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<!-- Einddatum banner -->
<div class="alert alert-warning py-2 small mb-3">
⏳ Bestel vóór <strong><?= date('j F Y', strtotime(ACTIE_EINDDATUM)) ?></strong>
</div>
<form method="POST" id="orderForm">
<div class="row gy-3">
<div class="col-12">
<div class="form-floating">
<select class="form-select" name="groep" id="groep" required>
<option value="">— Kies een groep —</option>
<?php foreach (GROEPEN as $g): ?>
<option value="<?= htmlspecialchars($g) ?>"
<?= (($_POST['groep'] ?? '') === $g) ? 'selected' : '' ?>>
<?= htmlspecialchars($g) ?>
</option>
<?php endforeach; ?>
</select>
<label for="groep">Groep</label>
</div>
</div>
<div class="col-12">
<div class="form-floating">
<input type="text" class="form-control" name="naam_verkoper" id="naam_verkoper"
placeholder="Naam verkoper"
value="<?= htmlspecialchars($_POST['naam_verkoper'] ?? '') ?>" required>
<label for="naam_verkoper">Naam verkoper</label>
</div>
</div>
<div class="col-12">
<div class="form-floating">
<input type="text" class="form-control" name="naam_ouder" id="naam_ouder"
placeholder="Naam ouder"
value="<?= htmlspecialchars($_POST['naam_ouder'] ?? '') ?>">
<label for="naam_ouder">Naam ouder <span class="text-secondary fw-normal">(betreft welpen/verkenners)</span></label>
</div>
</div>
<div class="col-12">
<div class="form-floating">
<input type="number" class="form-control" name="aantal" id="aantal"
min="1" max="10000" placeholder="Aantal"
value="<?= htmlspecialchars($_POST['aantal'] ?? '') ?>" required>
<label for="aantal">Aantal stuks</label>
</div>
</div>
<div class="col-12" id="totaalPreview" style="display:none">
<div class="alert alert-success py-2 mb-0">
Totaalbedrag: <strong id="totaalBedrag"></strong>
</div>
</div>
<div class="col-12">
<div class="d-grid">
<button class="btn btn-dark btn-lg" type="submit">
Doorgaan naar betaling →
</button>
</div>
</div>
</div>
</form>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<?php if (!$verlopen): ?>
<script>
const aantalInput = document.getElementById('aantal');
const preview = document.getElementById('totaalPreview');
const bedragEl = document.getElementById('totaalBedrag');
const prijs = <?= PRIJS_PER_STUK ?>;
aantalInput.addEventListener('input', () => {
const n = parseInt(aantalInput.value);
if (n > 0) {
bedragEl.textContent = '€' + (n * prijs).toFixed(2).replace('.', ',');
preview.style.display = 'block';
} else {
preview.style.display = 'none';
}
});
if (aantalInput.value) aantalInput.dispatchEvent(new Event('input'));
</script>
<?php endif; ?>
</body>
</html>