Simplest way to get how much a user is pledging to my campaign

I’m using the patreon api with the php library and finding it really difficult just to get how much a user is pledging just to me. When I get the users identity it returns all pledges to my campaig and other campaigns how do I know which is mine? This seems it should be a really easy thing to achieve.

I’ve followed all the code examples in the library GIT and trawled through many topics here with no avail.

This is the array I see (with values removed) though I can’t tell which ID refers to my campaing tier:

[included] => Array
(
[0] => Array
(
[attributes] => Array
(
[currently_entitled_amount_cents] =>
[last_charge_date] =>
[last_charge_status] =>
[lifetime_support_cents] =>
[patron_status] =>
[pledge_relationship_start] =>
)

            [id] => 
            [type] => 
        )

    [1] => Array
        (
            [attributes] => Array
                (
                    [currently_entitled_amount_cents] => 
                    [last_charge_date] => 
                    [last_charge_status] => 
                    [lifetime_support_cents] => 
                    [patron_status] => 
                    [pledge_relationship_start] => 
                )

            [id] => 
            [type] => 
        )

    [2] => Array
        (
            [attributes] => Array
                (
                    [currently_entitled_amount_cents] => 
                    [last_charge_date] => 
                    [last_charge_status] => 
                    [lifetime_support_cents] => 
                    [patron_status] => 
                    [pledge_relationship_start] => 
                )

            [id] => 
            [type] => 
        )
)

[links] => Array
(
[self] =>
)
Please help I’m loosing my mind over this!!

If you are not requesting the identity.memberships scope, you should be able to only see memberships to your campaign. Then you can use the attributes of the membership to get the amount the user is pledging.

1 Like

Either what ghostrider said, or just iterate the campaigns and match the memberships to your campaign.

I’ve managed to do this thanks. Here is my code for anyone facing the same issue:

$user_pledge_data = $patron_response['included'];

                        if (isset($user_pledge_data)) {
                               
                            $correct_pledge = patreon_check_pledge ($user_pledge_data);
                               
                        }

function patreon_check_pledge($pledges) {
    // Loop through and look for pledges matching the campaign ID
    foreach ($pledges as $pledge) {
        if (isset($pledge['relationships']['campaign']['data']['id']) && $pledge['relationships']['campaign']['data']['id'] == CAMPAIGN_ID) {
            // Check if currently_entitled_amount_cents is set and greater than MIN_PLEDGE
            $pledge_amount = $pledge['attributes']['currently_entitled_amount_cents'] ?? 0;

            if ($pledge_amount > MIN_PLEDGE) {
                return true;  // Valid pledge found
            }
        }
    }
    // No valid pledge found after looping through all pledges
    return false;
}