prepare("SELECT * FROM users WHERE id = ? AND status = 'approved'"); $stmt->execute([$_SESSION['user_id']]); $user_data = $stmt->fetch(); } catch (PDOException $e) { logError('Error fetching user data for donation form: ' . $e->getMessage()); } } // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'donate') { header('Content-Type: application/json'); if (!verifyCSRF($_POST['csrf_token'])) { logError('Invalid CSRF token in donation form submission'); echo json_encode(['success' => false, 'message' => 'Invalid CSRF token']); exit; } // Validate required fields $required_fields = ['name', 'F_name', 'mobile', 'address', 'amount', 'payment_method']; foreach ($required_fields as $field) { if (empty($_POST[$field])) { logError("Missing required field: $field"); echo json_encode(['success' => false, 'message' => "Please fill all required fields"]); exit; } } $name = sanitizeInput($_POST['name']); $father_name = sanitizeInput($_POST['F_name']); $mobile = sanitizeInput($_POST['mobile']); $email = !empty($_POST['email']) ? sanitizeInput($_POST['email']) : null; $address = sanitizeInput($_POST['address']); $pancard = !empty($_POST['pancard']) ? sanitizeInput($_POST['pancard']) : null; $amount = floatval($_POST['amount']); $payment_method = sanitizeInput($_POST['payment_method']); $order_id = !empty($_POST['order_id']) ? sanitizeInput($_POST['order_id']) : null; $payment_id = !empty($_POST['payment_id']) ? sanitizeInput($_POST['payment_id']) : null; $photo = null; $payment_proof = null; $status = $payment_method === 'online' ? 'completed' : 'pending'; $user_id = isLoggedIn() ? $_SESSION['user_id'] : 0; // Validate amount if ($amount < 10) { logError("Invalid donation amount: $amount"); echo json_encode(['success' => false, 'message' => 'Donation amount must be at least ₹10']); exit; } // Handle photo upload if (!empty($_FILES['photo']['name']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK) { $uploadResult = uploadFile($_FILES['photo'], 'img/users'); if ($uploadResult['success']) { $photo = $uploadResult['filename']; } else { logError('Photo upload failed: ' . $uploadResult['message']); echo json_encode(['success' => false, 'message' => $uploadResult['message']]); exit; } } // Handle payment proof upload for offline payments if ($payment_method === 'offline' && !empty($_FILES['payment_proof']['name']) && $_FILES['payment_proof']['error'] === UPLOAD_ERR_OK) { $uploadResult = uploadFile($_FILES['payment_proof'], 'img/payments'); if ($uploadResult['success']) { $payment_proof = $uploadResult['filename']; } else { logError('Payment proof upload failed: ' . $uploadResult['message']); echo json_encode(['success' => false, 'message' => $uploadResult['message']]); exit; } } elseif ($payment_method === 'offline' && empty($_FILES['payment_proof']['name'])) { logError('Missing payment proof for offline payment'); echo json_encode(['success' => false, 'message' => 'Payment proof is required for offline payments']); exit; } try { $stmt = $db->prepare(" INSERT INTO donations ( name, father_name, mobile, email, address, pan_card, amount, photo, order_id, payment_id, payment_proof, status, created_at, payment_method, user_id ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?, ?) "); $stmt->execute([ $name, $father_name, $mobile, $email, $address, $pancard, $amount, $photo, $order_id, $payment_id, $payment_proof, $status, $payment_method, $user_id ]); $donation_id = $db->lastInsertId(); if ($status === 'completed' && !empty($email)) { try { // Get donation data for email $donation_data = [ 'id' => $donation_id, 'name' => $name, 'father_name' => $father_name, 'mobile' => $mobile, 'email' => $email, 'address' => $address, 'pan_card' => $pancard, 'amount' => $amount, 'payment_id' => $payment_id, 'order_id' => $order_id, 'status' => $status, 'payment_method' => $payment_method, 'created_at' => date('Y-m-d H:i:s') ]; // Generate receipt HTML for email require_once 'admin/includes/receipt-generator.php'; $receipt_options = [ 'type' => 'donation', 'auto_print' => false, 'show_buttons' => false, 'download' => false, 'email_template' => true ]; $receipt_html = generateUniversalReceipt($donation_data, $receipt_options); // Send email with receipt $email_subject = "Donation Receipt - Thank You for Your Contribution"; $email_body = "
Dear {$name},
We have successfully received your donation of ₹{$amount}. Your generosity helps us continue our mission and make a positive impact.
Donation Details:
If you have any questions about your donation, please don't hesitate to contact us.
Thank you once again for your support!
Best regards,
" . SITE_NAME . " Team