<?php
// File: web/mobile/Flutter/activate_membership.php
// Enable error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Enable CORS headers
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type");
header("Content-Type: application/json; charset=UTF-8");
require_once 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Start a database transaction
$pdo->beginTransaction();
try {
// Validate input
$requiredFields = ['mobile', 'plan', 'start_date', 'end_date', 'questions_left', 'razorpay_payment_id'];
foreach ($requiredFields as $field) {
if (!isset($_POST[$field]) || empty($_POST[$field])) {
throw new Exception("Missing required field: $field");
}
}
$mobile = $_POST['mobile'];
$plan = $_POST['plan'];
$startDate = $_POST['start_date'];
$endDate = $_POST['end_date'];
$questionsLeft = intval($_POST['questions_left']);
$razorpayPaymentId = $_POST['razorpay_payment_id'];
// Check if user has an existing active membership
$checkMembershipStmt = $pdo->prepare("
SELECT id, mem_plan_type, mem_end_date
FROM membership
WHERE mem_mobile = :mobile
AND mem_end_date >= CURDATE()
ORDER BY id DESC
LIMIT 1
");
$checkMembershipStmt->execute(['mobile' => $mobile]);
$existingMembership = $checkMembershipStmt->fetch(PDO::FETCH_ASSOC);
// Define plan priority (higher number = more priority)
$planPriority = [
'free_7_days' => 1,
'7_days' => 2,
'6_months' => 3,
'1_year' => 4
];
// If an active membership exists
if ($existingMembership) {
// Check if the new plan has higher priority
$existingPlanPriority = $planPriority[$existingMembership['mem_plan_type']] ?? 0;
$newPlanPriority = $planPriority[$plan] ?? 0;
// If new plan has lower or equal priority, throw an exception
if ($newPlanPriority <= $existingPlanPriority) {
throw new Exception("An active membership with higher or equal priority already exists");
}
// Update existing membership to inactive
$updateStmt = $pdo->prepare("
UPDATE membership
SET status = 'Expired'
WHERE id = :id
");
$updateStmt->execute(['id' => $existingMembership['id']]);
}
// Insert new membership record
$insertStmt = $pdo->prepare("
INSERT INTO membership
(mem_mobile, mem_start_date, mem_end_date, mem_question_left, status, mem_plan_type, razorpay_payment_id)
VALUES
(:mobile, :start_date, :end_date, :questions_left, 'Active', :plan_type, :razorpay_payment_id)
");
$insertStmt->execute([
'mobile' => $mobile,
'start_date' => $startDate,
'end_date' => $endDate,
'questions_left' => $questionsLeft,
'plan_type' => $plan,
'razorpay_payment_id' => $razorpayPaymentId
]);
// Commit the transaction
$pdo->commit();
// Prepare success response
$response = [
'status' => 'success',
'message' => 'Membership activated successfully',
'planDetails' => [
'start_date' => $startDate,
'end_date' => $endDate,
'questions_left' => $questionsLeft,
'plan_type' => $plan
]
];
echo json_encode($response);
} catch (Exception $e) {
// Rollback the transaction in case of error
$pdo->rollBack();
// Prepare error response
$response = [
'status' => 'error',
'message' => $e->getMessage()
];
echo json_encode($response);
exit;
}
} else {
// Method not allowed
http_response_code(405);
echo json_encode([
'status' => 'error',
'message' => 'Method Not Allowed'
]);
}
?>