false, 'ERROR' => 'missing_params', 'MESSAGE' => 'UserID is required.']); } try { $qUser = queryOne(" SELECT StripeConnectedAccountID, EmailAddress, FirstName, LastName FROM Users WHERE ID = ? ", [$userID]); if (!$qUser) { apiAbort(['OK' => false, 'ERROR' => 'user_not_found']); } $existingAccountID = trim($qUser['StripeConnectedAccountID'] ?? ''); if (!empty($existingAccountID)) { jsonResponse([ 'OK' => true, 'ACCOUNT_ID' => $existingAccountID, 'CREATED' => false, ]); } // Create new Stripe Connect Express account $stripeSecretKey = getenv('STRIPE_SECRET_KEY') ?: ''; $postFields = [ 'type' => 'express', 'country' => 'US', 'capabilities[transfers][requested]' => 'true', 'metadata[user_id]' => $userID, ]; $userEmail = trim($qUser['EmailAddress'] ?? ''); if (!empty($userEmail)) { $postFields['email'] = $userEmail; } $ch = curl_init('https://api.stripe.com/v1/accounts'); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => http_build_query($postFields), CURLOPT_USERPWD => $stripeSecretKey . ':', CURLOPT_RETURNTRANSFER => true, ]); $result = curl_exec($ch); curl_close($ch); $acctData = json_decode($result, true); if (isset($acctData['error'])) { apiAbort(['OK' => false, 'ERROR' => $acctData['error']['message']]); } $newAccountID = $acctData['id']; // Save to Users table queryTimed("UPDATE Users SET StripeConnectedAccountID = ? WHERE ID = ?", [$newAccountID, $userID]); jsonResponse([ 'OK' => true, 'ACCOUNT_ID' => $newAccountID, 'CREATED' => true, ]); } catch (Exception $e) { jsonResponse(['OK' => false, 'ERROR' => $e->getMessage()]); }