query("SELECT site_title, contact_email FROM theme_settings WHERE id=1"); $webSet = $stmtSet->fetch(PDO::FETCH_ASSOC); $site_title = $webSet['site_title'] ?? 'SejiStore'; $admin_email = $webSet['contact_email'] ?? 'noreply@sejistore.com'; } catch (Exception $e) { $site_title = 'SejiStore'; $admin_email = 'noreply@sejistore.com'; } // Receive JSON Data from JavaScript $data = json_decode(file_get_contents('php://input'), true); if ($data) { $payment_id = $data['payment_id']; $cart = $data['cart']; $cust = $data['user']; $create_account = $data['create_account']; $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 0; $generated_password = ""; $account_msg = ""; // --- 1. HANDLE USER CREATION (If requested & not logged in) --- if ($user_id == 0 && $create_account) { // Check if email exists $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?"); $stmt->execute([$cust['email']]); $existing = $stmt->fetch(); if ($existing) { $user_id = $existing['id']; // Link to existing account } else { // Create New Account $generated_password = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyz"), 0, 8); // Random Password $hashed_password = password_hash($generated_password, PASSWORD_DEFAULT); $sql = "INSERT INTO users (name, email, phone, password) VALUES (?, ?, ?, ?)"; $stmt = $pdo->prepare($sql); if($stmt->execute([$cust['name'], $cust['email'], $cust['phone'], $hashed_password])) { $user_id = $pdo->lastInsertId(); // Send Credentials Email $subject = "Your Account & Login Details - $site_title"; $msg_body = "

Welcome to $site_title!

An account has been created for you during checkout.

Email: {$cust['email']}
Password: $generated_password

Login Here

"; $headers = "MIME-Version: 1.0" . "\r\n" . "Content-type:text/html;charset=UTF-8" . "\r\n" . "From: $admin_email"; mail($cust['email'], $subject, $msg_body, $headers); $account_msg = "Account created! Password sent to email."; } } } // --- 2. SAVE ORDER & SEND DOWNLOAD EMAIL --- $order_list_html = ""; // --- 3. SEND ORDER RECEIPT EMAIL --- $to = $cust['email']; $subject = "Order Confirmation & Downloads - $site_title"; $message = " Order Receipt

Thank you for your purchase!

Hi {$cust['name']}, here are your order details:

Transaction ID: $payment_id


Your Downloads:

$order_list_html

You can also access these files from your dashboard.

"; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; $headers .= "From: $site_title <$admin_email>" . "\r\n"; if(mail($to, $subject, $message, $headers)) { echo json_encode(['status' => 'success', 'message' => 'Order placed & Email sent! ' . $account_msg]); } else { echo json_encode(['status' => 'success', 'message' => 'Order placed, but email failed (Server Issue).']); } } ?>