Skip to content

Feature: Donation details overview backend #7976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feature: donation statistics controller
alaca committed Jun 12, 2025
commit e6381b020d2ab1e13f47a44cbd118ba34dc01f28
125 changes: 125 additions & 0 deletions src/API/REST/V3/Routes/Donations/DonationStatisticsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace Give\API\REST\V3\Routes\Donations;

use Give\API\REST\V3\Routes\Donations\ValueObjects\DonationRoute;
use Give\Donations\Models\Donation;
use WP_Error;
use WP_REST_Controller;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;

/**
* @unreleased
*/
class DonationStatisticsController extends WP_REST_Controller
{
/**
* @unreleased
*/
public function __construct()
{
$this->namespace = DonationRoute::NAMESPACE;
$this->rest_base = DonationRoute::BASE;
}

/**
* @unreleased
*/
public function register_routes()
{
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<donationId>[\d]+)/statistics', [
[
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_item'],
'permission_callback' => [$this, 'get_item_permissions_check'],
'args' => [
'donationId' => [
'type' => 'integer',
'required' => true,
],
'mode' => [
'type' => 'string',
'default' => 'live',
'enum' => ['live', 'test'],
],
'campaignId' => [
'type' => 'integer',
'default' => 0,
],
],
],
]);
}

/**
* @unreleased
*/
public function get_item($request)
{
$donation = Donation::find($request->get_param('donationId'));
if ( ! $donation) {
return new WP_Error('donation_not_found', __('Donation not found', 'give'), ['status' => 404]);
}

$item = [
'donation' => [
'amount' => $donation->amount->formatToDecimal(),
'feeAmountRecovered' => $donation->feeAmountRecovered ? $donation->feeAmountRecovered->formatToDecimal() : 0,
'status' => $donation->status->getValue(),
'date' => $donation->createdAt->format('Y-m-d H:i:s'),
'paymentMethod' => $donation->gatewayId,
],
'donor' => [
'id' => $donation->donorId,
'name' => $donation->donor->name,
'email' => $donation->donor->email,
],
'campaign' => [
'id' => $donation->campaignId,
'title' => $donation->campaign->title,
],
];

$response = $this->prepare_item_for_response($item, $request);

return rest_ensure_response($response);
}

/**
* @unreleased
*
* @param WP_REST_Request $request
*
* @return bool
*/
public function get_item_permissions_check($request): bool
{
return current_user_can('view_give_reports');
}

/**
* @unreleased
*/
public function prepare_item_for_response($item, $request): WP_REST_Response
{
$self_url = rest_url(sprintf('%s/%s/%d/%s', $this->namespace, $this->rest_base,
$request->get_param('donationId'),
'statistics'));

$self_url = add_query_arg([
'mode' => $request->get_param('mode'),
'campaignId' => $request->get_param('campaignId'),
], $self_url);

$links = [
'self' => ['href' => $self_url],
];

$response = new WP_REST_Response($item);
$response->add_links($links);

return $response;
}
}
8 changes: 6 additions & 2 deletions src/API/REST/V3/Routes/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
use Give\API\REST\V3\Routes\Campaigns\GetCampaignStatistics;
use Give\API\REST\V3\Routes\Campaigns\RegisterCampaignRoutes;
use Give\API\REST\V3\Routes\Donations\DonationController;
use Give\API\REST\V3\Routes\Donations\DonationStatisticsController;
use Give\API\REST\V3\Routes\Donors\DonorController;
use Give\API\REST\V3\Routes\Donors\DonorNotesController;
use Give\API\REST\V3\Routes\Donors\DonorStatisticsController;
@@ -36,7 +37,7 @@ public function boot()

$this->loadCampaignsRoutes();
$this->loadDonorsRoutes();
$this->loadDonnationsRoutes();
$this->loadDonationsRoutes();
}

/**
@@ -70,11 +71,14 @@ private function loadDonorsRoutes()
/**
* @unreleased
*/
private function loadDonnationsRoutes()
private function loadDonationsRoutes()
{
add_action('rest_api_init', function () {
$donationsController = new DonationController();
$donationsController->register_routes();

$donationStatisticsController = new DonationStatisticsController();
$donationStatisticsController->register_routes();
});
}
}