PHP code example of salehye / subscription-core

1. Go to this page and download the library: Download salehye/subscription-core 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/ */

    

salehye / subscription-core example snippets




namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Salehye\Subscription\Traits\HasSubscriptions;

class User extends Authenticatable
{
    use HasSubscriptions;

    // Optional: Override for multi-tenancy
    public function getTenantId(): ?string
    {
        return $this->tenant_id;
    }
}

use Salehye\Subscription\Models\Plan;
use Salehye\Subscription\Models\Feature;

// Create a plan
$plan = Plan::create([
    'name' => 'Pro Monthly',
    'slug' => 'pro-monthly',
    'description' => 'Professional plan with all features.',
    'billing_cycle' => 'monthly', // monthly, yearly, lifetime
    'price' => 29.99,
    'trial_days' => 7,
    'is_active' => true,
    'sort_order' => 1,
]);

// Create features
$feature = Feature::create([
    'name' => 'Max Users',
    'slug' => 'max_users',
    'type' => 'limit', // toggle, consumable, limit
]);

// Attach feature to plan with a value
$plan->features()->attach($feature->id, ['value' => '10']);

$user = User::find(1);
$plan = Plan::where('slug', 'pro-monthly')->first();

// Via Facade
$subscription = \Subscription::subscribe($user, $plan);

// Via Trait
$subscription = $user->subscribeTo($plan);

// With custom trial
$subscription = $user->subscribeTo($plan, trialDays: 14, autoRenew: true);

// Cancel (at period end)
$user->cancelSubscription();

// Cancel immediately
$user->cancelSubscription(true);

// Switch plan
\Subscription::switchPlan($subscription, $newPlan);

// Pause / Resume
\Subscription::pause($subscription);
\Subscription::resume($subscription);

// Attach add-on
\Subscription::attachAddon($subscription, $addonPlan);

// Renew
\Subscription::renew($subscription);

// Check if user can access a feature
if ($user->canAccessFeature('api_access')) {
    // Grant API access
}

// Get remaining usage
$remaining = $user->remainingFeature('monthly_emails');

// Consume a feature
$user->consumeFeature('monthly_emails', 10);

use Salehye\Subscription\Services\FeatureGuard;

$guard = app(FeatureGuard::class);

// Check access
$guard->can($subscription, 'api_access');

// Get limit
$limit = $guard->limit($subscription, 'max_users');

// Get remaining
$remaining = $guard->remaining($subscription, 'monthly_emails');

// Get current usage
$usage = $guard->usage($subscription, 'monthly_emails');

// Consume
$guard->consume($subscription, 'monthly_emails', 5);

// In routes/web.php
Route::middleware('subscription.feature:api_access')->group(function () {
    Route::get('/api/dashboard', [ApiController::class, 'dashboard']);
});

// Redirect instead of abort
Route::middleware('subscription.feature:advanced_reports,redirect')->group(function () {
    Route::get('/reports', [ReportController::class, 'index']);
});

use Salehye\Subscription\Contracts\FeatureResolver;
use Salehye\Subscription\Models\Feature;
use Salehye\Subscription\Models\Subscription;

class CustomFeatureResolver implements FeatureResolver
{
    public function resolve(Subscription $subscription, Feature $feature): string
    {
        // Your custom resolution logic
    }

    // Implement other methods...
}

'feature_resolver' => App\Resolvers\CustomFeatureResolver::class,

'plan_repository' => App\Repositories\CustomPlanRepository::class,

'multi_tenancy' => [
    'enabled' => true,
    'tenant_column' => 'tenant_id',
    'tenant_resolver' => App\Resolvers\TenantResolver::class,
],
bash
# Publish config
php artisan vendor:publish --tag=subscription-config

# Publish migrations
php artisan vendor:publish --tag=subscription-migrations

# Run migrations
php artisan migrate
bash
php artisan db:seed --class="Salehye\Subscription\Database\Seeders\PlanSeeder"
php artisan db:seed --class="Salehye\Subscription\Database\Seeders\FeatureSeeder"