payfrit-api/api/portal/getSettings.php
John Mizerek 66e441b295 Add portal/getSettings and portal/updateSettings PHP endpoints
Rewrites the last two production-critical CFM endpoints for the biz.payfrit.com
Lucee removal project. Both endpoints follow the existing helpers.php patterns
with queryTimed/queryOne and are added to PUBLIC_ROUTES.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 15:42:24 -07:00

68 lines
1.9 KiB
PHP

<?php
/**
* Get Business Settings
* Returns settings for the currently selected business
*
* Requires: X-Business-ID header (set by auth middleware)
*/
require_once __DIR__ . '/../helpers.php';
runAuth();
global $businessId;
if ($businessId <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'no_business_selected']);
}
try {
$biz = queryOne("
SELECT ID, Name, TaxRate, Phone
FROM Businesses
WHERE ID = ?
LIMIT 1
", [$businessId]);
if (!$biz) {
apiAbort(['OK' => false, 'ERROR' => 'business_not_found']);
}
// Get address
$addr = queryOne("
SELECT a.Line1, a.Line2, a.City, a.ZIPCode, s.Abbreviation AS State
FROM Addresses a
LEFT JOIN tt_States s ON s.ID = a.StateID
WHERE (a.BusinessID = ? OR a.ID = (SELECT AddressID FROM Businesses WHERE ID = ?))
AND a.IsDeleted = 0
LIMIT 1
", [$businessId, $businessId]);
// Get owner email
$user = queryOne("
SELECT ContactNumber, EmailAddress
FROM Users
WHERE ID = (SELECT UserID FROM Businesses WHERE ID = ?)
LIMIT 1
", [$businessId]);
$taxRate = is_numeric($biz['TaxRate']) ? (float)$biz['TaxRate'] : 0;
jsonResponse([
'OK' => true,
'SETTINGS' => [
'BusinessID' => (int)$biz['ID'],
'Name' => $biz['Name'],
'TaxRate' => $taxRate,
'TaxRatePercent' => $taxRate * 100,
'Address' => $addr['Line1'] ?? '',
'City' => $addr['City'] ?? '',
'State' => $addr['State'] ?? '',
'Zip' => $addr['ZIPCode'] ?? '',
'Phone' => $biz['Phone'] ?? '',
'Email' => $user['EmailAddress'] ?? '',
],
]);
} catch (Throwable $e) {
apiAbort(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}