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']);
// 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);
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...
}