[ PHP ] How to Verify a User's Subscription to a Specific Patreon Creator via the API

Hello Patreon Community,

I am currently developing a feature for my website that interacts with the Patreon API. My goal is to check if a specific user (identified by their user ID) has an active subscription to a specific Patreon creator. Here’s what I need to accomplish:

  1. Verify User Subscription: Determine if a user with a known ID has an active subscription to a particular Patreon creator. If the user is not subscribed, I would like to provide a link that directs them to the Patreon page for subscription. If the user is already subscribed, I want to display specific content, like a message saying: “You have an active subscription to this creator!”
  2. API Implementation: I am looking for guidance on which API endpoints to use and how to structure my API requests to get this information.

I have already implemented the user authentication part and can retrieve the basic user data using the API. However, I am struggling with retrieving and interpreting the subscription data correctly.

Here’s a snippet of my current implementation (in PHP):

php

<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
session_start();
// This example shows how to have your users log in via Patreon, and acquire access and refresh tokens after logging in

require_once __DIR__.'/vendor/autoload.php';
 
use Patreon\API;
use Patreon\OAuth;

$client_id = '';      // Replace with your data
$client_secret = '';  // Replace with your data

// Set the redirect url where the user will land after oAuth. That url is where the access code will be sent as a _GET parameter. This may be any url in your app that you can accept and process the access code and login

// In this case, say, /patreon_login request uri

$redirect_uri = "https://................com/log_in_user_via_patreon.php"; // Replace http://mydomain.com/patreon_login with the url at your site which is going to receive users returning from Patreon confirmation 

// Generate the oAuth url

$href = 'https://www.patreon.com/oauth2/authorize?response_type=code&client_id=' . $client_id . '&redirect_uri=' . urlencode($redirect_uri);

// You can send an array of vars to Patreon and receive them back as they are. Ie, state vars to set the user state, app state or any other info which should be sent back and forth. 

// for example lets set final page which the user needs to land at - this may be a content the user is unlocking via oauth, or a welcome/thank you page

// Lets make it a thank you page

$state = array();

$state['final_page'] = 'https://........com/log_in_user_via_patreon.php'; // Replace http://mydomain.com/thank_you with the url that has your thank you page

// Add any number of vars you need to this array by $state['key'] = variable value

// Prepare state var. It must be json_encoded, base64_encoded and url encoded to be safe in regard to any odd chars
$state_parameters = '&state=' . urlencode( base64_encode( json_encode( $state ) ) );

// Append it to the url 

$href .= $state_parameters;

// Now place the url into a login link. Below is a very simple login link with just text. in assets/images folder, there is a button image made with official Patreon assets (login_with_patreon.php). You can also use this image as the inner html of the <a> tag instead of the text provided here

// Scopes! You must request the scopes you need to have the access token.
// In this case, we are requesting the user's identity (basic user info), user's email
// For example, if you do not request email scope while logging the user in, later you wont be able to get user's email via /identity endpoint when fetching the user details
// You can only have access to data identified with the scopes you asked. Read more at https://docs.patreon.com/#scopes

// Lets request identity of the user, and email.

$scope_parameters = '&scope=identity%20identity'.urlencode('[email]');

$href .= $scope_parameters;


if ( isset($_GET['code']) && !empty($_GET['code']) ) {
	
	$oauth_client = new OAuth($client_id, $client_secret);	
		
	$tokens = $oauth_client->get_tokens($_GET['code'], $redirect_uri);
    	
    if (isset($tokens['access_token']) && isset($tokens['refresh_token'])) {
        $access_token = $tokens['access_token'];
        $refresh_token = $tokens['refresh_token'];
        
        // Съхраняване на токените в сесия или база данни
        $_SESSION['access_token'] = $tokens['access_token'];
        $_SESSION['refresh_token'] = $tokens['refresh_token'];
        
    }
} else {
    echo '<a href="'.$href.'">Click here to login via Patreon</a>';
}

if (isset($_SESSION['access_token'])) {
    $access_token = $_SESSION['access_token'];

	$api_client = new API($access_token);

	// Return from the API can be received in either array, object or JSON formats by setting the return format. It defaults to array if not specifically set. Specifically setting return format is not necessary. Below is shown as an example of having the return parsed as an object. If there is anyone using Art4 JSON parser lib or any other parser, they can just set the API return to JSON and then have the return parsed by that parser

	// You dont need the below line if you simply want the result as an array
	#$api_client->api_return_format = 'object';

	// Now get the current user:
	$patron_response = $api_client->fetch_user();

	//print_r($patron_response);
    $id = $patron_response['data']['id'];
    $full_name = $patron_response['data']['attributes']['full_name'];
    $email = $patron_response['data']['attributes']['email'];

    echo "<br />Hello, " . $full_name . "<br />";
    echo "ID: " . $id . "<br />";
    echo "EMAIL: " . $email . "<br />";
    
    if (isset($patron_response['included'])) {
        // have subscribe
    } else {
        // no have subscribe
    }

}

Questions:

  • Which API endpoints should I use to verify a user’s subscription to a specific creator?
  • Are there specific parameters or scopes I need to include in my API request to access subscription data?
  • If a user is not subscribed, how can I generate a direct link for them to subscribe to a specific creator?

Any examples, documentation references, or general advice on how to approach this would be greatly appreciated.

Thank you in advance for your help!

/identity endpoint is ok, if you have a token approved by the user. (in this implementation, you would have that token).

You can add the memberships relations to that identity call. Check out how the WP plugin does it:

You should omit the relations and fields that you dont actually need. But a memberships include with the needed fields would be necessary for your purpose.

Then you can check the different memberships that the user has, and then match a specific membership to a creator over the creator id value.