PHP code example of vipertecpro / mobile-entitlements

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

    

vipertecpro / mobile-entitlements example snippets


if ($request->user()->hasEntitlement('pro')) {
    // ...
}

use Vipertecpro\MobileEntitlements\Concerns\HasEntitlements;

class User extends Authenticatable
{
    use HasEntitlements;
}

'entitlements' => [
    'pro' => ['com.example.pro.monthly', 'com.example.pro.yearly', 'pro_monthly', 'pro_yearly'],
    'lifetime' => ['com.example.lifetime', 'lifetime_unlock'],
],

// Google Play does not say whether a one-time product is a consumable, so list them here.
'consumables' => ['coins_100'],

$user->hasEntitlement('pro');          // bool
$user->entitlement('pro');             // ?Entitlement, the active one with the latest expiry
$user->entitlements;                   // every row
$user->activeEntitlements()->get();    // rows that grant access right now

Route::middleware(['auth:sanctum', 'entitled:pro'])->group(function () {
    // 402 JSON when the user lacks the entitlement
});

Route::middleware('entitled:pro,lifetime')->get(...); // any of several keys

use Vipertecpro\MobileEntitlements\Enums\Store;
use Vipertecpro\MobileEntitlements\Facades\MobileEntitlements;

MobileEntitlements::verify(Store::AppStore, $jws);            // VerifiedPurchase, nothing stored
MobileEntitlements::grantFromToken($user, Store::GooglePlay, $token);
MobileEntitlements::reconcile($user);

// routes/console.php
Schedule::command('mobile-entitlements:reconcile')->hourly();

'prices' => [
    'com.example.pro.monthly' => 9.99,
    'com.example.pro.yearly' => 99.99 / 12,
    'pro_monthly' => 9.99,
],

// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Gate;

public function boot(): void
{
    Gate::define('viewMobileEntitlementsSummary', fn ($user): bool => $user->is_admin);
}

Gate::define('redeemMobileEntitlementsPromoOffer', function ($user, string $productId, string $offerId): bool {
    return $offerId !== 'winback_50' || $user->entitlements()->where('product_id', $productId)->exists();
});

use Vipertecpro\MobileEntitlements\Enums\Store;
use Vipertecpro\MobileEntitlements\Facades\MobileEntitlements;
use Vipertecpro\MobileEntitlements\Support\VerifiedPurchase;

$fake = MobileEntitlements::fake([
    'test-token' => VerifiedPurchase::fake([
        'store' => Store::GooglePlay,
        'productId' => 'pro_monthly',
    ]),
]);

$this->actingAs($user)
    ->postJson('/mobile-entitlements/sync', ['store' => 'googlePlay', 'token' => 'test-token'])
    ->assertOk()
    ->assertJson(['granted' => ['pro']]);

$fake->assertVerified('test-token');
sh
composer n vendor:publish --tag=mobile-entitlements-config
php artisan vendor:publish --tag=mobile-entitlements-migrations
php artisan migrate
sh
php artisan mobile-entitlements:reconcile --stale=24h
php artisan mobile-entitlements:reconcile --user=42
sh
php artisan mobile-entitlements:report            # last 30 days, as a table
php artisan mobile-entitlements:report --days=7 --json