payfrit-api/api/addresses/list.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

49 lines
1.5 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
global $userId;
if ($userId <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'not_logged_in', 'MESSAGE' => 'Authentication required']);
}
try {
$rows = queryTimed("
SELECT a.ID, a.IsDefaultDelivery, a.Line1, a.Line2, a.City, a.StateID,
s.Abbreviation AS StateAbbreviation, s.Name AS StateName, a.ZIPCode
FROM Addresses a
LEFT JOIN tt_States s ON a.StateID = s.ID
WHERE a.UserID = ?
AND (a.BusinessID = 0 OR a.BusinessID IS NULL)
AND a.AddressTypeID = 2
AND a.IsDeleted = 0
ORDER BY a.IsDefaultDelivery DESC, a.ID DESC
", [$userId]);
$addresses = [];
foreach ($rows as $r) {
$line2 = $r['Line2'] ?? '';
$stateAbbr = $r['StateAbbreviation'] ?? '';
$displayText = $r['Line1']
. ($line2 !== '' ? ', ' . $line2 : '')
. ', ' . $r['City'] . ', ' . $stateAbbr . ' ' . $r['ZIPCode'];
$addresses[] = [
'AddressID' => (int) $r['ID'],
'IsDefault' => (int) $r['IsDefaultDelivery'] === 1,
'Line1' => $r['Line1'],
'Line2' => $line2,
'City' => $r['City'],
'StateID' => (int) $r['StateID'],
'StateAbbr' => $stateAbbr,
'ZIPCode' => $r['ZIPCode'],
'DisplayText' => $displayText,
];
}
jsonResponse(['OK' => true, 'ADDRESSES' => $addresses]);
} catch (Exception $e) {
apiAbort(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}