PHP code example of php-montsers / sasscription
1. Go to this page and download the library: Download php-montsers/sasscription 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/ */
php-montsers / sasscription example snippets
namespace App\Models;
use PhpMonsters\Sasscription\Models\Concerns\HasSubscriptions;
class User
{
use HasSubscriptions;
}
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use PhpMonsters\Sasscription\Enums\PeriodicityType;
use PhpMonsters\Sasscription\Models\Feature;
class FeatureSeeder extends Seeder
{
public function run()
{
$deployMinutes = Feature::create([
'consumable' => true,
'name' => 'deploy-minutes',
'periodicity_type' => PeriodicityType::Day,
'periodicity' => 1,
]);
$customDomain = Feature::create([
'consumable' => false,
'name' => 'custom-domain',
]);
}
}
$cpuUsage = Feature::create([
'consumable' => true,
'postpaid' => true,
'name' => 'cpu-usage',
]);
class FeatureSeeder extends Seeder
{
public function run()
{
$storage = Feature::create([
'consumable' => true,
'quota' => true,
'name' => 'storage',
]);
}
}
...
class PhotoController extends Controller
{
public function store(Request $request)
{
$userFolder = auth()->id() . '-files';
$request->file->store($userFolder);
$usedSpace = collect(Storage::allFiles($userFolder))
->map(fn (string $subFile) => Storage::size($subFile))
->sum();
auth()->user()->setConsumedQuota('storage', $usedSpace);
return redirect()->route('files.index');
}
}
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use PhpMonsters\Sasscription\Enums\PeriodicityType;
use PhpMonsters\Sasscription\Models\Plan;
class PlanSeeder extends Seeder
{
public function run()
{
$bronze = Plan::create([
'name' => 'bronze',
'periodicity_type' => PeriodicityType::Month,
'periodicity' => 1,
]);
$silver = Plan::create([
'name' => 'silver',
'periodicity_type' => PeriodicityType::Month,
'periodicity' => 1,
]);
$gold = Plan::create([
'name' => 'gold',
'periodicity_type' => PeriodicityType::Month,
'periodicity' => 1,
]);
}
}
$free = Plan::create([
'name' => 'free',
'periodicity_type' => null,
'periodicity' => null,
]);
$gold = Plan::create([
'name' => 'gold',
'periodicity_type' => PeriodicityType::Month,
'periodicity' => 1,
'grace_days' => 7,
]);
use PhpMonsters\Sasscription\Models\Feature;
// ...
$deployMinutes = Feature::whereName('deploy-minutes')->first();
$subdomains = Feature::whereName('subdomains')->first();
$silver->features()->attach($deployMinutes, ['charges' => 15]);
$gold->features()->attach($deployMinutes, ['charges' => 25]);
$gold->features()->attach($subdomains);
namespace App\Listeners;
use App\Events\PaymentApproved;
class SubscribeUser
{
public function handle(PaymentApproved $event)
{
$subscriber = $event->user;
$plan = $event->plan;
$subscriber->subscribeTo($plan);
}
}
$subscriber->subscribeTo($plan, expiration: today()->addYear());
namespace App\Http\Controllers;
use App\Http\Requests\StudentStoreFormRequest;
use App\Models\Course;
use App\Models\User;
use PhpMonsters\Sasscription\Models\Plan;
class StudentController extends Controller
{
public function store(StudentStoreFormRequest $request, Course $course)
{
$student = User::make($request->validated());
$student->course()->associate($course);
$student->save();
$plan = Plan::find($request->input('plan_id'));
$student->subscribeTo($plan, startDate: $course->starts_at);
return redirect()->route('admin.students.index');
}
}
$student->switchTo($newPlan);
$student->balance('notes-download');
$primeMonthly = Plan::whereName('prime-monthly')->first();
$user->subscribeTo($primeMonthly);
...
$primeYearly = Plan::whereName('prime-yearly')->first();
$user->switchTo($primeYearly, immediately: false);
$subscriber->subscription->renew();
$subscriber->lastSubscription();
$subscriber->lastSubscription()->renew();
$subscriber->subscription->cancel();
$subscriber->subscription->suppress();
$subscriber->subscription->start(); // To start it immediately
$subscriber->subscription->start($startDate); // To determine when to start
$subscriber->consume('deploy-minutes', 4.5);
$subscriber->canConsume('deploy-minutes', 10);
$subscriber->cantConsume('deploy-minutes', 10);
$subscriber->hasFeature('deploy-minutes');
$subscriber->missingFeature('deploy-minutes');
$subscriber->giveTicketFor('deploy-minutes', today()->addMonth(), 10);
class UserFeatureTrialController extends Controller
{
public function store(FeatureTrialRequest $request, User $user)
{
$featureName = $request->input('feature_name');
$expiration = today()->addDays($request->input('trial_days'));
$user->giveTicketFor($featureName, $expiration);
return redirect()->route('admin.users.show', $user);
}
}
$subscriber->giveTicketFor('deploy-minutes', null, 10);
bash
composer
bash
php artisan vendor:publish --tag="sasscription-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="sasscription-config"