payfrit-api/api/stripe/status.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

71 lines
2.5 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
require_once __DIR__ . '/../config/stripe.php';
runAuth();
try {
$data = readJsonBody();
$businessID = (int) ($data['BusinessID'] ?? 0);
if ($businessID === 0) apiAbort(['OK' => false, 'ERROR' => 'BusinessID is required']);
$config = getStripeConfig();
$qBusiness = queryOne("SELECT StripeAccountID, StripeOnboardingComplete FROM Businesses WHERE ID = ?", [$businessID]);
if (!$qBusiness) apiAbort(['OK' => false, 'ERROR' => 'Business not found']);
$stripeAccountID = $qBusiness['StripeAccountID'] ?? '';
if (empty($stripeAccountID)) {
jsonResponse(['OK' => true, 'CONNECTED' => false, 'ACCOUNT_STATUS' => 'not_started']);
}
if (!empty($config['secretKey'])) {
$accountData = stripeRequest('GET', "https://api.stripe.com/v1/accounts/$stripeAccountID");
if (isset($accountData['error'])) {
jsonResponse([
'OK' => true,
'CONNECTED' => false,
'ACCOUNT_STATUS' => 'error',
'ERROR_DETAIL' => $accountData['error']['message'],
]);
}
$chargesEnabled = $accountData['charges_enabled'] ?? false;
$payoutsEnabled = $accountData['payouts_enabled'] ?? false;
$detailsSubmitted = $accountData['details_submitted'] ?? false;
if ($chargesEnabled && $payoutsEnabled) {
$accountStatus = 'active';
if (!$qBusiness['StripeOnboardingComplete']) {
queryTimed("UPDATE Businesses SET StripeOnboardingComplete = 1 WHERE ID = ?", [$businessID]);
}
} elseif ($detailsSubmitted) {
$accountStatus = 'pending_verification';
} else {
$accountStatus = 'incomplete';
}
jsonResponse([
'OK' => true,
'CONNECTED' => $chargesEnabled && $payoutsEnabled,
'ACCOUNT_STATUS' => $accountStatus,
'STRIPE_ACCOUNT_ID' => $stripeAccountID,
'CHARGES_ENABLED' => $chargesEnabled,
'PAYOUTS_ENABLED' => $payoutsEnabled,
'DETAILS_SUBMITTED' => $detailsSubmitted,
]);
}
// No Stripe key, return what we have in DB
jsonResponse([
'OK' => true,
'CONNECTED' => (int) $qBusiness['StripeOnboardingComplete'] === 1,
'ACCOUNT_STATUS' => (int) $qBusiness['StripeOnboardingComplete'] === 1 ? 'active' : 'unknown',
'STRIPE_ACCOUNT_ID' => $stripeAccountID,
]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => $e->getMessage()]);
}