-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdonation-api.php
193 lines (171 loc) · 6.29 KB
/
donation-api.php
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
// donation-api.php
require_once __DIR__ . '/lib/autoload.php';
require_once __DIR__ . '/lib/DonationProcessor.php';
require_once __DIR__ . '/lib/Donate.php';
// Parse the request
$method = $_SERVER["REQUEST_METHOD"];
$action = isset($_GET["action"]) ? $_GET["action"] : "process";
// Get JSON data for POST/PUT requests
$data = null;
if ($method === "POST" || $method === "PUT") {
$data = json_decode(file_get_contents("php://input"), true);
}
// Initialize the donation handlers
$processor = new DonationProcessor();
$donate = new Donate(getenv("APP_ENV") === "development");
// Handle CORS
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
if ($_SERVER["REQUEST_METHOD"] === "OPTIONS") {
exit(0);
}
// Helper function to send JSON response
function sendJson($code, $data) {
http_response_code($code);
header("Content-Type: application/json");
echo json_encode($data);
exit;
}
// Route the request based on action and method
switch ($action) {
case "process":
if ($method === "POST") {
// Process a new donation
$result = $processor->processDonation($data);
sendJson(
$result["success"] ? 200 : 400,
$result
);
} else {
sendJson(405, ["error" => "Method not allowed"]);
}
break;
case "crypto":
if ($method === "GET" && isset($_GET["supported"])) {
// Get supported cryptocurrencies
$cryptos = $donate->getSupportedCryptos();
sendJson(200, [
"success" => true,
"data" => $cryptos
]);
} else if ($method === "POST") {
// Process crypto donation
try {
$result = $donate->processCryptoDonation([
"cryptoType" => $data["cryptoType"],
"amount" => $data["amount"],
"campaignId" => $data["campaignId"],
"donorInfo" => [
"name" => $data["name"] ?? "Anonymous",
"email" => $data["email"] ?? null
]
]);
sendJson(200, [
"success" => true,
"walletAddress" => $result["walletAddress"],
"network" => $result["network"],
"qrCode" => $result["qrCode"],
"instructions" => $result["instructions"],
"transactionId" => $result["transactionId"]
]);
} catch (Exception $e) {
sendJson(400, [
"success" => false,
"error" => $e->getMessage()
]);
}
} else {
sendJson(405, ["error" => "Method not allowed"]);
}
break;
case "square":
if ($method === "POST") {
// Process Square payment
try {
$result = $donate->processSquarePayment([
"nonce" => $data["nonce"],
"amount" => $data["amount"],
"currency" => "USD",
"campaignId" => $data["campaignId"],
"donorInfo" => [
"name" => $data["name"] ?? "Anonymous",
"email" => $data["email"] ?? null
]
]);
sendJson(200, [
"success" => true,
"transactionId" => $result["transactionId"]
]);
} catch (Exception $e) {
sendJson(400, [
"success" => false,
"error" => $e->getMessage()
]);
}
} else {
sendJson(405, ["error" => "Method not allowed"]);
}
break;
case "status":
if ($method === "GET") {
// Check donation status
$transactionId = $_GET["id"] ?? null;
if (!$transactionId) {
sendJson(400, ["error" => "Transaction ID required"]);
}
$result = $processor->getDonationStatus($transactionId);
sendJson(
$result["success"] ? 200 : 404,
$result
);
} else {
sendJson(405, ["error" => "Method not allowed"]);
}
break;
case "update":
if ($method === "PUT") {
// Update donation status (e.g., from webhook)
$transactionId = $_GET["id"] ?? $data["transactionId"] ?? null;
$status = $data["status"] ?? null;
if (!$transactionId || !$status) {
sendJson(400, ["error" => "Transaction ID and status required"]);
}
$result = $processor->updateDonationStatus($transactionId, $status);
sendJson(
$result["success"] ? 200 : 404,
$result
);
} else {
sendJson(405, ["error" => "Method not allowed"]);
}
break;
case "recurring":
if ($method === "GET") {
// Process due recurring donations (typically called by a cron job)
$apiKey = $_GET["key"] ?? null;
// Simple API key validation for cron job
if ($apiKey !== getenv("CRON_API_KEY")) {
sendJson(401, ["error" => "Unauthorized"]);
}
$result = $processor->processRecurringDonations();
sendJson(200, $result);
} else if ($method === "DELETE") {
// Cancel a recurring donation
$donationId = $_GET["id"] ?? null;
if (!$donationId) {
sendJson(400, ["error" => "Donation ID required"]);
}
$result = $processor->cancelRecurringDonation($donationId);
sendJson(
$result["success"] ? 200 : 404,
$result
);
} else {
sendJson(405, ["error" => "Method not allowed"]);
}
break;
default:
sendJson(404, ["error" => "Action not found"]);
}