67 lines
2.1 KiB
PHP
67 lines
2.1 KiB
PHP
<?php
|
|
// ============================================================
|
|
// api/payments/get_prime_status.php
|
|
// PURPOSE : جلب حالة اشتراك Siro Prime للراكب
|
|
// AUTH : JWT (passenger)
|
|
// ============================================================
|
|
|
|
require_once __DIR__ . '/../../connect.php';
|
|
|
|
$passengerId = $user_id ?? null;
|
|
if (!$passengerId || $role !== 'passenger') {
|
|
jsonError("Unauthorized");
|
|
exit;
|
|
}
|
|
|
|
$isPrime = false;
|
|
$expireAt = null;
|
|
|
|
// 1. تحقق من Redis أولاً (الأسرع)
|
|
if (isset($redis) && $redis !== null) {
|
|
try {
|
|
$cached = $redis->get("prime:passenger:{$passengerId}");
|
|
if ($cached) {
|
|
$data = json_decode($cached, true);
|
|
if (isset($data['is_prime']) && $data['is_prime'] == 1) {
|
|
$expTime = strtotime($data['expire_at'] ?? '0');
|
|
if ($expTime > time()) {
|
|
$isPrime = true;
|
|
$expireAt = $data['expire_at'];
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception $e) {}
|
|
}
|
|
|
|
// 2. إذا ما وُجد في Redis، ارجع للـ DB
|
|
if (!$isPrime) {
|
|
try {
|
|
$stmt = $con->prepare("SELECT is_prime, expire_at FROM passenger_prime_subscriptions WHERE passenger_id = :pid LIMIT 1");
|
|
$stmt->execute([':pid' => $passengerId]);
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($row && $row['is_prime'] == 1 && strtotime($row['expire_at']) > time()) {
|
|
$isPrime = true;
|
|
$expireAt = $row['expire_at'];
|
|
|
|
// تحديث Redis للمرات القادمة
|
|
if (isset($redis) && $redis !== null) {
|
|
try {
|
|
$redis->setex("prime:passenger:{$passengerId}", 3600, json_encode([
|
|
'is_prime' => 1,
|
|
'expire_at' => $expireAt
|
|
]));
|
|
} catch (Exception $e) {}
|
|
}
|
|
}
|
|
} catch (PDOException $e) {
|
|
error_log("[Prime Status] DB Error: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
jsonSuccess([
|
|
'is_prime' => $isPrime,
|
|
'expire_at' => $expireAt,
|
|
], "Prime status fetched");
|
|
?>
|