Update: 2026-05-16 01:51:22
This commit is contained in:
50
backend/api/subscription/check_payment.php
Normal file
50
backend/api/subscription/check_payment.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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);
|
||||
$referenceCode = $input['reference_code'] ?? null;
|
||||
$fingerprint = $input['fingerprint'] ?? null;
|
||||
|
||||
if (!$referenceCode || !$fingerprint) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Missing reference_code or fingerprint']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$stmt = $pdo->prepare("SELECT status FROM cliq_payments WHERE reference_code = :ref AND fingerprint = :fingerprint LIMIT 1");
|
||||
$stmt->execute([':ref' => $referenceCode, ':fingerprint' => $fingerprint]);
|
||||
$payment = $stmt->fetch();
|
||||
|
||||
if ($payment) {
|
||||
// If it's still pending but older than 15 minutes, mark it as expired
|
||||
if ($payment['status'] === 'pending') {
|
||||
$stmtDate = $pdo->prepare("UPDATE cliq_payments SET status = 'expired' WHERE reference_code = :ref AND created_at < NOW() - INTERVAL 15 MINUTE");
|
||||
$stmtDate->execute([':ref' => $referenceCode]);
|
||||
|
||||
// Re-fetch if we just expired it
|
||||
if ($stmtDate->rowCount() > 0) {
|
||||
$payment['status'] = 'expired';
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'status' => $payment['status'] // 'pending', 'paid', or 'expired'
|
||||
]);
|
||||
} else {
|
||||
http_response_code(404);
|
||||
echo json_encode(['success' => false, 'message' => 'Payment not found']);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
|
||||
}
|
||||
50
backend/api/subscription/init_payment.php
Normal file
50
backend/api/subscription/init_payment.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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;
|
||||
$amount = $input['amount'] ?? null;
|
||||
|
||||
if (!$fingerprint || !$plan || !$amount) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Missing required fields']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Expire old pending payments for this user to avoid confusion
|
||||
try {
|
||||
$stmt = $pdo->prepare("UPDATE cliq_payments SET status = 'expired' WHERE fingerprint = :fingerprint AND status = 'pending'");
|
||||
$stmt->execute([':fingerprint' => $fingerprint]);
|
||||
|
||||
// Generate a unique 6-character reference code (e.g. JB-1A2B3C)
|
||||
$refCode = 'JB-' . strtoupper(substr(md5(uniqid(rand(), true)), 0, 6));
|
||||
|
||||
// Insert new pending payment
|
||||
$stmt = $pdo->prepare("INSERT INTO cliq_payments (fingerprint, reference_code, amount, plan, status) VALUES (:fingerprint, :refCode, :amount, :plan, 'pending')");
|
||||
$stmt->execute([
|
||||
':fingerprint' => $fingerprint,
|
||||
':refCode' => $refCode,
|
||||
':amount' => $amount,
|
||||
':plan' => $plan
|
||||
]);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'reference_code' => $refCode,
|
||||
'amount' => $amount,
|
||||
'cliq_alias' => 'JordanBot', // Change this to the actual CliQ alias
|
||||
'expires_in_minutes' => 10
|
||||
]);
|
||||
} catch (PDOException $e) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
|
||||
}
|
||||
22
backend/api/subscription/setup_cliq_db.php
Normal file
22
backend/api/subscription/setup_cliq_db.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../../config/db.php';
|
||||
|
||||
try {
|
||||
$sql = "CREATE TABLE IF NOT EXISTS cliq_payments (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
fingerprint VARCHAR(255) NOT NULL,
|
||||
reference_code VARCHAR(20) NOT NULL UNIQUE,
|
||||
amount DECIMAL(10,2) NOT NULL,
|
||||
plan VARCHAR(50) NOT NULL,
|
||||
status ENUM('pending', 'paid', 'expired') DEFAULT 'pending',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
INDEX (reference_code),
|
||||
INDEX (fingerprint)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
|
||||
|
||||
$pdo->exec($sql);
|
||||
echo "Table 'cliq_payments' created or already exists successfully.";
|
||||
} catch (PDOException $e) {
|
||||
echo "Error creating table: " . $e->getMessage();
|
||||
}
|
||||
91
backend/api/subscription/webhook_sms.php
Normal file
91
backend/api/subscription/webhook_sms.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../../config/db.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// This webhook is called by your SMS Bot application
|
||||
// Expected fields: sender (e.g., Arab Bank), message (SMS text), timestamp
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$message = $input['message'] ?? '';
|
||||
$sender = $input['sender'] ?? '';
|
||||
|
||||
if (empty($message)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'message' => 'Empty message']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 1. Log the incoming SMS for debugging
|
||||
error_log("JordanBot SMS Received: Sender: [$sender], Content: [$message]");
|
||||
|
||||
// 2. Extract Reference Code (Pattern: JB-XXXXXX)
|
||||
// Matches JB- followed by 6 alphanumeric characters
|
||||
preg_match('/JB-([A-Z0-9]{6})/', strtoupper($message), $matches);
|
||||
$refCode = isset($matches[0]) ? $matches[0] : null;
|
||||
|
||||
// 3. Extract Amount (Pattern: finds decimal numbers)
|
||||
// Note: Jordan uses 'JOD' or 'دينار'
|
||||
preg_match('/([0-9]+(\.[0-9]{2})?)/', $message, $amtMatches);
|
||||
$amountReceived = isset($amtMatches[0]) ? floatval($amtMatches[0]) : 0;
|
||||
|
||||
if (!$refCode) {
|
||||
echo json_encode(['success' => false, 'message' => 'No Reference Code found in SMS']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// 4. Find the pending payment
|
||||
$stmt = $pdo->prepare("SELECT * FROM cliq_payments WHERE reference_code = :ref AND status = 'pending' LIMIT 1");
|
||||
$stmt->execute([':ref' => $refCode]);
|
||||
$payment = $stmt->fetch();
|
||||
|
||||
if ($payment) {
|
||||
// Optional: Verify amount match (allowing for minor differences or currency symbols)
|
||||
if (abs($payment['amount'] - $amountReceived) > 0.05) {
|
||||
error_log("JordanBot: Amount mismatch for $refCode. Expected: {$payment['amount']}, Received: $amountReceived");
|
||||
// We can still proceed or mark for manual review
|
||||
}
|
||||
|
||||
$pdo->beginTransaction();
|
||||
|
||||
// 5. Update payment status
|
||||
$stmt = $pdo->prepare("UPDATE cliq_payments SET status = 'paid' WHERE id = :id");
|
||||
$stmt->execute([':id' => $payment['id']]);
|
||||
|
||||
// 6. Activate/Extend Subscription
|
||||
$fingerprint = $payment['fingerprint'];
|
||||
$plan = $payment['plan'];
|
||||
|
||||
// Calculate expiration (e.g. basic=30 days, annual=365 days)
|
||||
$days = ($plan === 'annual') ? 365 : 30;
|
||||
$expiresAt = date('Y-m-d H:i:s', strtotime("+$days days"));
|
||||
|
||||
// Check if user already has a subscription to extend it
|
||||
$stmtCheck = $pdo->prepare("SELECT id, expires_at FROM subscriptions WHERE fingerprint = :fingerprint AND is_active = 1 LIMIT 1");
|
||||
$stmtCheck->execute([':fingerprint' => $fingerprint]);
|
||||
$existing = $stmtCheck->fetch();
|
||||
|
||||
if ($existing) {
|
||||
// Extend existing
|
||||
$newExpiry = date('Y-m-d H:i:s', strtotime($existing['expires_at'] . " +$days days"));
|
||||
$stmtUpdate = $pdo->prepare("UPDATE subscriptions SET expires_at = :expiry, plan = :plan WHERE id = :id");
|
||||
$stmtUpdate->execute([':expiry' => $newExpiry, ':plan' => $plan, ':id' => $existing['id']]);
|
||||
} else {
|
||||
// Create new
|
||||
$stmtInsert = $pdo->prepare("INSERT INTO subscriptions (fingerprint, plan, expires_at, is_active) VALUES (:fingerprint, :plan, :expiry, 1)");
|
||||
$stmtInsert->execute([':fingerprint' => $fingerprint, ':plan' => $plan, ':expiry' => $expiresAt]);
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
|
||||
error_log("JordanBot: Subscription activated for $fingerprint via $refCode ($plan)");
|
||||
echo json_encode(['success' => true, 'message' => "Subscription activated for $refCode"]);
|
||||
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Reference code not found or already processed']);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
if ($pdo->inTransaction()) $pdo->rollBack();
|
||||
http_response_code(500);
|
||||
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
|
||||
}
|
||||
Reference in New Issue
Block a user