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

38 lines
1.2 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
global $userId;
if ($userId <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'not_logged_in', 'MESSAGE' => 'Authentication required']);
}
$data = readJsonBody();
$addressId = (int) ($data['AddressID'] ?? ($_GET['id'] ?? 0));
if ($addressId <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'invalid_id', 'MESSAGE' => 'Address ID required']);
}
try {
$qAddr = queryOne("
SELECT Line1, Line2, City, StateID, ZIPCode
FROM Addresses WHERE ID = ? AND UserID = ? AND IsDeleted = 0
", [$addressId, $userId]);
if (!$qAddr) {
apiAbort(['OK' => false, 'ERROR' => 'not_found', 'MESSAGE' => 'Address not found']);
}
// Soft-delete all matching duplicates
queryTimed("
UPDATE Addresses SET IsDeleted = 1
WHERE UserID = ? AND Line1 = ? AND Line2 = ? AND City = ? AND StateID = ? AND ZIPCode = ? AND IsDeleted = 0
", [$userId, $qAddr['Line1'], $qAddr['Line2'] ?? '', $qAddr['City'], (int) $qAddr['StateID'], $qAddr['ZIPCode']]);
jsonResponse(['OK' => true, 'MESSAGE' => 'Address deleted']);
} catch (Exception $e) {
apiAbort(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}