Documentation
supastarter for Nuxtsupastarter for NuxtPayments

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 composable.

The usePurchases composable 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 specific purchase for a given plan id
<script setup lang="ts">
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");
</script>

Check for purchases on server side

You can also check for purchases and get the active plan on the server side by utilizing the createPurchasesHelper function, which will generate a bunch of helper functions based on the purchases you pass to it.

import { createPurchasesHelper } from "@repo/payments/lib/helper";

const purchases = await getPurchases({ organizationId }); // or getPurchases({ userId: user.id })
const { activePlan, hasSubscription, hasPurchase } = createPurchasesHelper(purchases);

Check for purchases of organization

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

<script setup lang="ts">
const { activeOrganization } = useActiveOrganization();
const { activePlan, hasSubscription, hasPurchase } = usePurchases(activeOrganization.value?.id);
</script>