This API provides complete donation management functionality, replacing the JavaScript array logic with a server-side PHP solution backed by MySQL database.
Endpoint: POST /api/donations/create.php
Parameters:
donor_id(int, required) - ID of the donorprogram(string, required) - Program namedonation_plan(string, required) - "one-time" or "monthly"amount(float, required) - Donation amountpayment_method(string, required) - "credit_card", "paypal", "mobile", or "bank_transfer"transaction_id(string, optional) - Transaction referencepayment_details(JSON, optional) - Additional payment details
Example Request:
const donationData = {
donor_id: 1,
program: 'Education',
donation_plan: 'one-time',
amount: 100.00,
payment_method: 'credit_card',
transaction_id: 'TXN123456'
};
const result = await donationManager.createDonation(donationData);Response:
{
"success": true,
"message": "Donation created successfully",
"donation_id": 45,
"data": {
"donation_id": 45,
"donor_id": 1,
"program": "Education",
"donation_plan": "one-time",
"amount": 100.00,
"payment_method": "credit_card",
"status": "pending",
"donation_date": "2026-05-05 10:30:00"
}
}Endpoint: GET /api/donations/list.php
Query Parameters:
donor_id(int, optional) - Filter by donorprogram(string, optional) - Filter by program namestatus(string, optional) - Filter by status (pending, completed, failed, cancelled, processing)limit(int, default: 100) - Number of results per pageoffset(int, default: 0) - Pagination offsetsort_by(string, default: "donation_date") - Sort columnsort_order(string, default: "DESC") - Sort order (ASC/DESC)
Example Request:
const result = await donationManager.getDonations({
program: 'Education',
status: 'completed',
limit: 20,
offset: 0
});Response:
{
"success": true,
"data": [
{
"donation_id": 45,
"donor_id": 1,
"donor_name": "John Doe",
"donor_email": "john@example.com",
"program": "Education",
"donation_plan": "one-time",
"amount": 100.00,
"payment_method": "credit_card",
"status": "completed",
"transaction_id": "TXN123456",
"donation_date": "2026-05-05 10:30:00",
"updated_at": "2026-05-05 10:30:00"
}
],
"pagination": {
"total": 150,
"limit": 20,
"offset": 0,
"page": 1
}
}Endpoint: GET /api/donations/get.php
Query Parameters:
donation_id(int, required) - Donation ID
Example Request:
const result = await donationManager.getDonation(45);Response:
{
"success": true,
"data": {
"donation_id": 45,
"donor_id": 1,
"donor_name": "John Doe",
"donor_email": "john@example.com",
"donor_phone": "+1234567890",
"country": "USA",
"city": "New York",
"program": "Education",
"donation_plan": "one-time",
"amount": 100.00,
"payment_method": "credit_card",
"payment_details": {
"card_last_four": "4242",
"authorization_code": "AUTH123"
},
"status": "completed",
"transaction_id": "TXN123456",
"donation_date": "2026-05-05 10:30:00",
"updated_at": "2026-05-05 10:30:00"
}
}Endpoint: POST /api/donations/update.php
Parameters:
donation_id(int, required) - Donation IDstatus(string, optional) - New statustransaction_id(string, optional) - Transaction IDpayment_details(JSON, optional) - Updated payment details
Valid Status Values:
pending- Initial stateprocessing- Payment processingcompleted- Successfully completedfailed- Payment failedcancelled- Donation cancelled
Example Request:
const result = await donationManager.updateDonation(45, {
status: 'completed',
transaction_id: 'TXN123456'
});Response:
{
"success": true,
"message": "Donation updated successfully",
"donation_id": 45
}Endpoint: POST /api/donations/delete.php
Parameters:
donation_id(int, required) - Donation ID
Example Request:
const result = await donationManager.deleteDonation(45);Response:
{
"success": true,
"message": "Donation deleted successfully",
"donation_id": 45
}Endpoint: GET /api/donations/stats.php
Query Parameters:
donor_id(int, optional) - Filter statistics by donorprogram(string, optional) - Filter statistics by program
Example Request:
const result = await donationManager.getStatistics({
program: 'Education'
});Response:
{
"success": true,
"summary": {
"total_donations": 150,
"completed_donations": 120,
"pending_donations": 25,
"failed_donations": 5,
"total_amount": 15000.50,
"completed_amount": 12500.00,
"average_amount": 100.00,
"max_amount": 5000.00,
"min_amount": 10.00
},
"by_program": [
{
"program": "Education",
"count": 50,
"total": 5000.00,
"completed_total": 4500.00
}
],
"by_status": [
{
"status": "completed",
"count": 120,
"total": 12500.00
}
]
}<form id="donationForm">
<input type="hidden" id="donor_id" value="1">
<select id="program" required>
<option value="">Select Program</option>
<option value="Education">Education</option>
<option value="Healthcare">Healthcare</option>
</select>
<select id="donation_plan" required>
<option value="one-time">One-time</option>
<option value="monthly">Monthly</option>
</select>
<input type="number" id="amount" placeholder="Amount" step="0.01" required>
<select id="payment_method" required>
<option value="">Select Payment Method</option>
<option value="credit_card">Credit Card</option>
<option value="paypal">PayPal</option>
<option value="mobile">Mobile Payment</option>
</select>
<button type="submit">Donate</button>
</form><div id="donationsContainer"></div>
<script>
// Load donations on page load
document.addEventListener('DOMContentLoaded', () => {
loadDonationsTable('donationsContainer', {
limit: 50
});
});
</script>// Get stats for dashboard
const stats = await donationManager.getStatistics();
// Update UI with stats
document.getElementById('totalDonations').textContent = stats.summary.total_donations;
document.getElementById('totalAmount').textContent = stats.summary.total_amount;
document.getElementById('completedAmount').textContent = stats.summary.completed_amount;The donations are stored in the donations table with the following structure:
CREATE TABLE donations (
donation_id INT PRIMARY KEY AUTO_INCREMENT,
donor_id INT NOT NULL,
program VARCHAR(100) NOT NULL,
donation_plan VARCHAR(20) NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
payment_method VARCHAR(50) NOT NULL,
payment_details JSON,
status VARCHAR(20) DEFAULT 'pending',
transaction_id VARCHAR(100),
donation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (donor_id) REFERENCES donors(donor_id) ON DELETE CASCADE
);Update the database credentials in config/db.php:
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'ngo_project');project-root/
├── api/
│ └── donations/
│ ├── create.php # Create new donation
│ ├── list.php # List all donations with filters
│ ├── get.php # Get single donation
│ ├── update.php # Update donation
│ ├── delete.php # Delete donation
│ └── stats.php # Get statistics
├── config/
│ └── db.php # Database configuration
├── includes/
│ └── session.php # Session management
└── Users-panel/
└── donation-handler.js # JavaScript client library
All endpoints return JSON responses. Check the success field to determine if the request was successful:
const result = await donationManager.createDonation(data);
if (result.success) {
// Handle success
console.log(result.data);
} else {
// Handle error
console.error(result.message);
}- All inputs are validated server-side
- Donor passwords use bcrypt hashing (Admin passwords use plain text)
- SQL prepared statements prevent SQL injection
- Implement session validation for sensitive operations
- Use HTTPS in production
- Validate payment details before processing