[ 'title' => 'Donation Successful', 'thank_you' => 'Thank You!', 'success_msg' => 'Your donation has been successfully processed. Your generosity helps us continue our mission.', 'download_cert' => 'Download Certificate', 'return_home' => 'Return to Home', 'trans_id' => 'Transaction ID', 'status' => 'Status', 'completed' => 'Completed', 'conf_sent' => 'Confirmation messages have been sent to the relevant parties.', 'failed_title' => 'Payment Verification Failed', 'failed_msg' => 'We couldn\'t verify your payment. If you believe this is an error, please contact support.', 'back_home' => 'Back to Home' ], 'ar' => [ 'title' => 'تم التبرع بنجاح', 'thank_you' => 'شكراً لك!', 'success_msg' => 'تمت معالجة تبرعك بنجاح. سخاؤك يساعدنا في الاستمرار في مهمتنا.', 'download_cert' => 'تحميل الشهادة', 'return_home' => 'العودة للرئيسية', 'trans_id' => 'رقم المعاملة', 'status' => 'الحالة', 'completed' => 'مكتمل', 'conf_sent' => 'تم إرسال رسائل التأكيد إلى الأطراف المعنية.', 'failed_title' => 'فشل التحقق من الدفع', 'failed_msg' => 'لم نتمكن من التحقق من دفعتك. إذا كنت تعتقد أن هذا خطأ، يرجى الاتصال بالدعم.', 'back_home' => 'العودة للرئيسية' ] ]; $t = $texts[$lang]; if (!$session_id) { header('Location: index.php?lang=' . $lang); exit; } $pdo = db(); $success = false; $donation = null; // 1. Identify the donation if (strpos($session_id, 'mock_session_') === 0 && $donation_id) { // Simulation Mode $stmt = $pdo->prepare("SELECT * FROM donations WHERE id = ? AND status = 'pending'"); $stmt->execute([$donation_id]); $donation = $stmt->fetch(); } else { // Real Thawani verification $ch = curl_init(THAWANI_API_URL . '/checkout/session/' . $session_id); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'thawani-api-key: ' . THAWANI_SECRET_KEY ]); $response = curl_exec($ch); curl_close($ch); $data = json_decode($response, true); if (isset($data['success']) && $data['success'] === true && $data['data']['payment_status'] === 'paid') { $donation_id = $data['data']['client_reference_id']; $stmt = $pdo->prepare("SELECT * FROM donations WHERE id = ? AND status = 'pending'"); $stmt->execute([$donation_id]); $donation = $stmt->fetch(); } } // 2. Process success if ($donation) { // Update donation status $pdo->prepare("UPDATE donations SET status = 'completed', transaction_id = ? WHERE id = ?") ->execute([$session_id, $donation['id']]); // Update case raised amount $pdo->prepare("UPDATE cases SET raised = raised + ? WHERE id = ?") ->execute([$donation['amount'], $donation['case_id']]); // Check if goal reached and mark as completed if so $stmt = $pdo->prepare("SELECT raised, goal FROM cases WHERE id = ?"); $stmt->execute([$donation['case_id']]); $updatedCase = $stmt->fetch(); if ($updatedCase && $updatedCase['raised'] >= $updatedCase['goal']) { $pdo->prepare("UPDATE cases SET status = 'completed' WHERE id = ?") ->execute([$donation['case_id']]); } // Refresh donation data to get name/phone/gift info $stmt = $pdo->prepare("SELECT * FROM donations WHERE id = ?"); $stmt->execute([$donation['id']]); $fullDonation = $stmt->fetch(); // Send WhatsApp notifications WablasService::sendThankYou($fullDonation, $lang); WablasService::sendCaseDonationNotification($fullDonation, $lang); if ($fullDonation['is_gift']) { WablasService::sendGiftNotification($fullDonation, $lang); } $success = true; $final_donation_id = $fullDonation['id']; } else { // Check if it was already completed (user refreshed page) $stmt = $pdo->prepare("SELECT * FROM donations WHERE transaction_id = ? AND status = 'completed'"); $stmt->execute([$session_id]); $existing = $stmt->fetch(); if ($existing) { $success = true; $final_donation_id = $existing['id']; } } $org = $pdo->query("SELECT * FROM org_profile LIMIT 1")->fetch(); $orgName = ($lang === 'en') ? ($org['name_en'] ?? 'Organization') : ($org['name_ar'] ?? 'المؤسسة'); ?> <?= $t['title'] ?> - <?= htmlspecialchars($orgName) ?>