Helper Functions & Blade Directives
SaaSykit offers helper functions/directives to ease your life when building your SaaS application.
PHP Helpersโ
User::isSubscribed()
โ
This helper function returns true
if the user is subscribed to a plan, false
otherwise.
You can also pass in the subscription product slug to the function to check if the user is subscribed to that specific product.
$user->isSubscribed('product-slug');
For SaaSykit Tenancy users, you can also pass in the Tenant
to the function to check if the user is subscribed to a specific tenant.
$user->isSubscribed('product-slug', $tenant);
User::isTrialing()
โ
This helper function returns true
if the user is trialing (subscribed to a plan and in trial period), false
otherwise.
You can also pass in the subscription product slug to the function to check if the user is trialing that specific product.
$user->isTrialing('product-slug');
For SaaSykit Tenancy users, you can also pass in the Tenant
to the function to check if the user is trialing a specific tenant.
$user->isTrialing('product-slug', $tenant);
User::hasPurchased()
โ
This helper function returns true
if the user has purchased a product, false
otherwise.
You can also pass in the one-time purchase product slug to the function to check if the user has purchased that specific product.
$user->hasPurchased('product-slug');
For SaaSykit Tenancy users, you can also pass in the Tenant
to the function to check if the user has purchased a product in a specific tenant.
$user->hasPurchased('product-slug', $tenant);
Report usage-based subscription usage to payment providerโ
SubscriptionUsageManager::reportUsage(int $unitCount, ?Subscription $subscription = null): bool
This helper function is used to report usage for a usage-based plan to payment provider. It takes in the number of units used and the subscription to report the usage for.
$subscriptionUsageManager->reportUsage(10, $subscription);
You should call this function once your user has consumed the units.
If you don't pass in the subscription, the function will pick the first usage-based subscription the user is subscribed to.
This function will return true
if the usage was reported successfully, false
otherwise.
SubscriptionManager::getUserSubscriptionProductMetadata(User $user)
โ
This helper in only supported in SaaSykit, see below for the SaaSykit Tenancy version of this helper.
Each product can have a set of metadata that you define in the Admin Panel that is accessible to your application via this helper function.
You can use this metadata to store information about the product that you want to access in your application. For example, you can store the number of users allowed in the product, and then use this helper function to access that information in your application and display it to your users or check against it.
$metadata = $subscriptionManager->getUserSubscriptionProductMetadata($user);
This function will return an array of metadata for the user's subscription product.
[
'metadata_key1' => 'metadata_value1',
'metadata_key2' => 'metadata_value2',
]
If you enabled "Multiple Subscription Products" in the Admin Panel, this function will return an array of metadata for all the subscription products the user is subscribed to in the form of an array of arrays.
[
'product_slug1' => [
'metadata_key1' => 'metadata_value1',
'metadata_key2' => 'metadata_value2',
],
'product_slug2' => [
'metadata_key1' => 'metadata_value1',
'metadata_key2' => 'metadata_value2',
],
]
You can also get the user subscription product metadata by using the function User::subscriptionProductMetadata(). So you can retrieve the metadata of the subscription product(s) of current logged in user by calling auth()->user()->subscriptionProductMetadata()
.
Refer back to Products for more information about that.
SubscriptionManager::getTenantSubscriptionProductMetadataโ
This helper in only supported in SaaSykit Tenancy, see above for the SaaSykit version of this helper.
Just like the SubscriptionManager::getUserSubscriptionProductMetadata
helper function above, this function will return the metadata of the subscription product(s) of a specific tenant.
$metadata = $subscriptionManager->getTenantSubscriptionProductMetadata($tenant);
This function will return an array of metadata for the tenant's subscription product.
[
'metadata_key1' => 'metadata_value1',
'metadata_key2' => 'metadata_value2',
]
Blade Directivesโ
@subscribed
โ
This directive will act as an if-statement and render the content if the user is subscribed to a plan.
@subscribed
<p>You are subscribed to a plan.</p>
@endsubscribed
You can also pass in the subscription product slug to the directive to check if the user is subscribed to that specific product.
@subscribed('product-slug')
<p>You are subscribed to the product with the slug 'product-slug'.</p>
@endsubscribed
For SaaSykit Tenancy users, you can also pass in the Tenant
to the directive to check if the user is subscribed to a specific tenant.
@subscribed('product-slug', $tenant)
<p>You are subscribed to the product with the slug 'product-slug' in the tenant.</p>
@endsubscribed
@notsubscribed
โ
This directive will render the content if the user is not subscribed to a plan.
@notsubscribed
<p>You are not subscribed to a plan.</p>
@endnotsubscribed
You can also pass in the subscription product slug to the directive to check if the user is not subscribed to that specific product.
@notsubscribed('product-slug')
<p>You are not subscribed to the product with the slug 'product-slug'.</p>
@endnotsubscribed
For SaaSykit Tenancy users, you can also pass in the Tenant
to the directive to check if the user is not subscribed to a specific tenant.
@notsubscribed('product-slug', $tenant)
<p>You are not subscribed to the product with the slug 'product-slug' in the tenant.</p>
@endnotsubscribed
@trialing
โ
This directive will render the content if the user is trialing.
@trialing
<p>You are trialing.</p>
@endtrialing
Same as the @subscribed
directive, you can also pass in the subscription product slug to the directive to check if the user is trialing that specific product.
@trialing('product-slug')
<p>You are trialing the product with the slug 'product-slug'.</p>
@endtrialing
For SaaSykit Tenancy users, you can also pass in the Tenant
to the directive to check if the user is trialing a specific tenant.
@trialing('product-slug', $tenant)
<p>You are trialing the product with the slug 'product-slug' in the tenant.</p>
@endtrialing
purchasedโ
This directive will render the content if the user has purchased a product.
@purchased
<p>You have purchased a product.</p>
@endpurchased
You can also pass in the one-time purchase product slug to the directive to check if the user has purchased that specific product.
@purchased('product-slug')
<p>You have purchased the product with the slug 'product-slug'.</p>
@endpurchased
For SaaSykit Tenancy users, you can also pass in the Tenant
to the directive to check if the user has purchased a product in a specific tenant.
@purchased('product-slug', $tenant)
<p>You have purchased the product with the slug 'product-slug' in the tenant.</p>
@endpurchased