Update: 2026-05-15 03:09:36
This commit is contained in:
70
backend/api/subscription/activate.php
Normal file
70
backend/api/subscription/activate.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../../config/db.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['success' => false, 'message' => 'Method Not Allowed']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$fingerprint = $input['fingerprint'] ?? null;
|
||||
$plan = $input['plan'] ?? null;
|
||||
$paymentRef = $input['payment_ref'] ?? null;
|
||||
|
||||
if (!$fingerprint || !$plan) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Missing fingerprint or plan']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$validPlans = ['free', 'basic', 'pro', 'annual'];
|
||||
if (!in_array($plan, $validPlans)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid plan type']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Calculate expiration date based on plan
|
||||
$expiresAt = null;
|
||||
if ($plan === 'basic' || $plan === 'pro') {
|
||||
$expiresAt = date('Y-m-d H:i:s', strtotime('+30 days'));
|
||||
} elseif ($plan === 'annual') {
|
||||
$expiresAt = date('Y-m-d H:i:s', strtotime('+365 days'));
|
||||
}
|
||||
|
||||
try {
|
||||
$pdo->beginTransaction();
|
||||
|
||||
// Deactivate previous active subscriptions for this device
|
||||
$stmt = $pdo->prepare("UPDATE subscriptions SET is_active = 0 WHERE fingerprint = :fingerprint");
|
||||
$stmt->execute([':fingerprint' => $fingerprint]);
|
||||
|
||||
// Insert new subscription
|
||||
$stmt = $pdo->prepare("INSERT INTO subscriptions (fingerprint, plan, expires_at, payment_ref, is_active)
|
||||
VALUES (:fingerprint, :plan, :expires_at, :payment_ref, 1)");
|
||||
$stmt->execute([
|
||||
':fingerprint' => $fingerprint,
|
||||
':plan' => $plan,
|
||||
':expires_at' => $expiresAt,
|
||||
':payment_ref' => $paymentRef
|
||||
]);
|
||||
|
||||
$pdo->commit();
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Subscription activated successfully',
|
||||
'plan' => $plan,
|
||||
'expires_at' => $expiresAt
|
||||
]);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
|
||||
}
|
||||
68
backend/api/subscription/check.php
Normal file
68
backend/api/subscription/check.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../../config/db.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['success' => false, 'message' => 'Method Not Allowed']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$fingerprint = $input['fingerprint'] ?? null;
|
||||
|
||||
if (!$fingerprint) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Missing fingerprint']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. Get Subscription Status
|
||||
$stmt = $pdo->prepare("SELECT * FROM subscriptions WHERE fingerprint = :fingerprint AND is_active = 1 ORDER BY id DESC LIMIT 1");
|
||||
$stmt->execute([':fingerprint' => $fingerprint]);
|
||||
$sub = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$plan = $sub ? $sub['plan'] : 'free';
|
||||
$expiresAt = $sub ? $sub['expires_at'] : null;
|
||||
|
||||
// Check expiration
|
||||
if ($expiresAt && strtotime($expiresAt) < time()) {
|
||||
// Expired, revert to free
|
||||
$stmt = $pdo->prepare("UPDATE subscriptions SET is_active = 0 WHERE id = :id");
|
||||
$stmt->execute([':id' => $sub['id']]);
|
||||
$plan = 'free';
|
||||
$expiresAt = null;
|
||||
}
|
||||
|
||||
// 2. Get Daily Usage
|
||||
$today = date('Y-m-d');
|
||||
$stmt = $pdo->prepare("SELECT rides_accepted FROM daily_usage WHERE fingerprint = :fingerprint AND usage_date = :today");
|
||||
$stmt->execute([
|
||||
':fingerprint' => $fingerprint,
|
||||
':today' => $today
|
||||
]);
|
||||
$usage = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$ridesToday = $usage ? (int)$usage['rides_accepted'] : 0;
|
||||
|
||||
// 3. Determine limits
|
||||
$limit = 1; // Default for free
|
||||
if ($plan === 'basic') $limit = 10;
|
||||
if ($plan === 'pro' || $plan === 'annual') $limit = -1; // Unlimited
|
||||
|
||||
$canAccept = ($limit === -1) || ($ridesToday < $limit);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'plan' => $plan,
|
||||
'expires_at' => $expiresAt,
|
||||
'rides_today' => $ridesToday,
|
||||
'rides_limit' => $limit,
|
||||
'can_accept' => $canAccept
|
||||
]);
|
||||
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
|
||||
}
|
||||
Reference in New Issue
Block a user