checkout.php 5.9 KB

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