payfrit-api/api/orders/getCart.php
John Mizerek 1f81d98c52 Initial PHP API migration from CFML
Complete port of all 163 API endpoints from Lucee/CFML to PHP 8.3.
Shared helpers in api/helpers.php (DB, auth, request/response, security).
PDO prepared statements throughout. Same JSON response shapes as CFML.
2026-03-14 14:26:59 -07:00

124 lines
5.2 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
/**
* Get Cart (order details with line items and calculations)
* POST: { OrderID: int }
*/
$data = readJsonBody();
$OrderID = (int) ($data['OrderID'] ?? 0);
if ($OrderID <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_orderid', 'MESSAGE' => 'OrderID is required.']);
}
try {
$qOrder = queryOne("
SELECT
o.ID, o.UUID, o.UserID, o.BusinessID, o.DeliveryMultiplier,
o.OrderTypeID, o.DeliveryFee, o.StatusID, o.AddressID, o.PaymentID,
o.PaymentStatus, o.Remarks, o.AddedOn, o.LastEditedOn, o.SubmittedOn,
o.ServicePointID,
sp.Name AS ServicePointName
FROM Orders o
LEFT JOIN ServicePoints sp ON sp.ID = o.ServicePointID
WHERE o.ID = ?
LIMIT 1
", [$OrderID]);
if (!$qOrder) {
apiAbort(['OK' => false, 'ERROR' => 'not_found', 'MESSAGE' => 'Order not found.']);
}
// Get business info
$qBusiness = queryOne(
"SELECT DeliveryFlatFee, OrderTypes, TaxRate, PayfritFee FROM Businesses WHERE ID = ? LIMIT 1",
[(int) $qOrder['BusinessID']]
);
$businessDeliveryFee = $qBusiness ? (float) $qBusiness['DeliveryFlatFee'] : 0;
$businessTaxRate = ($qBusiness && is_numeric($qBusiness['TaxRate'])) ? (float) $qBusiness['TaxRate'] : 0;
$businessPayfritFee = ($qBusiness && is_numeric($qBusiness['PayfritFee'])) ? (float) $qBusiness['PayfritFee'] : 0.05;
$businessOrderTypes = ($qBusiness && trim($qBusiness['OrderTypes'] ?? '') !== '') ? $qBusiness['OrderTypes'] : '1,2,3';
$businessOrderTypesArray = explode(',', $businessOrderTypes);
// Get line items
$qLI = queryTimed("
SELECT
oli.ID, oli.ParentOrderLineItemID, oli.OrderID, oli.ItemID,
oli.StatusID, oli.Price, oli.Quantity, oli.Remark, oli.IsDeleted, oli.AddedOn,
i.Name, i.ParentItemID, i.IsCheckedByDefault,
parent.Name AS ItemParentName
FROM OrderLineItems oli
INNER JOIN Items i ON i.ID = oli.ItemID
LEFT JOIN Items parent ON parent.ID = i.ParentItemID
WHERE oli.OrderID = ? AND oli.IsDeleted = 0
ORDER BY oli.ID
", [$OrderID]);
$rows = [];
$subtotal = 0;
foreach ($qLI as $r) {
$rows[] = [
'OrderLineItemID' => (int) $r['ID'],
'ParentOrderLineItemID' => (int) $r['ParentOrderLineItemID'],
'OrderID' => (int) $r['OrderID'],
'ItemID' => (int) $r['ItemID'],
'StatusID' => (int) $r['StatusID'],
'Price' => (float) $r['Price'],
'Quantity' => (int) $r['Quantity'],
'Remark' => $r['Remark'] ?? '',
'IsDeleted' => (int) $r['IsDeleted'],
'AddedOn' => $r['AddedOn'],
'Name' => $r['Name'] ?? '',
'ParentItemID' => (int) $r['ParentItemID'],
'ItemParentName' => $r['ItemParentName'] ?? '',
'IsCheckedByDefault' => (int) $r['IsCheckedByDefault'],
];
// Subtotal from root items only
if ((int) $r['ParentOrderLineItemID'] === 0) {
$subtotal += (float) $r['Price'] * (int) $r['Quantity'];
}
}
$taxAmount = $subtotal * $businessTaxRate;
$deliveryFee = ((int) $qOrder['OrderTypeID'] === 3) ? (float) $qOrder['DeliveryFee'] : 0;
$total = $subtotal + $taxAmount + $deliveryFee;
jsonResponse([
'OK' => true,
'ERROR' => '',
'ORDER' => [
'OrderID' => (int) $qOrder['ID'],
'UUID' => $qOrder['UUID'] ?? '',
'UserID' => (int) $qOrder['UserID'],
'BusinessID' => (int) $qOrder['BusinessID'],
'DeliveryMultiplier' => (float) $qOrder['DeliveryMultiplier'],
'OrderTypeID' => (int) $qOrder['OrderTypeID'],
'DeliveryFee' => $deliveryFee,
'BusinessDeliveryFee' => $businessDeliveryFee,
'TaxRate' => $businessTaxRate,
'PayfritFee' => $businessPayfritFee,
'Subtotal' => $subtotal,
'Tax' => $taxAmount,
'Total' => $total,
'OrderTypes' => $businessOrderTypesArray,
'StatusID' => (int) $qOrder['StatusID'],
'AddressID' => (int) ($qOrder['AddressID'] ?? 0),
'PaymentID' => (int) ($qOrder['PaymentID'] ?? 0),
'PaymentStatus' => $qOrder['PaymentStatus'] ?? '',
'Remarks' => $qOrder['Remarks'] ?? '',
'AddedOn' => $qOrder['AddedOn'],
'LastEditedOn' => $qOrder['LastEditedOn'],
'SubmittedOn' => $qOrder['SubmittedOn'],
'ServicePointID' => (int) ($qOrder['ServicePointID'] ?? 0),
'ServicePointName' => $qOrder['ServicePointName'] ?? '',
],
'ORDERLINEITEMS' => $rows,
]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => 'DB error loading cart', 'DETAIL' => $e->getMessage()]);
}