38394-vm/success.php
2026-02-13 08:55:01 +00:00

135 lines
6.3 KiB
PHP

<?php
require_once 'db/config.php';
require_once 'db/thawani_config.php';
require_once 'mail/WablasService.php';
$session_id = $_GET['session_id'] ?? null;
$donation_id = $_GET['donation_id'] ?? null; // For simulation
if (!$session_id) {
header('Location: index.php');
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']]);
// 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 notification to donor
WablasService::sendThankYou($fullDonation);
// Send WhatsApp notification to recipient if it's a gift
if ($fullDonation['is_gift']) {
WablasService::sendGiftNotification($fullDonation);
}
$success = true;
} else {
// Check if it was already completed (user refreshed page)
$stmt = $pdo->prepare("SELECT * FROM donations transaction_id = ? AND status = 'completed'");
$stmt->execute([$session_id]);
if ($stmt->fetch()) {
$success = true;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Donation Successful - CharityHub</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
<style>
body { background-color: #f8fafc; font-family: 'Inter', sans-serif; }
.success-card { max-width: 600px; border-radius: 24px; border: none; overflow: hidden; }
.success-icon { background: #ecfdf5; color: #10b981; width: 100px; height: 100px; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto; }
.btn-home { background: #059669; color: white; border: none; padding: 12px 40px; border-radius: 12px; font-weight: 600; transition: all 0.3s; }
.btn-home:hover { background: #047857; color: white; transform: translateY(-2px); }
</style>
</head>
<body>
<div class="container py-5">
<div class="row justify-content-center">
<div class="col-md-8 text-center">
<?php if ($success): ?>
<div class="card success-card mx-auto p-5 shadow-lg">
<div class="success-icon mb-4">
<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" fill="currentColor" class="bi bi-check-lg" viewBox="0 0 16 16">
<path d="M12.736 3.97a.733.733 0 0 1 1.047 0c.286.289.29.756.01 1.05L7.88 12.01a.733.733 0 0 1-1.065.02L3.217 8.384a.757.757 0 0 1 0-1.06.733.733 0 0 1 1.047 0l3.052 3.093 5.42-6.447a.733.733 0 0 1 0 0z"/>
</svg>
</div>
<h1 class="fw-bold mb-3">Thank You!</h1>
<p class="text-muted fs-5 mb-4">Your donation has been successfully processed. Your generosity helps us continue our mission.</p>
<div class="bg-light p-4 rounded-4 mb-4 text-start">
<div class="d-flex justify-content-between mb-2">
<span class="text-muted">Transaction ID</span>
<span class="fw-medium text-break"><?= htmlspecialchars($session_id) ?></span>
</div>
<div class="d-flex justify-content-between">
<span class="text-muted">Status</span>
<span class="badge bg-success rounded-pill px-3">Completed</span>
</div>
</div>
<p class="small text-muted mb-4">Confirmation messages have been sent to the relevant parties.</p>
<a href="index.php" class="btn btn-home">Return to Home</a>
</div>
<?php else: ?>
<div class="card border-0 shadow-lg p-5 rounded-4">
<div class="text-danger mb-4">
<svg xmlns="http://www.w3.org/2000/svg" width="80" height="80" fill="currentColor" class="bi bi-exclamation-circle-fill" viewBox="0 0 16 16">
<path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM8 4a.905.905 0 0 0-.9.995l.35 3.507a.552.552 0 0 0 1.1 0l.35-3.507A.905.905 0 0 0 8 4zm.002 6a1 1 0 1 0 0 2 1 1 0 0 0 0-2z"/>
</svg>
</div>
<h2 class="fw-bold">Payment Verification Failed</h2>
<p class="text-muted">We couldn't verify your payment. If you believe this is an error, please contact support.</p>
<a href="index.php" class="btn btn-secondary rounded-pill px-4 mt-3">Back to Home</a>
</div>
<?php endif; ?>
</div>
</div>
</div>
</body>
</html>