Skip to content
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

Feature: add new routes for donations and donors #7699

Open
wants to merge 37 commits into
base: epic/campaigns
Choose a base branch
from

Conversation

glaubersilva
Copy link
Contributor

@glaubersilva glaubersilva commented Jan 28, 2025

Related to GIVE-1392 and GIVE-1393

Description

This PR implements 4 new REST API endpoints to retrieve Donations and Donors. In the endpoints that return multiple entries, is possible to filter the returned data using custom parameters in the request and also is possible use pagination and sort the results using the page, per_page, sort and direction parameters.

Another thing to consider is that sensitive data will be returned only if the user making the request is the site administrator.

Sensitive data for donations:

$sensitiveProperties = [
    'donorIp',
    'email',
    'phone',
    'billingAddress',
];

Sensitive data for donors:

$sensitiveProperties = [
    'userId',
    'email',
    'phone',
    'additionalEmails',
];

The new endpoints to retrieve a single entry:

/give-api/v2/donations/(?P<id>[0-9]+)

/give-api/v2/donors/(?P<id>[0-9]+)

The new endpoints to retrieve multiple entries:

/give-api/v2/donations

/give-api/v2/donors

Important: These endpoints that return multiple entries allow filtering the returned data through the campaignId parameter. It's also possible to use the hideAnonymousDonations or hideAnonymousDonors parameter to exclude from the results the donations/donors that made anonymous donations. Beyond that, on the /give-api/v2/donors endpoint, it is possible to use the onlyWithDonations parameter to retrieve all donors or just the ones that have valid donations completed.

Sample request including anonymous donations in the results:

$request = new WP_REST_Request('GET' 'give-api/v2/donations');

$request->set_query_params(
    [
        'hideAnonymousDonations' => false,   
        //'campaignId' => $campaign1->id, //Uncomment this line to filter by campaign
    ]
);

Sample request including anonymous donors in the results:

$request = new WP_REST_Request('GET' 'give-api/v2/donors');

$request->set_query_params(
    [
        'hideAnonymousDonors' => false,   
        //'campaignId' => $campaign1->id, //Uncomment this line to filter by campaign
    ]
);

Sample request to retrieve the 5 most recent donations:

$request = new WP_REST_Request('GET' 'give-api/v2/donations');

$request->set_query_params(
    [
        'page' => 1,
        'per_page' => 5,
        'sort' => 'createdAt',
        'direction' => 'DESC',
        //'campaignId' => $campaign1->id, //Uncomment this line to filter by campaign
    ]
);

Sample request to retrieve the top 5 donors:

$request = new WP_REST_Request('GET' 'give-api/v2/donors');

$request->set_query_params(
    [
        'page' => 1,
        'per_page' => 5,
        'sort' => 'totalAmountDonated',
        'direction' => 'DESC',
        //'campaignId' => $campaign1->id, // Uncomment this line to filter by campaign
    ]
);

Affects

GiveWP Rest API endpoints available for public use.

Testing Instructions

In your terminal, run the following commands:

composer test -- --filter GetDonationRouteTest

composer test -- --filter GetDonationsRouteTest

composer test -- --filter GetDonorRouteTest

composer test -- --filter GetDonorsRouteTest

Pre-review Checklist

  • Acceptance criteria satisfied and marked in related issue
  • Relevant @unreleased tags included in DocBlocks
  • Includes unit tests
  • Reviewed by the designer (if follows a design)
  • Self Review of code and UX completed

@glaubersilva glaubersilva self-assigned this Jan 28, 2025
@glaubersilva glaubersilva marked this pull request as ready for review January 29, 2025 17:40
@glaubersilva glaubersilva requested a review from kjohnson January 29, 2025 17:43
Copy link
Member

@kjohnson kjohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool seeing headers and links for pagination! This is often overlooked, but good that we are starting to leverage this feature.

I added some feedback on the queries. In particular, we have the CampaignDonationQuery which we should consider using - or at least update what is here to account for subscriptions and test payments.

@JasonTheAdams
Copy link
Contributor

Out of curiosity, is the give-api namespace pre-existing? The -api suffix part feels redundant since this is part of the REST API path.

@glaubersilva
Copy link
Contributor Author

@JasonTheAdams Yes, it's a pre-existing thing.

image

@JasonTheAdams
Copy link
Contributor

I'm seriously considering recommending a give/v3 or something that deviates from the old REST API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants