From 6d773e2e04038c21081a00bbe73c84a2b5f67617 Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Thu, 18 Jun 2026 11:59:58 -0500 Subject: [PATCH] =?UTF-8?q?Add=20NpoDonorController=20API=20=E2=80=94=20do?= =?UTF-8?q?nor=20CRUD,=20pledges,=20recurring=20revenue=20summary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/src/Controller/NpoDonorController.php | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 source/packages/com_mokosuitenpo/api/src/Controller/NpoDonorController.php diff --git a/source/packages/com_mokosuitenpo/api/src/Controller/NpoDonorController.php b/source/packages/com_mokosuitenpo/api/src/Controller/NpoDonorController.php new file mode 100644 index 0000000..95f8b78 --- /dev/null +++ b/source/packages/com_mokosuitenpo/api/src/Controller/NpoDonorController.php @@ -0,0 +1,111 @@ +getIdentity(); + if (!$user || $user->guest || (!$user->authorise('core.admin') && !$user->authorise($action, 'com_mokosuitenpo'))) { + http_response_code(403); + echo json_encode(['error' => 'Access denied']); + Factory::getApplication()->close(); + } + } + + public function listDonors(): void + { + $this->requireAuth('npo.donors'); + $input = Factory::getApplication()->getInput(); + $model = new \Moko\Component\MokoSuiteNpo\Administrator\Model\DonorsModel(); + $donors = $model->getItems( + $input->getString('search', ''), + $input->getString('type', ''), + $input->getString('level', ''), + $input->getInt('limit', 50) + ); + $this->sendJson($donors); + } + + public function getDonor(): void + { + $this->requireAuth('npo.donors'); + $id = Factory::getApplication()->getInput()->getInt('id', 0); + $model = new \Moko\Component\MokoSuiteNpo\Administrator\Model\DonorsModel(); + $donor = $model->getDonor($id); + + if (!$donor) { + http_response_code(404); + $this->sendJson(['error' => 'Donor not found']); + return; + } + + // Get recent donations + $donationsModel = new \Moko\Component\MokoSuiteNpo\Administrator\Model\DonationsModel(); + $donor->recent_donations = $donationsModel->getItems('', '', (int) $donor->id, 0, 0, 20); + + $this->sendJson($donor); + } + + public function donorPledges(): void + { + $this->requireAuth('npo.donors'); + $donorId = Factory::getApplication()->getInput()->getInt('id', 0); + $pledges = \Moko\Plugin\System\MokoSuiteNpo\Helper\RecurringDonationHelper::getDonorPledges($donorId); + $this->sendJson($pledges); + } + + public function createPledge(): void + { + $this->requireAuth('npo.donors'); + $input = Factory::getApplication()->getInput(); + + $pledgeId = \Moko\Plugin\System\MokoSuiteNpo\Helper\RecurringDonationHelper::createPledge( + $input->getInt('donor_id', 0), + $input->getFloat('amount', 0), + $input->getString('frequency', 'monthly'), + $input->getInt('fund_id', 0), + $input->getInt('saved_payment_id', 0) ?: null + ); + + $this->sendJson(['success' => true, 'pledge_id' => $pledgeId]); + } + + public function cancelPledge(): void + { + $this->requireAuth('npo.donors'); + $pledgeId = Factory::getApplication()->getInput()->getInt('pledge_id', 0); + $result = \Moko\Plugin\System\MokoSuiteNpo\Helper\RecurringDonationHelper::cancelPledge($pledgeId); + $this->sendJson(['success' => $result]); + } + + public function recurringSummary(): void + { + $this->requireAuth('npo.donors'); + $summary = \Moko\Plugin\System\MokoSuiteNpo\Helper\RecurringDonationHelper::getRecurringSummary(); + $this->sendJson($summary); + } + + private function sendJson(mixed $data): void + { + header('Content-Type: application/json; charset=utf-8'); + echo json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE); + Factory::getApplication()->close(); + } +} -- 2.52.0