PHP code example of jojostx / larasubs

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

    

jojostx / larasubs example snippets



namespace App\Models;

use Jojostx\Larasubs\Models\Concerns\HasSubscriptions;

class User
{
    use HasSubscriptions;
}


namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Jojostx\Larasubs\Enums\IntervalType;
use Jojostx\Larasubs\Models\Models\Plan;

class PlanSeeder extends Seeder
{
    public function run()
    {
        $silver = Plan::create([
            'name'                    => 'silver',
            'description'             => 'Plan for medium businesses',
            'active'                  => true,
            'price'                   => 100000, // price in the lowest currency value (kobo)
            'currency'                => 'NGN',
            'interval'                => 6,
            'interval_type'           => IntervalType::YEAR,
            'trial_interval'          => 1,
            'trial_interval_type'     => IntervalType::MONTH,
            'grace_interval'          => 1,
            'grace_interval_type'     => IntervalType::MONTH,
            'sort_order' => 1,
        ]);

        $gold = Plan::create([
            'name'                    => 'gold',
            'description'             => 'Plan for large businesses',
            'active'                  => true,
            'price'                   => 10000000, // price in the lowest currency value (kobo)
            'currency'                => 'NGN',
            'interval'                => 6,
            'interval_type'           => IntervalType::YEAR,
            'trial_interval'          => 1,
            'trial_interval_type'     => IntervalType::MONTH,
            'grace_interval'          => 1,
            'grace_interval_type'     => IntervalType::MONTH
        ]);
    }
}



namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Jojostx\Larasubs\Enums\IntervalType;
use Jojostx\Larasubs\Models\Models\Feature;

class FeatureSeeder extends Seeder
{
    public function run()
    {
        $deployMinutes = Feature::create([
            'name'             => 'deploy-minutes',
            'consumable'       => true,
            'interval'      => 1,
            'interval_type' => IntervalType::DAY,
        ]);

        $customDomain = Feature::create([
            'name'       => 'custom-domain',
            'description' => 'Ability to create and use subdomains',
            'consumable' => false,
            'active' => true,
            'interval'      => 1,
            'interval_type' => IntervalType::DAY,
        ]);
    }
}

use Jojostx\Larasubs\Models\Feature;

// ...

$deployMinutes = Feature::where('name', 'deploy-minutes')->first();
$subdomains    = Feature::where('name', 'subdomains')->first();

$silver->features()->attach($deployMinutes, ['units' => 15]);

$gold->features()->attach($deployMinutes, ['units' => 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;
        $subscriptionName  = $user->name . strval(time()); // should be unique

        $subscriber->subscribeTo(
            $plan,
            $subscriptionName
        );
    }
}
bash
php artisan vendor:publish --provider="Jojostx\Larasubs\LarasubsServiceProvider" --tag=larasubs-config

php artisan vendor:publish --provider="Jojostx\Larasubs\LarasubsServiceProvider" --tag=larasubs-migrations