Update: 2026-06-11 19:26:42
This commit is contained in:
@@ -1,127 +0,0 @@
|
||||
<?php
|
||||
// wallet/finalize_wallet_payment.php
|
||||
|
||||
// تم حذف include_once من هنا لأنه يُفترض أن ملف الاتصال قد تم تحميله بالفعل
|
||||
// بواسطة الملف الذي يستدعي هذه الدالة (confirm_payment.php).
|
||||
// include_once "../../../jwtconnect.php";
|
||||
|
||||
define("LOG_FILE", __DIR__ . "/../logs/payment_verification.log");
|
||||
|
||||
if (!function_exists('logError')) {
|
||||
function logError($step, $message, $data = null) {
|
||||
$logDir = dirname(LOG_FILE);
|
||||
if (!is_dir($logDir)) { mkdir($logDir, 0755, true); }
|
||||
$logEntry = "[" . date('Y-m-d H:i:s') . "] STEP {$step}: {$message}";
|
||||
if ($data !== null) { $logEntry .= " | Data: " . json_encode($data, JSON_UNESCAPED_UNICODE); }
|
||||
file_put_contents(LOG_FILE, $logEntry . PHP_EOL, FILE_APPEND);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('generateToken')) {
|
||||
function generateToken($con, $driverId, $amount): ?string {
|
||||
global $secretKey;
|
||||
$data = $driverId . $amount . time() . ($secretKey ?? 'default_secret');
|
||||
$hash = hash('sha256', $data);
|
||||
$randomBytes = bin2hex(random_bytes(16));
|
||||
$token = substr($hash . $randomBytes, 0, 64);
|
||||
|
||||
$stmt = $con->prepare("INSERT INTO payment_tokens (token, driverID, dateCreated, amount) VALUES (:token, :driverID, NOW(), :amount)");
|
||||
$stmt->execute([':token' => $token, ':driverID' => $driverId, ':amount' => $amount]);
|
||||
return $stmt->rowCount() > 0 ? $token : null;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('generatePaymentID')) {
|
||||
function generatePaymentID($con, $driverId, $amount, $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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('finalizeWalletPayment')) {
|
||||
function finalizeWalletPayment($con) {
|
||||
$orderRef = $_GET['orderRef'] ?? null;
|
||||
if (empty($orderRef)) {
|
||||
logError("FINALIZE", "Missing orderRef");
|
||||
return; // لا نستخدم throw هنا لأن الخطأ داخلي
|
||||
}
|
||||
|
||||
// 1. تحقق من الدفع
|
||||
$stmt = $con->prepare("SELECT * FROM `paymentsLogSyriaDriver` WHERE order_ref = :order_ref AND status = 1 LIMIT 1");
|
||||
$stmt->execute([':order_ref' => $orderRef]);
|
||||
$payment = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$payment) {
|
||||
logError("FINALIZE", "Payment not found or not completed", ['orderRef' => $orderRef]);
|
||||
return;
|
||||
}
|
||||
|
||||
// [تحسين] استخدام معاملات لضمان سلامة البيانات
|
||||
$con->beginTransaction();
|
||||
try {
|
||||
$driverId = $payment['user_id'];
|
||||
$originalAmount = floatval($payment['amount']);
|
||||
$paymentMethod = $payment['payment_method'] ?? 'ecash';
|
||||
|
||||
// حساب المكافأة
|
||||
$bonusAmount = match ((int)$originalAmount) {
|
||||
100 => 100.0,
|
||||
200 => 210.0,
|
||||
400 => 450.0,
|
||||
1000 => 1100.0,
|
||||
default => $originalAmount,
|
||||
};
|
||||
|
||||
// إنشاء التوكنات
|
||||
$tokenDriver = generateToken($con, $driverId, $bonusAmount);
|
||||
if (!$tokenDriver) throw new Exception('Failed to generate driver token');
|
||||
|
||||
$tokenSefer = generateToken($con, $driverId, $originalAmount);
|
||||
if (!$tokenSefer) throw new Exception('Failed to generate sefer token');
|
||||
|
||||
$paymentID = generatePaymentID($con, $driverId, $bonusAmount, $paymentMethod);
|
||||
if (!$paymentID) throw new Exception('Failed to generate payment ID');
|
||||
|
||||
// driverWallet
|
||||
$insertDriver = $con->prepare("INSERT INTO driverWallet (driverID, paymentID, amount, paymentMethod) VALUES (:driverID, :paymentID, :amount, :paymentMethod)");
|
||||
$insertDriver->execute([
|
||||
':driverID' => $driverId,
|
||||
':paymentID' => $paymentID,
|
||||
':amount' => $bonusAmount,
|
||||
':paymentMethod' => $paymentMethod
|
||||
]);
|
||||
if ($insertDriver->rowCount() === 0) throw new Exception('Insert to driverWallet failed');
|
||||
|
||||
$con->prepare("UPDATE payment_tokens SET isUsed = TRUE WHERE token = :token")->execute([':token' => $tokenDriver]);
|
||||
|
||||
// seferWallet
|
||||
$insertSefer = $con->prepare("INSERT INTO seferWallet (driverId, passengerId, amount, paymentMethod, token, createdAt)
|
||||
VALUES (:driverId, :passengerId, :amount, :paymentMethod, :token, CURRENT_TIMESTAMP)");
|
||||
$insertSefer->execute([
|
||||
':driverId' => $driverId,
|
||||
':passengerId' => 'driver',
|
||||
':amount' => $originalAmount,
|
||||
':paymentMethod' => $paymentMethod,
|
||||
':token' => $tokenSefer
|
||||
]);
|
||||
if ($insertSefer->rowCount() === 0) throw new Exception('Insert to seferWallet failed');
|
||||
|
||||
$con->prepare("UPDATE payment_tokens SET isUsed = TRUE WHERE token = :token")->execute([':token' => $tokenSefer]);
|
||||
|
||||
// إذا نجحت كل العمليات، قم بتثبيتها في قاعدة البيانات
|
||||
$con->commit();
|
||||
|
||||
logError("FINALIZE", "Wallets updated successfully", ['orderRef' => $orderRef]);
|
||||
// لا نطبع نجاح هنا، الملف المستدعي هو المسؤول عن إرسال الرد النهائي
|
||||
|
||||
} catch (Throwable $e) {
|
||||
// في حالة حدوث أي خطأ، تراجع عن كل التغييرات
|
||||
$con->rollBack();
|
||||
logError("FINALIZE", "Exception during finalization: " . $e->getMessage(), ['orderRef' => $orderRef]);
|
||||
// نمرر الخطأ للملف الأعلى ليعالجه
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user