97 lines
4.2 KiB
PHP
97 lines
4.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/app.php';
|
|
|
|
$result = trim((string) ($_GET['result'] ?? 'cancel'));
|
|
$orderId = (int) ($_GET['order_id'] ?? 0);
|
|
|
|
if ($orderId <= 0) {
|
|
redirect_to('shop.php', [
|
|
'payment_status' => 'failed',
|
|
'message' => tr('تعذر العثور على الطلب.', 'Could not find the order.'),
|
|
]);
|
|
}
|
|
|
|
$stmt = db()->prepare('SELECT * FROM online_orders WHERE id = ? LIMIT 1');
|
|
$stmt->execute([$orderId]);
|
|
$order = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
if (!$order) {
|
|
redirect_to('shop.php', [
|
|
'payment_status' => 'failed',
|
|
'message' => tr('الطلب غير موجود.', 'The order was not found.'),
|
|
]);
|
|
}
|
|
|
|
$items = json_decode((string) ($order['items_json'] ?? '[]'), true) ?: [];
|
|
$oldStatus = (string) ($order['status'] ?? 'pending');
|
|
$currentPaymentStatus = (string) ($order['payment_status'] ?? 'unpaid');
|
|
$sessionId = trim((string) ($order['gateway_session_id'] ?? $_GET['session_id'] ?? ''));
|
|
|
|
$redirectStatus = 'failed';
|
|
$redirectMessage = tr('تعذر تأكيد الدفع. حاول مرة أخرى أو اختر الدفع لاحقاً.', 'We could not confirm the payment. Please try again or choose Pay Later.');
|
|
$sendCustomerWhatsapp = false;
|
|
|
|
if ($result === 'success' && $sessionId !== '') {
|
|
$verification = thawani_retrieve_session($sessionId);
|
|
if (!empty($verification['success']) && thawani_session_paid($verification)) {
|
|
$transactionId = thawani_session_transaction_id($verification);
|
|
$updateStmt = db()->prepare('UPDATE online_orders SET payment_status = ?, gateway_session_id = ?, gateway_transaction_id = ?, paid_at = NOW() WHERE id = ?');
|
|
$updateStmt->execute(['paid', $sessionId, $transactionId !== '' ? $transactionId : null, $orderId]);
|
|
|
|
$sendCustomerWhatsapp = true;
|
|
$redirectStatus = 'paid';
|
|
$redirectMessage = tr('تم الدفع بنجاح وتم استلام طلبك.', 'Payment completed successfully and your order was received.');
|
|
} else {
|
|
if ($oldStatus === 'pending') {
|
|
db()->beginTransaction();
|
|
try {
|
|
sync_online_order_stock_reservation($items, $oldStatus, $items, 'rejected');
|
|
$updateStmt = db()->prepare('UPDATE online_orders SET status = ?, payment_status = ? WHERE id = ?');
|
|
$updateStmt->execute(['rejected', 'failed', $orderId]);
|
|
db()->commit();
|
|
} catch (Throwable $e) {
|
|
if (db()->inTransaction()) {
|
|
db()->rollBack();
|
|
}
|
|
throw $e;
|
|
}
|
|
} elseif ($currentPaymentStatus !== 'paid') {
|
|
$updateStmt = db()->prepare('UPDATE online_orders SET payment_status = ? WHERE id = ?');
|
|
$updateStmt->execute(['failed', $orderId]);
|
|
}
|
|
}
|
|
} else {
|
|
if ($currentPaymentStatus !== 'paid' && $oldStatus === 'pending') {
|
|
db()->beginTransaction();
|
|
try {
|
|
sync_online_order_stock_reservation($items, $oldStatus, $items, 'rejected');
|
|
$updateStmt = db()->prepare('UPDATE online_orders SET status = ?, payment_status = ? WHERE id = ?');
|
|
$updateStmt->execute(['rejected', 'cancelled', $orderId]);
|
|
db()->commit();
|
|
} catch (Throwable $e) {
|
|
if (db()->inTransaction()) {
|
|
db()->rollBack();
|
|
}
|
|
throw $e;
|
|
}
|
|
} elseif ($currentPaymentStatus !== 'paid') {
|
|
$updateStmt = db()->prepare('UPDATE online_orders SET payment_status = ? WHERE id = ?');
|
|
$updateStmt->execute(['cancelled', $orderId]);
|
|
}
|
|
|
|
$redirectStatus = 'cancelled';
|
|
$redirectMessage = tr('تم إلغاء الدفع. يمكنك إعادة الطلب أو اختيار الدفع لاحقاً.', 'Payment was cancelled. You can place the order again or choose Pay Later.');
|
|
}
|
|
|
|
if ($sendCustomerWhatsapp && wablas_is_configured()) {
|
|
try {
|
|
wablas_notify_online_order_by_id($orderId, 'created');
|
|
} catch (Throwable $e) {
|
|
error_log('Customer WhatsApp notify failed after Thawani payment for order #' . $orderId . ': ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
redirect_to('shop.php', [
|
|
'payment_status' => $redirectStatus,
|
|
'message' => $redirectMessage,
|
|
]);
|