This guide covers how to manage user subscriptions using the tahsil package.
To subscribe a user (or any model using the HasSubscriptions trait) to a package:
use Foysal50x\Tashil\Models\Package;
use App\Models\User;
$user = User::find(1);
$package = Package::where('slug', 'pro-monthly')->first();
// Subscribe
$subscription = app('tashil')->subscription()->subscribe($user, $package);
// Subscribe with Trial (if package supports it)
$subscription = app('tashil')->subscription()->subscribe($user, $package, withTrial: true);You can check if a user has an active subscription:
if ($user->onPlan('pro-monthly')) {
// User is on the Pro Monthly plan
}
if ($user->subscribed()) {
// User has ANY active subscription
}Cancel a subscription either immediately or at the end of the billing cycle.
$subscription = $user->subscription;
// Cancel at end of period (default)
app('tashil')->subscription()->cancel($subscription);
// Cancel immediately
app('tashil')->subscription()->cancel($subscription, immediate: true, reason: 'User request');Resume a cancelled subscription if it's within the grace period (before it fully expires).
$subscription = $user->subscription;
if ($subscription->onGracePeriod()) {
app('tashil')->subscription()->resume($subscription);
}Upgrade or downgrade a user's subscription. The old subscription is cancelled, and a new one is created.
$newPackage = Package::where('slug', 'enterprise-yearly')->first();
$newSubscription = app('tashil')->subscription()->switchPlan($subscription, $newPackage);