checkout.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 (!checkoutRateLimitTryConsume()) {
  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. clearCart();
  54. logAccess("Order created", [
  55. "order_id" => $result["order"]["id"],
  56. "customer" => $customerEmail,
  57. ]);
  58. header(
  59. "Location: order-success.php?id=" .
  60. urlencode($result["order"]["id"]),
  61. );
  62. exit();
  63. }
  64. }
  65. }
  66. }
  67. include __DIR__ . "/includes/header.php";
  68. ?>
  69. <h2>Bestellung abschließen</h2>
  70. <?php if (!empty($errors)): ?>
  71. <div class="alert alert-error">
  72. <ul class="list-indent">
  73. <?php foreach ($errors as $error): ?>
  74. <li><?php echo escape($error); ?></li>
  75. <?php endforeach; ?>
  76. </ul>
  77. </div>
  78. <?php endif; ?>
  79. <div class="checkout-grid">
  80. <div>
  81. <h3>Ihre Auswahl</h3>
  82. <?php foreach ($cartItems as $cartItem): ?>
  83. <div class="panel panel-compact">
  84. <strong><?php echo escape(
  85. $cartItem["product"]["name"],
  86. ); ?></strong><br>
  87. <?php if ($cartItem["size"] !== ""): ?>
  88. Größe: <?php echo escape($cartItem["size"]); ?><br>
  89. <?php endif; ?>
  90. <?php if ($cartItem["availability_label"] !== ""): ?>
  91. Lieferhinweis: <?php echo escape(
  92. $cartItem["availability_label"],
  93. ); ?>
  94. <?php endif; ?>
  95. </div>
  96. <?php endforeach; ?>
  97. </div>
  98. <div>
  99. <h3>Bestelldaten</h3>
  100. <form method="POST">
  101. <div class="form-group">
  102. <label for="customer_name">Name *</label>
  103. <input type="text" id="customer_name" name="customer_name" required value="<?php echo isset(
  104. $_POST['customer_name'],
  105. )
  106. ? escape($_POST['customer_name'])
  107. : ""; ?>">
  108. </div>
  109. <div class="form-group">
  110. <label for="customer_email">E-Mail-Adresse *</label>
  111. <input type="email" id="customer_email" name="customer_email" required value="<?php echo isset(
  112. $_POST['customer_email'],
  113. )
  114. ? escape($_POST['customer_email'])
  115. : ""; ?>">
  116. </div>
  117. <div class="form-group">
  118. <label for="organization_id">Organisation *</label>
  119. <select id="organization_id" name="organization_id" required>
  120. <option value="">Bitte wählen</option>
  121. <?php foreach ($organizations as $organization): ?>
  122. <option value="<?php echo escape(
  123. $organization["id"],
  124. ); ?>" <?php echo isset($_POST['organization_id']) &&
  125. $_POST['organization_id'] === $organization["id"]
  126. ? "selected"
  127. : ""; ?>>
  128. <?php echo escape($organization["label"]); ?>
  129. </option>
  130. <?php endforeach; ?>
  131. </select>
  132. </div>
  133. <div class="form-group">
  134. <label for="comment">Kommentar</label>
  135. <textarea id="comment" name="comment" rows="5"><?php echo isset(
  136. $_POST['comment'],
  137. )
  138. ? escape($_POST['comment'])
  139. : ""; ?></textarea>
  140. </div>
  141. <div class="alert alert-info">
  142. Nach dem Absenden wird die Bestellung direkt an die Gerätewarte weitergeleitet.
  143. </div>
  144. <?php echo csrfField(); ?>
  145. <button type="submit" name="create_order" class="btn btn-block">Bestellung absenden</button>
  146. </form>
  147. <div class="mt-2">
  148. <a href="cart.php" class="btn btn-secondary">Zurück zum Warenkorb</a>
  149. </div>
  150. </div>
  151. </div>
  152. <?php include __DIR__ . "/includes/footer.php"; ?>