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.
78 lines
2.2 KiB
PHP
78 lines
2.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../helpers.php';
|
|
runAuth();
|
|
|
|
/**
|
|
* Create Stripe Checkout Session for activation early unlock
|
|
* POST: { UserID: int }
|
|
*/
|
|
|
|
$data = readJsonBody();
|
|
$userID = (int) ($data['UserID'] ?? 0);
|
|
|
|
global $userId;
|
|
if ($userID <= 0) $userID = $userId;
|
|
|
|
if ($userID <= 0) {
|
|
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'UserID is required.']);
|
|
}
|
|
|
|
try {
|
|
$qUser = queryOne("
|
|
SELECT ActivationBalanceCents, ActivationCapCents
|
|
FROM Users WHERE ID = ?
|
|
", [$userID]);
|
|
|
|
if (!$qUser) {
|
|
apiAbort(['OK' => false, 'ERROR' => 'user_not_found']);
|
|
}
|
|
|
|
$remainingCents = (int) $qUser['ActivationCapCents'] - (int) $qUser['ActivationBalanceCents'];
|
|
|
|
if ($remainingCents <= 0) {
|
|
jsonResponse([
|
|
'OK' => true,
|
|
'ALREADY_COMPLETE' => true,
|
|
]);
|
|
}
|
|
|
|
$stripeSecretKey = getenv('STRIPE_SECRET_KEY') ?: '';
|
|
$base = baseUrl();
|
|
|
|
$postFields = [
|
|
'mode' => 'payment',
|
|
'line_items[0][price_data][unit_amount]' => $remainingCents,
|
|
'line_items[0][price_data][currency]' => 'usd',
|
|
'line_items[0][price_data][product_data][name]' => 'Payfrit Activation',
|
|
'line_items[0][quantity]' => '1',
|
|
'success_url' => $base . '/works/stripe-return.cfm?status=success',
|
|
'cancel_url' => $base . '/works/stripe-return.cfm?status=cancel',
|
|
'metadata[user_id]' => $userID,
|
|
'metadata[type]' => 'activation_unlock',
|
|
];
|
|
|
|
$ch = curl_init('https://api.stripe.com/v1/checkout/sessions');
|
|
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);
|
|
|
|
$sessionData = json_decode($result, true);
|
|
|
|
if (isset($sessionData['error'])) {
|
|
apiAbort(['OK' => false, 'ERROR' => $sessionData['error']['message']]);
|
|
}
|
|
|
|
jsonResponse([
|
|
'OK' => true,
|
|
'CHECKOUT_URL' => $sessionData['url'],
|
|
'AMOUNT_DUE_CENTS' => $remainingCents,
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
jsonResponse(['OK' => false, 'ERROR' => $e->getMessage()]);
|
|
}
|