PHP code example of turahe / subscription

1. Go to this page and download the library: Download turahe/subscription library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

turahe / subscription example snippets




namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Turahe\Subscription\Traits\HasPlanSubscriptions;

class User extends Authenticatable
{
    use HasPlanSubscriptions;
    
    // ... your existing code
}

use Turahe\Subscription\Models\Plan;
use Turahe\Subscription\Models\PlanFeature;
use Turahe\Subscription\Enums\Interval;

// Create a plan
$plan = Plan::create([
    'name' => 'Pro Plan',
    'description' => 'Professional plan with advanced features',
    'price' => 29.99,
    'signup_fee' => 0,
    'invoice_period' => 1,
    'invoice_interval' => Interval::MONTH,
    'trial_period' => 14,
    'trial_interval' => Interval::DAY,
    'currency' => 'USD',
    'is_active' => true,
]);

// Add features to the plan
$plan->features()->saveMany([
    new PlanFeature([
        'name' => 'api_calls',
        'value' => 1000,
        'sort_order' => 1,
        'resettable_period' => 1,
        'resettable_interval' => 'month'
    ]),
    new PlanFeature([
        'name' => 'storage_gb',
        'value' => 10,
        'sort_order' => 2
    ]),
    new PlanFeature([
        'name' => 'priority_support',
        'value' => 'Y',
        'sort_order' => 3
    ])
]);

$user = User::find(1);
$plan = Plan::find(1);

// Create a new subscription
$subscription = $user->newPlanSubscription('main', $plan);

// Check if user is subscribed to a plan
$user->subscribedTo($plan->id);

// Check if subscription is active
$user->planSubscription('main')->active();

// Check if on trial
$user->planSubscription('main')->onTrial();

use Turahe\Subscription\Models\Plan;
use Turahe\Subscription\Enums\Interval;

$plan = Plan::create([
    'name' => 'Enterprise',
    'description' => 'Enterprise plan with unlimited features',
    'price' => 99.99,
    'signup_fee' => 50.00,
    'invoice_period' => 1,
    'invoice_interval' => Interval::MONTH,
    'trial_period' => 30,
    'trial_interval' => Interval::DAY,
    'grace_period' => 7,
    'grace_interval' => 'day',
    'currency' => 'USD',
    'is_active' => true,
]);

// Add features to a plan
$plan->features()->saveMany([
    new PlanFeature([
        'name' => 'users',
        'value' => 10,
        'sort_order' => 1
    ]),
    new PlanFeature([
        'name' => 'projects',
        'value' => 50,
        'sort_order' => 2
    ]),
    new PlanFeature([
        'name' => 'api_requests',
        'value' => 10000,
        'sort_order' => 3,
        'resettable_period' => 1,
        'resettable_interval' => 'month'
    ])
]);

// Get plan features
$features = $plan->features;

// Get specific feature
$feature = $plan->getFeatureBySlug('users');

$user = User::find(1);
$plan = Plan::find(1);

// Create subscription with custom start date
$subscription = $user->newPlanSubscription('main', $plan, Carbon::now()->addDays(7));

$newPlan = Plan::find(2);
$subscription = $user->planSubscription('main');

// Change to new plan
$subscription->changePlan($newPlan);

// Cancel at period end (default)
$user->planSubscription('main')->cancel();

// Cancel immediately
$user->planSubscription('main')->cancel(true);

// Renew subscription
$user->planSubscription('main')->renew();

// Check if user can use a feature
$canUse = $user->planSubscription('main')->canUseFeature('api_calls');

// Get remaining uses
$remaining = $user->planSubscription('main')->getFeatureRemainings('api_calls');

// Get current usage
$usage = $user->planSubscription('main')->getFeatureUsage('api_calls');

// Record single usage
$user->planSubscription('main')->recordFeatureUsage('api_calls');

// Record multiple uses
$user->planSubscription('main')->recordFeatureUsage('api_calls', 5);

// Override usage (non-incremental)
$user->planSubscription('main')->recordFeatureUsage('api_calls', 10, false);

// Reduce usage by 1
$user->planSubscription('main')->reduceFeatureUsage('api_calls');

// Reduce usage by specific amount
$user->planSubscription('main')->reduceFeatureUsage('api_calls', 3);

// Get all active subscriptions for a user
$activeSubscriptions = $user->activePlanSubscriptions();

// Get subscribed plans
$subscribedPlans = $user->subscribedPlans();

use Turahe\Subscription\Models\PlanSubscription;

// Get subscriptions by plan
$subscriptions = PlanSubscription::byPlanId($planId)->get();

// Get user's subscriptions
$userSubscriptions = PlanSubscription::ofSubscriber($user)->get();

// Get subscriptions ending trial in 3 days
$endingTrials = PlanSubscription::findEndingTrial(3)->get();

// Get subscriptions ending period in 7 days
$endingPeriods = PlanSubscription::findEndingPeriod(7)->get();

return [
    'tables' => [
        'plans' => 'plans',
        'features' => 'plan_features',
        'subscriptions' => 'plan_subscriptions',
        'subscription_usage' => 'plan_subscription_usage',
    ],
    
    'models' => [
        'plan' => \Turahe\Subscription\Models\Plan::class,
        'feature' => \Turahe\Subscription\Models\PlanFeature::class,
        'subscription' => \Turahe\Subscription\Models\PlanSubscription::class,
        'subscription_usage' => \Turahe\Subscription\Models\PlanSubscriptionUsage::class,
    ],
];
bash
    php artisan vendor:publish --provider="Turahe\Subscription\SubscriptionServiceProvider"
    
bash
    php artisan migrate