Documentation
supastarter for Next.jsPayments

Check for purchases or subscriptions

Learn how to check for purchases or subscriptions in your application to provide access to premium features.

One of the most common use cases for payments is to provide access to premium features.

Plan ID

The plan id is used to identify a plan. It is defined in the key property of the plan object from the config file like free, pro, enterprise or lifetime from the example config.

Check for purchases or subscriptions on client side

You can check if a user has a purchase or subscription by using the usePurchases hook.

The usePurchases hook returns the following properties:

  • activePlan: The active plan of the user
  • purchases: An array of all the purchases of the user
  • hasSubscription: A function to check if the user has an active subscription. Optionally takes a plan id or an array of plan ids as argument to check for specific plans.
  • hasPurchase: A function to check if the user has a specifc purchase for a given plan id
 
function SomeComponent() {
    const { activePlan, hasSubscription, hasPurchase } = usePurchases();
 
    // check if the user has an active subscription
    const hasActiveSubscription = hasSubscription();
 
    // check if the user has a purchase for the pro plan
    const hasProPurchase = hasPurchase("pro");
    // or check if the user has a purchase for the pro plan or the enterprise plan
    const hasProOrEnterprisePurchase = hasPurchase(["pro", "enterprise"]);
 
    // check if the user has a purchase of the lifetime plan
    const hasLifetimeAccess = hasPurchase("lifetime");
}

Check for purchases on server side

You can also check for purchases and get the active plan on the server side by using the getPurchases function.

import { getPurchases } from "@saas/payments/lib/server";
import { getActivePlanFromPurchases } from "@saas/payments/lib/active-plan";
 
const purchases = await getPurchases();
 
const activePlan = getActivePlanFromPurchases(purchases);

Check for purchases of organization

Both usePurchases and getPurchases also support organizations. Simply pass the organization id as an argument.

// ... get the organization slug from the url
const organization = await getActiveOrganization(organizationSlug);
const purchases = await getPurchases(organizationId);
const { activeOrganization } = useActiveOrganization();
const { activePlan, hasSubscription, hasPurchase } = usePurchases(activeOrganization!.id);

On this page