97 lines
3.5 KiB
PHP
97 lines
3.5 KiB
PHP
<?php
|
|
session_start();
|
|
|
|
require_once __DIR__ . '/../Models/Order.php';
|
|
require_once __DIR__ . '/CartController.php';
|
|
require_once __DIR__ . '/../../mail/MailService.php';
|
|
|
|
class OrderController {
|
|
public function index() {
|
|
return Order::getAll();
|
|
}
|
|
|
|
public function checkout() {
|
|
public function checkout() {
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('Location: /cart.php');
|
|
exit();
|
|
}
|
|
|
|
$customerName = trim($_POST['name']);
|
|
$customerEmail = trim($_POST['email']);
|
|
|
|
if (empty($customerName) || empty($customerEmail) || !filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
|
|
// Basic validation
|
|
header('Location: /checkout.php?error=Invalid data');
|
|
exit();
|
|
}
|
|
|
|
$cartItems = CartController::getCartContents();
|
|
$cartTotal = CartController::getCartTotal();
|
|
|
|
if (empty($cartItems)) {
|
|
header('Location: /cart.php');
|
|
exit();
|
|
}
|
|
|
|
// Create Order
|
|
$orderId = Order::create($customerName, $customerEmail, $cartTotal);
|
|
|
|
// Create Order Items
|
|
foreach ($cartItems as $item) {
|
|
Order::createOrderItem($orderId, $item['product_id'], $item['quantity'], $item['price']);
|
|
}
|
|
|
|
// Clear the cart
|
|
$_SESSION['cart'] = [];
|
|
|
|
// Send confirmation emails
|
|
$this->sendConfirmationEmails($orderId, $customerName, $customerEmail, $cartItems, $cartTotal);
|
|
|
|
// Redirect to a success page
|
|
header('Location: /thank-you.php?order_id=' . $orderId);
|
|
exit();
|
|
}
|
|
|
|
private function sendConfirmationEmails($orderId, $customerName, $customerEmail, $cartItems, $cartTotal) {
|
|
// Email to Customer
|
|
$customerSubject = "Confirmação do seu Pedido #{$orderId}";
|
|
$customerHtml = "<h1>Obrigado pela sua compra, {$customerName}!</h1>";
|
|
$customerHtml .= "<p>Seu pedido #{$orderId} foi recebido e está sendo processado.</p>";
|
|
$customerHtml .= $this->formatOrderForEmail($cartItems, $cartTotal);
|
|
MailService::sendMail($customerEmail, $customerSubject, $customerHtml);
|
|
|
|
// Email to Admin
|
|
$adminEmail = getenv('MAIL_TO') ?: getenv('MAIL_FROM');
|
|
if ($adminEmail) {
|
|
$adminSubject = "Novo Pedido Recebido #{$orderId}";
|
|
$adminHtml = "<h1>Novo Pedido de {$customerName} ({$customerEmail})</h1>";
|
|
$adminHtml .= $this->formatOrderForEmail($cartItems, $cartTotal);
|
|
MailService::sendMail($adminEmail, $adminSubject, $adminHtml);
|
|
}
|
|
}
|
|
|
|
private function formatOrderForEmail($items, $total) {
|
|
$html = '<table border="1" cellpadding="10" cellspacing="0" width="100%">';
|
|
$html .= '<thead><tr><th>Produto</th><th>Qtd</th><th>Preço</th><th>Subtotal</th></tr></thead>';
|
|
$html .= '<tbody>';
|
|
foreach ($items as $item) {
|
|
$html .= sprintf('<tr><td>%s</td><td>%d</td><td>R$ %.2f</td><td>R$ %.2f</td></tr>',
|
|
htmlspecialchars($item['name']),
|
|
$item['quantity'],
|
|
$item['price'],
|
|
$item['price'] * $item['quantity']
|
|
);
|
|
}
|
|
$html .= '</tbody>';
|
|
$html .= sprintf('<tfoot><tr><th colspan="3" align="right">Total:</th><th>R$ %.2f</th></tr></tfoot>', $total);
|
|
$html .= '</table>';
|
|
return $html;
|
|
}
|
|
}
|
|
|
|
if (isset($_POST['action']) && $_POST['action'] === 'checkout') {
|
|
$orderController = new OrderController();
|
|
$orderController->checkout();
|
|
}
|
|
?>
|