checkout.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. require_once __DIR__ . "/config.php";
  3. require_once __DIR__ . "/includes/functions.php";
  4. $pageTitle = "Bestellung abschließen";
  5. $cartItems = getCartItemsDetailed();
  6. $organizations = getOrganizations(true);
  7. $errors = [];
  8. if (empty($cartItems)) {
  9. header("Location: cart.php");
  10. exit();
  11. }
  12. if ($_SERVER['REQUEST_METHOD'] === "POST" && isset($_POST['create_order'])) {
  13. // Validate CSRF token
  14. if (!validateCsrfToken($_POST['csrf_token'] ?? "")) {
  15. $errors[] = "Ungültiges Token. Bitte versuchen Sie es erneut.";
  16. } else {
  17. $validator = new Validator($_POST);
  18. $validator
  19. ->required("customer_name", "Name")
  20. ->minLength("customer_name", 2, "Name")
  21. ->maxLength("customer_name", 100, "Name")
  22. ->required("customer_email", "E-Mail-Adresse")
  23. ->email("customer_email", "E-Mail-Adresse")
  24. ->maxLength("customer_email", 255, "E-Mail-Adresse")
  25. ->required("organization_id", "Organisation")
  26. ->maxLength("comment", 1000, "Kommentar");
  27. // Validate organization exists
  28. $organizationId = $_POST['organization_id'] ?? "";
  29. $organizations = getOrganizations(true);
  30. $validOrgIds = array_column($organizations, "id");
  31. if (!in_array($organizationId, $validOrgIds, true)) {
  32. $validator->addError("Die gewählte Organisation ist ungültig.");
  33. }
  34. if (!$validator->isValid()) {
  35. $errors = array_merge($errors, $validator->getErrors());
  36. } elseif (!checkoutRateLimitWouldAllow()) {
  37. $errors[] =
  38. "Zu viele Bestellversuche von dieser Verbindung. Bitte versuchen Sie es später erneut.";
  39. } else {
  40. $customerName = trim($_POST['customer_name']);
  41. $customerEmail = trim(strtolower($_POST['customer_email']));
  42. $comment = trim($_POST['comment'] ?? "");
  43. $result = createOrder(
  44. $customerName,
  45. $customerEmail,
  46. $organizationId,
  47. $comment,
  48. buildOrderItemsFromCart(),
  49. );
  50. if (!$result["success"]) {
  51. $errors[] = $result["message"];
  52. } else {
  53. checkoutRateLimitTryConsume();
  54. clearCart();
  55. logAccess("Order created", [
  56. "order_id" => $result["order"]["id"],
  57. "customer" => $customerEmail,
  58. ]);
  59. header(
  60. "Location: order-success.php?id=" .
  61. urlencode($result["order"]["id"]),
  62. );
  63. exit();
  64. }
  65. }
  66. }
  67. }
  68. include __DIR__ . "/includes/header.php";
  69. ?>
  70. <h2>Bestellung abschließen</h2>
  71. <?php if (!empty($errors)): ?>
  72. <div class="alert alert-error">
  73. <ul class="list-indent">
  74. <?php foreach ($errors as $error): ?>
  75. <li><?php echo escape($error); ?></li>
  76. <?php endforeach; ?>
  77. </ul>
  78. </div>
  79. <?php endif; ?>
  80. <div class="checkout-grid">
  81. <div>
  82. <h3>Ihre Auswahl</h3>
  83. <?php foreach ($cartItems as $cartItem): ?>
  84. <div class="panel panel-compact">
  85. <strong><?php echo escape(
  86. $cartItem["product"]["name"],
  87. ); ?></strong><br>
  88. <?php if ($cartItem["size"] !== ""): ?>
  89. Größe: <?php echo escape($cartItem["size"]); ?><br>
  90. <?php endif; ?>
  91. <?php if ($cartItem["availability_label"] !== ""): ?>
  92. Lieferhinweis: <?php echo escape(
  93. $cartItem["availability_label"],
  94. ); ?>
  95. <?php endif; ?>
  96. </div>
  97. <?php endforeach; ?>
  98. </div>
  99. <div>
  100. <h3>Bestelldaten</h3>
  101. <form method="POST">
  102. <div class="form-group">
  103. <label for="customer_name">Name *</label>
  104. <input type="text" id="customer_name" name="customer_name" required value="<?php echo isset(
  105. $_POST['customer_name'],
  106. )
  107. ? escape($_POST['customer_name'])
  108. : ""; ?>">
  109. </div>
  110. <div class="form-group">
  111. <label for="customer_email">E-Mail-Adresse *</label>
  112. <input type="email" id="customer_email" name="customer_email" required value="<?php echo isset(
  113. $_POST['customer_email'],
  114. )
  115. ? escape($_POST['customer_email'])
  116. : ""; ?>">
  117. </div>
  118. <div class="form-group">
  119. <label for="organization_id">Organisation *</label>
  120. <select id="organization_id" name="organization_id" required>
  121. <option value="">Bitte wählen</option>
  122. <?php foreach ($organizations as $organization): ?>
  123. <option value="<?php echo escape(
  124. $organization["id"],
  125. ); ?>" <?php echo isset($_POST['organization_id']) &&
  126. $_POST['organization_id'] === $organization["id"]
  127. ? "selected"
  128. : ""; ?>>
  129. <?php echo escape($organization["label"]); ?>
  130. </option>
  131. <?php endforeach; ?>
  132. </select>
  133. </div>
  134. <div class="form-group">
  135. <label for="comment">Kommentar</label>
  136. <textarea id="comment" name="comment" rows="5"><?php echo isset(
  137. $_POST['comment'],
  138. )
  139. ? escape($_POST['comment'])
  140. : ""; ?></textarea>
  141. </div>
  142. <div class="alert alert-info">
  143. Nach dem Absenden wird die Bestellung direkt an die Gerätewarte weitergeleitet.
  144. </div>
  145. <?php echo csrfField(); ?>
  146. <button type="submit" name="create_order" class="btn btn-block">Bestellung absenden</button>
  147. </form>
  148. <div class="mt-2">
  149. <a href="cart.php" class="btn btn-secondary">Zurück zum Warenkorb</a>
  150. </div>
  151. </div>
  152. </div>
  153. <?php include __DIR__ . "/includes/footer.php"; ?>