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 = "
An account has been created for you during checkout.
Email: {$cust['email']}
Password: $generated_password
Hi {$cust['name']}, here are your order details:
Transaction ID: $payment_id
You can also access these files from your dashboard.