Update: 2026-06-11 18:22:57
This commit is contained in:
106
walletintaleq.intaleq.xyz/v2/main/ride/mtn_new/finalize_payment.php
Executable file
106
walletintaleq.intaleq.xyz/v2/main/ride/mtn_new/finalize_payment.php
Executable file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
// --- finalize_payment.php ---
|
||||
// يحتوي على الدوال المنطقية لإضافة الرصيد للمستخدمين بعد تأكيد الدفع
|
||||
|
||||
// ملاحظة: هذا الملف لا يتم استدعاؤه مباشرة، بل يتم تضمينه في mtn_webhook_handler.php
|
||||
|
||||
/**
|
||||
* دالة مركزية لإتمام الدفع بعد التحقق منه
|
||||
* @param PDO $con اتصال قاعدة البيانات
|
||||
* @param int $invoiceId معرّف الفاتورة في جدول mtn_invoices
|
||||
* @return array نتيجة العملية
|
||||
*/
|
||||
function finalizeMtnPayment(PDO $con, int $invoiceId): array
|
||||
{
|
||||
try {
|
||||
// جلب تفاصيل الفاتورة
|
||||
$stmt = $con->prepare("SELECT * FROM `mtn_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 = 'mtn_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' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
// --- دوال مساعدة خاصة بالسائق ---
|
||||
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.');
|
||||
|
||||
// إضافة سجل محاسبي لمحفظة سفر
|
||||
$stmtSefer = $con->prepare("INSERT INTO seferWallet (driverId, passengerId, amount, paymentMethod) VALUES (:driverId, 'driver', :amount, :paymentMethod)");
|
||||
$stmtSefer->execute([':driverId' => $driverId, ':amount' => $amount, ':paymentMethod' => $paymentMethod]);
|
||||
if ($stmtSefer->rowCount() === 0) throw new Exception('Insert to seferWallet 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.');
|
||||
|
||||
// إضافة سجل محاسبي لمحفظة سفر
|
||||
$stmtSefer = $con->prepare("INSERT INTO seferWallet (passengerId, driverId, amount, paymentMethod) VALUES (:passengerId, 'passenger', :amount, :paymentMethod)");
|
||||
$stmtSefer->execute([':passengerId' => $passengerId, ':amount' => $amount, ':paymentMethod' => $paymentMethod]);
|
||||
if ($stmtSefer->rowCount() === 0) throw new Exception('Insert to seferWallet 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;
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user