Files
Siro/walletintaleq.intaleq.xyz/v2/main/ride/cliq/finalize_payment.php
2026-06-18 16:46:30 +03:00

107 lines
5.1 KiB
PHP
Executable File

<?php
// --- finalize_payment.php ---
// يحتوي على الدوال المنطقية لإضافة الرصيد للمستخدمين بعد تأكيد الدفع
// ملاحظة: هذا الملف لا يتم استدعاؤه مباشرة، بل يتم تضمينه في mtn_webhook_handler.php
/**
* دالة مركزية لإتمام الدفع بعد التحقق منه
* @param PDO $con اتصال قاعدة البيانات
* @param int $invoiceId معرّف الفاتورة في جدول mtn_invoices
* @return array نتيجة العملية
*/
function finalizeClickPayment(PDO $con, int $invoiceId): array
{
try {
// جلب تفاصيل الفاتورة
$stmt = $con->prepare("SELECT * FROM `cliq_invoices` WHERE id = :id AND status = 'completed' LIMIT 1");
$stmt->execute([':id' => $invoiceId]);
$invoice = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$invoice) {
return ['success' => false, 'message' => 'Invoice not found or not completed.'];
}
$userType = $invoice['user_type'];
$userId = $invoice['user_id'];
$amount = (float) $invoice['amount'];
$paymentMethod = 'click_cash'; // تحديد طريقة الدفع
// تحديد ما إذا كان المستخدم سائقاً أم راكباً
if ($userType === 'driver') {
return finalizeForDriver($con, $userId, $amount, $paymentMethod);
} elseif ($userType === 'passenger') {
return finalizeForPassenger($con, $userId, $amount, $paymentMethod);
} else {
return ['success' => false, 'message' => 'Unknown user type.'];
}
} catch (Exception $e) {
error_log("Finalization Exception: " . $e->getMessage());
return ['success' => false, 'message' => 'Finalization failed'];
}
}
// --- دوال مساعدة خاصة بالسائق ---
function finalizeForDriver(PDO $con, int $driverId, float $amount, string $paymentMethod): array
{
// حساب قيمة البونص كما في الكود الأصلي
$bonusAmount = match ((int)$amount) {
10000 => 10000.0,
20000 => 21000.0,
40000 => 45000.0,
100000 => 110000.0,
default => $amount,
};
// إنشاء سجل دفع جديد والحصول على ID
$paymentID = generatePaymentID($con, $driverId, $bonusAmount, $paymentMethod);
if (!$paymentID) throw new Exception('Failed to generate payment ID for driver.');
// إضافة الرصيد لمحفظة السائق
$stmtDriver = $con->prepare("INSERT INTO driverWallet (driverID, paymentID, amount, paymentMethod) VALUES (:driverID, :paymentID, :amount, :paymentMethod)");
$stmtDriver->execute([':driverID' => $driverId, ':paymentID' => $paymentID, ':amount' => $bonusAmount, ':paymentMethod' => $paymentMethod]);
if ($stmtDriver->rowCount() === 0) throw new Exception('Insert to driverWallet failed.');
// إضافة سجل محاسبي لمحفظة سفر
$stmtSiro = $con->prepare("INSERT INTO siroWallet (driverId, passengerId, amount, paymentMethod) VALUES (:driverId, 'driver', :amount, :paymentMethod)");
$stmtSiro->execute([':driverId' => $driverId, ':amount' => $amount, ':paymentMethod' => $paymentMethod]);
if ($stmtSiro->rowCount() === 0) throw new Exception('Insert to siroWallet failed.');
return ['success' => true, 'message' => 'Driver wallets updated.'];
}
function generatePaymentID(PDO $con, string $driverId, float $amount, string $method): ?string {
$stmt = $con->prepare("INSERT INTO paymentsDriverPoints (`amount`, `payment_method`, `driverID`) VALUES (:amount, :method, :driverID)");
$stmt->execute([':driverID' => $driverId, ':amount' => $amount, ':method' => $method]);
return $stmt->rowCount() > 0 ? $con->lastInsertId() : null;
}
// --- دوال مساعدة خاصة بالراكب ---
function finalizeForPassenger(PDO $con, string $passengerId, float $amount, string $paymentMethod): array
{
// حساب البونص للراكب
$finalAmount = calculatePassengerBonus($amount);
// إضافة الرصيد لمحفظة الراكب
$stmtPassenger = $con->prepare("INSERT INTO passengerWallet (passenger_id, balance) VALUES (:id, :amount) ON DUPLICATE KEY UPDATE balance = balance + :amount");
$stmtPassenger->execute([':id' => $passengerId, ':amount' => $finalAmount]);
if ($stmtPassenger->rowCount() === 0) throw new Exception('Update passengerWallet failed.');
// إضافة سجل محاسبي لمحفظة سفر
$stmtSiro = $con->prepare("INSERT INTO siroWallet (passengerId, driverId, amount, paymentMethod) VALUES (:passengerId, 'passenger', :amount, :paymentMethod)");
$stmtSiro->execute([':passengerId' => $passengerId, ':amount' => $amount, ':paymentMethod' => $paymentMethod]);
if ($stmtSiro->rowCount() === 0) throw new Exception('Insert to siroWallet for passenger failed.');
return ['success' => true, 'message' => 'Passenger wallets updated.'];
}
function calculatePassengerBonus(float $amount): float {
if ($amount == 20000) return 20500;
if ($amount == 40000) return 42500;
if ($amount == 100000) return 104000;
return $amount;
}
?>