%PDF- %GIF98; %PNG; .
Cyber Programmer
Logo of a company Server : Apache
System : Linux sh00666.bluehost.com 5.14.0-687.26.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jul 14 16:32:02 EDT 2026 x86_64
User : ndkhermy ( 1200)
PHP Version : 8.5.12
Disable Function : NONE
Directory :  /home1/ndkhermy/public_html/website_6ca89795/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home1/ndkhermy/public_html/website_6ca89795/process-order.php
<?php
ob_start();
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('dbcon.php');
/*
|--------------------------------------------------------------------------
| User / Guest
|--------------------------------------------------------------------------
*/
$user_id = isset($_SESSION['id']) ? (int)$_SESSION['id'] : 0;
if (!isset($_SESSION['session_id'])) {
    $_SESSION['session_id'] = session_create_id();
}
$session_id = $_SESSION['session_id'];
$guest_id   = ($user_id == 0) ? $session_id : null;

/*
|--------------------------------------------------------------------------
| Get Cart Items
|--------------------------------------------------------------------------
*/
if ($user_id > 0) {
    $stmt = $conn->prepare("
        SELECT ci.id, ci.product_id, ci.quantity, p.price
        FROM cart_items ci
        JOIN areawecover p ON ci.product_id = p.id
        WHERE ci.user_id = ?
    ");
    $stmt->bind_param("i", $user_id);
} else {
    $stmt = $conn->prepare("
        SELECT ci.id, ci.product_id, ci.quantity, p.price
        FROM cart_items ci
        JOIN areawecover p ON ci.product_id = p.id
        WHERE ci.session_id = ?
    ");
    $stmt->bind_param("s", $session_id);
}

$stmt->execute();
$cart_items = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

if (empty($cart_items)) {
    die("Cart is empty");
}

/*
|--------------------------------------------------------------------------
| Billing Details
|--------------------------------------------------------------------------
*/
$full_name = trim($_POST['full_name'] ?? '');
$email     = trim($_POST['email'] ?? '');
$phone     = trim($_POST['phone'] ?? '');
$address   = trim($_POST['address'] ?? '');

// Agar form se country/currency aa rahi hai to theek, nahi to default (E.g., UAE/AED ya SA/SAR) set karein
$country   = trim($_POST['country'] ?? 'AE'); // Example: AE for UAE, SA for Saudi Arabia
$currency  = trim($_POST['currency'] ?? 'AED'); // Example: AED or SAR

if (empty($full_name) || empty($email) || empty($phone) || empty($address)) {
    die("Please fill all required fields.");
}

/*
|--------------------------------------------------------------------------
| Payment Method
|--------------------------------------------------------------------------
*/
$payment_type = $_POST['payment_method'] ?? 'cod';

/*
|--------------------------------------------------------------------------
| Calculate Total
|--------------------------------------------------------------------------
*/
$total_amount = 0;
foreach ($cart_items as $item) {
    $total_amount += ($item['quantity'] * $item['price']);
}

/*
|--------------------------------------------------------------------------
| Save Order
|--------------------------------------------------------------------------
*/
$conn->begin_transaction();

try {
    $status = ($payment_type == 'online') ? "Pending Payment" : "Pending";

    // NOTE: Agar aapke orders table me country/currency column nahi hai, to ye check kar lein.
    $stmt = $conn->prepare("
        INSERT INTO orders
        (
            user_id, guest_id, full_name, email, phone, address, total_amount, payment_type, order_status
        )
        VALUES
        (
            ?,?,?,?,?,?,?,?,?
        )
    ");

    $stmt->bind_param(
        "isssssdss",
        $user_id, $guest_id, $full_name, $email, $phone, $address, $total_amount, $payment_type, $status
    );

    if (!$stmt->execute()) {
        throw new Exception($stmt->error);
    }

    $order_id = $conn->insert_id;
    $stmt->close();

    /*
    |--------------------------------------------------------------------------
    | Generate Unique Order ID
    |--------------------------------------------------------------------------
    */
    $unique_order_id = 'JGT' . str_pad($order_id, 6, '0', STR_PAD_LEFT);

    /*
    |--------------------------------------------------------------------------
    | Update Order Number
    |--------------------------------------------------------------------------
    */
    $stmt = $conn->prepare("
        UPDATE orders
        SET order_id = ?, unique_order_id = ?
        WHERE id = ?
    ");

    $stmt->bind_param("isi", $order_id, $unique_order_id, $order_id);
    $stmt->execute();
    $stmt->close();

    /*
    |--------------------------------------------------------------------------
    | Save Order Items
    |--------------------------------------------------------------------------
    */
    $stmt = $conn->prepare("
        INSERT INTO order_items (order_id, product_id, quantity, price)
        VALUES (?,?,?,?)
    ");

    foreach ($cart_items as $item) {
        $product_id = (int)$item['product_id'];
        $quantity   = (int)$item['quantity'];
        $price      = (float)$item['price'];

        $stmt->bind_param("iiid", $order_id, $product_id, $quantity, $price);

        if (!$stmt->execute()) {
            throw new Exception($stmt->error);
        }
    }
    $stmt->close();

    /*
    |--------------------------------------------------------------------------
    | Clear Cart (Sirf COD ke liye)
    |--------------------------------------------------------------------------
    */
    if ($payment_type != 'online') {
        if ($user_id > 0) {
            $stmt = $conn->prepare("DELETE FROM cart_items WHERE user_id = ?");
            $stmt->bind_param("i", $user_id);
        } else {
            $stmt = $conn->prepare("DELETE FROM cart_items WHERE session_id = ?");
            $stmt->bind_param("s", $session_id);
        }
        $stmt->execute();
        $stmt->close();
    }

    $conn->commit();

    /*
    |--------------------------------------------------------------------------
    | Redirect
    |--------------------------------------------------------------------------
    */
    if ($payment_type == 'online') {
        // Hum sessions me temporary country/currency save kar rahe hain taaki sumup.php use kar sake
        $_SESSION['temp_country'] = $country;
        $_SESSION['temp_currency'] = $currency;
        
        header("Location: sumup.php?order_id=" . $order_id);
        exit;
    } else {
        header("Location: thankyou.php?order_id=" . $order_id);
        exit;
    }

} catch (Exception $e) {
    $conn->rollback();
    die("Order Error: " . $e->getMessage());
}

$conn->close();
ob_end_flush();
?>

VaKeR 2022