PHP code example of glueful / subscriptions

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

    

glueful / subscriptions example snippets


use Glueful\Entitlements\Contracts\EntitlementCheckerInterface;

$checker = app($context, EntitlementCheckerInterface::class);

if ($checker->allows($tenantUuid, 'reports.export')) {
    // gated feature
}

$limit = $checker->limit($tenantUuid, 'projects.limit'); // ?int -- null = unlimited

$router->get('/reports/export', [ReportController::class, 'export'])
    ->middleware(['

return [
    'default_plan' => 'free',
    'plans' => [
        'free' => [
            'entitlements' => [
                'reports.export' => false,
                'projects.limit' => 3,
                'team.limit'     => 1,
            ],
        ],
        'pro' => [
            'provider_price_id' => null, // optional provider price/plan id
            'entitlements' => [
                'reports.export' => true,
                'projects.limit' => 50,
                'team.limit'     => 20,
                'api.monthly'    => 100000,
            ],
        ],
    ],
    'rate_tiers' => ['enterprise', 'pro'], // highest-first (rate-limit bridge)
    'grace_days' => 3,                     // dunning grace before downgrade
    'cache' => ['enabled' => true, 'ttl' => 300],
    'permissive_middleware' => false,
    'reconcile' => ['schedule_enabled' => false],
];

use Glueful\Extensions\Subscriptions\Repositories\OverrideRepository;
use Glueful\Extensions\Subscriptions\Subject;

$overrides = new OverrideRepository();

// A workspace-level override (the 1.x meaning: the tenant's own subject).
$overrides->upsertForSubject($context, Subject::tenant($tenantUuid), 'projects.limit', 500);

// A member-level override, scoped to one user inside one workspace.
$overrides->upsertForSubject(
    $context,
    Subject::user($tenantUuid, $userUuid),
    'content.premium',
    false,                              // a DENY override
    expiresAt: '2026-12-31 23:59:59',   // optional
    reason: 'chargeback hold',          // optional
);

$overrides->deleteForSubject($context, Subject::tenant($tenantUuid), 'projects.limit');

$overrides->listForSubject($context, Subject::user($tenantUuid, $userUuid));

use Glueful\Extensions\Subscriptions\SubscriptionService;

$service = app($context, SubscriptionService::class);

// Free/comp/trial -- no provider object needed, all provider_* columns stay NULL.
$service->start($tenantUuid, 'free');
$service->start($tenantUuid, 'pro', [
    'status' => 'trialing',
    'trial_ends_at' => '2026-07-01 00:00:00',
]);

$service->current($tenantUuid);          // ?array (the subscriptions row)
$service->changePlan($tenantUuid, 'pro');
$service->cancel($tenantUuid);                       // at period end (metadata flag)
$service->cancel($tenantUuid, atPeriodEnd: false);   // immediate: status=canceled
$service->reconcile($tenantUuid);        // pull provider truth, when a provider puller is bound

use Glueful\Extensions\Subscriptions\CheckoutReservationException;
use Glueful\Extensions\Subscriptions\Subject;

$reservation = $service->reserveCheckoutFor(
    Subject::tenant($tenantUuid),
    $planUuid,
    $originationUuid,          // Payvia's opaque checkout correlation id
    ['actor' => $currentUserUuid],   // optional; audit-stamped only
);

$released = $service->releaseCheckoutReservation(Subject::tenant($tenantUuid), $originationUuid);

use Glueful\Extensions\Subscriptions\Plans\PlanPurchasability;

$purchasable = PlanPurchasability::forGateway($context, 'stripe');
// list<array{plan_uuid: string, plan_key: string, name: string, provider_identifier: string}>

$rows = $service->currentForTenants(['tenant-a', 'tenant-b']);
// ['tenant-a' => [...subscription row...], 'tenant-b' => [...]]
// an absent key means that tenant has no subscription -- never a null value

// In your app's ServiceProvider.
use Glueful\Extensions\Subscriptions\Contracts\SubjectResolverInterface;

public static function services(): array
{
    return [
        SubjectResolverInterface::class => [
            'class' => YourAppSubjectResolver::class,
            'shared' => true,
            'autowire' => true,
        ],
    ];
}

use Glueful\Extensions\Subscriptions\Catalog\PlanCatalog;
use Glueful\Extensions\Subscriptions\Resolution\MemberEntitlementResolver;

$catalog = PlanCatalog::forScope($context, 'user', $tenantUuid); // THIS workspace only
$resolver = new MemberEntitlementResolver(
    $catalog,
    $subscriptionRepository,
    $overrideRepository,
    $effectivePlanResolver,
);

$map = $resolver->resolveMap($context, $tenantUuid, $userUuid);

$router->get('/articles/{id}', [ArticleController::class, 'show'])
    ->middleware(['

use Glueful\Extensions\Subscriptions\Schema\SubscriptionSchemaReadiness;

$ready = app($context, SubscriptionSchemaReadiness::class)->isReady();
bash
composer extensions:enable subscriptions
php glueful migrate:run
php glueful subscriptions:plans:import-config
text
GET    /subscriptions/plans
POST   /subscriptions/plans
POST   /subscriptions/plans/import-config
GET    /subscriptions/plans/{key}
PATCH  /subscriptions/plans/{key}
POST   /subscriptions/plans/{key}/archive
bash
php glueful subscriptions:show --tenant=<uuid>
php glueful subscriptions:set-plan --tenant=<uuid> --plan=pro
php glueful subscriptions:reconcile [--tenant=<uuid>]
php glueful subscriptions:plans:create --key=pro --name="Pro" --entitlements='{"reports.export":true}'
php glueful subscriptions:plans:update --key=pro --status=archived
php glueful subscriptions:plans:archive --key=pro
php glueful subscriptions:plans:import-config [--force]
php glueful subscriptions:plans:list
bash
   php glueful subscriptions:prepare-v2