-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayments.php
More file actions
146 lines (120 loc) · 4.42 KB
/
payments.php
File metadata and controls
146 lines (120 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
require_once 'config.php';
// Check if user is logged in and is super admin
if (!isLoggedIn()) {
sendResponse(false, 'Authentication required');
}
if (!isSuperAdmin()) {
sendResponse(false, 'Super admin access required');
}
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
if (isset($_GET['summary'])) {
getPaymentSummary();
} else {
getAllPayments();
}
break;
case 'POST':
createPayment();
break;
case 'PUT':
parse_str(file_get_contents("php://input"), $_PUT);
updatePayment($_PUT);
break;
case 'DELETE':
parse_str(file_get_contents("php://input"), $_DELETE);
deletePayment($_DELETE['id'] ?? null);
break;
default:
sendResponse(false, 'Method not allowed');
}
function getAllPayments() {
global $pdo;
try {
$stmt = $pdo->query("SELECT * FROM payments ORDER BY created_at DESC");
$payments = $stmt->fetchAll();
sendResponse(true, 'Payments retrieved successfully', $payments);
} catch(PDOException $e) {
sendResponse(false, 'Database error: ' . $e->getMessage());
}
}
function getPaymentSummary() {
global $pdo;
try {
// Get total revenue
$stmt = $pdo->query("SELECT SUM(amount) as total_revenue FROM payments WHERE status = 'completed'");
$totalRevenue = $stmt->fetch()['total_revenue'] ?? 0;
// Get total transactions
$stmt = $pdo->query("SELECT COUNT(*) as total_transactions FROM payments WHERE status = 'completed'");
$totalTransactions = $stmt->fetch()['total_transactions'] ?? 0;
// Calculate average transaction
$averageTransaction = $totalTransactions > 0 ? $totalRevenue / $totalTransactions : 0;
sendResponse(true, 'Payment summary retrieved successfully', [
'total_revenue' => number_format($totalRevenue, 2),
'total_transactions' => $totalTransactions,
'average_transaction' => number_format($averageTransaction, 2)
]);
} catch(PDOException $e) {
sendResponse(false, 'Database error: ' . $e->getMessage());
}
}
function createPayment() {
global $pdo;
$donorName = trim($_POST['donor_name'] ?? '');
$amount = $_POST['amount'] ?? 0;
$type = trim($_POST['type'] ?? '');
$status = trim($_POST['status'] ?? 'pending');
if (empty($donorName) || $amount <= 0 || empty($type)) {
sendResponse(false, 'Donor name, valid amount, and type are required');
}
try {
$stmt = $pdo->prepare("INSERT INTO payments (donor_name, amount, type, status) VALUES (?, ?, ?, ?)");
$stmt->execute([$donorName, $amount, $type, $status]);
$paymentId = $pdo->lastInsertId();
sendResponse(true, 'Payment record created successfully', ['id' => $paymentId]);
} catch(PDOException $e) {
sendResponse(false, 'Database error: ' . $e->getMessage());
}
}
function updatePayment($data) {
global $pdo;
$id = $data['id'] ?? null;
$donorName = trim($data['donor_name'] ?? '');
$amount = $data['amount'] ?? 0;
$type = trim($data['type'] ?? '');
$status = trim($data['status'] ?? '');
if (!$id || empty($donorName) || $amount <= 0 || empty($type) || empty($status)) {
sendResponse(false, 'All fields are required');
}
try {
$stmt = $pdo->prepare("UPDATE payments SET donor_name = ?, amount = ?, type = ?, status = ? WHERE id = ?");
$stmt->execute([$donorName, $amount, $type, $status, $id]);
if ($stmt->rowCount() > 0) {
sendResponse(true, 'Payment updated successfully');
} else {
sendResponse(false, 'Payment not found or no changes made');
}
} catch(PDOException $e) {
sendResponse(false, 'Database error: ' . $e->getMessage());
}
}
function deletePayment($id) {
global $pdo;
if (!$id) {
sendResponse(false, 'Payment ID is required');
}
try {
$stmt = $pdo->prepare("DELETE FROM payments WHERE id = ?");
$stmt->execute([$id]);
if ($stmt->rowCount() > 0) {
sendResponse(true, 'Payment deleted successfully');
} else {
sendResponse(false, 'Payment not found');
}
} catch(PDOException $e) {
sendResponse(false, 'Database error: ' . $e->getMessage());
}
}
?>