PHP code example of impruthvi / cashier-entitlements

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

    

impruthvi / cashier-entitlements example snippets


use Illuminate\Database\Eloquent\Relations\Relation;
use Impruthvi\CashierEntitlements\Billing\{PriceCatalog, PriceMapping};
use Laravel\Cashier\Cashier;

// boot(): merge with your application's existing morph map.
Cashier::useCustomerModel(Organization::class);
Relation::morphMap(['organization' => Organization::class]);

// register(): must match the configured Stripe account and mode.
$this->app->singleton(PriceCatalog::class, fn () => new PriceCatalog(
    version: 'v1',
    prices: [
        'price_base' => new PriceMapping('base', ['projects' => 2,  'ai' => false]),
        'price_pro'  => new PriceMapping('pro',  ['projects' => 10, 'ai' => true]),
    ],
    providerContext: 'platform',
    liveMode: false,
));

'enabled' => true,
'live_mode' => false,
'freshness' => ['max_stale_age' => 3600],
// Alternative: 'freshness' => ['retain_last_known' => true],

use Impruthvi\CashierEntitlements\Reconciliation\OwnerLocator;
use Impruthvi\CashierEntitlements\Resolution\LocalResolver;

$owner  = app(OwnerLocator::class)->reference($organization);
$access = app(LocalResolver::class)->for($owner);

$access->can('ai');            // bool — boolean features only
$access->limit('projects');    // int; null is unlimited, zero is none
$access->usage('projects');    // int consumed in the current period
$access->remaining('projects'); // int, or null for unlimited
$access->all();                // the whole allowance map in one query

use Impruthvi\CashierEntitlements\Usage\NativeUsage;

$receipt = app(NativeUsage::class)->admit(
    $owner, 'projects', 1, $requestId, $resolver,
    function (Connection $db, UsageReceipt $receipt) {
        $db->table('projects')->insert(['id' => $receipt->id, /* ... */]);
    },
);

'meters' => [
    'projects'  => 'calendar_month',
    'exports'   => 'calendar_day',
    'seats'     => 'billing:price_pro',  // exact Stripe period boundaries
],

app(RefreshManager::class)->request($organization);

Schedule::command('entitlements:recover --limit=100')->everyMinute()->withoutOverlapping();

app(NativeOverrides::class)->grant($owner, 'projects', 25,
    'Enterprise trial extension, ticket 4821', 'admin:'.$actor->id, $from, $until);
sh
php artisan vendor:publish --tag=cashier-entitlements-config
php artisan vendor:publish --tag=cashier-entitlements-migrations
php artisan migrate
sh
php artisan entitlements:doctor --json
php artisan entitlements:reconcile --owner-type=organization --owner=123 --dry-run --json