PHP code example of laulamanapps / apple-passbook-laravel

1. Go to this page and download the library: Download laulamanapps/apple-passbook-laravel 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/ */

    

laulamanapps / apple-passbook-laravel example snippets


namespace App\Http\Controllers;

use LauLamanApps\ApplePassbook\Build\Compiler;
use LauLamanApps\ApplePassbook\GenericPassbook;
use LauLamanApps\ApplePassbook\MetaData\Barcode;
use LauLamanApps\ApplePassbook\Style\BarcodeFormat;

final class PassbookController
{
    public function __construct(
        private readonly Compiler $compiler,
    ) {
    }

    public function download()
    {
        $passbook = new GenericPassbook('8j23fm3');
        $passbook->setTeamIdentifier('<TeamId>');
        $passbook->setPassTypeIdentifier('<PassTypeId>');
        $passbook->setOrganizationName('Toy Town');
        $passbook->setDescription('Toy Town Membership');

        $barcode = new Barcode();
        $barcode->setFormat(BarcodeFormat::Pdf417);
        $barcode->setMessage('123456789');
        $passbook->setBarcode($barcode);

        return response($this->compiler->compile($passbook), 200, [
            'Content-Description' => 'File Transfer',
            'Content-Type' => 'application/vnd.apple.pkpass',
            'Content-Disposition' => 'filename="passbook.pkpass"',
        ]);
    }
}

$passbook->setWebService('https://example.com/', $authenticationToken);

namespace App\Listeners;

use App\Models\Pass;
use DateTimeImmutable;
use LauLamanApps\ApplePassbookLaravel\Events\DeviceRegisteredEvent;

final class RegisterDevice
{
    public function handle(DeviceRegisteredEvent $event): void
    {
        $pass = Pass::where('serial_number', $event->getSerialNumber())->first();

        if ($pass === null || !$event->isAuthenticatedBy($pass->getAuthToken())) {
            $event->notAuthorized();

            return;
        }

        $registration = $pass->registrations()->firstOrCreate(
            ['device_library_identifier' => $event->getDeviceLibraryIdentifier()],
            ['push_token' => $event->getPushToken()],
        );

        if (!$registration->wasRecentlyCreated) {
            $event->alreadyRegistered();

            return;
        }

        $event->deviceRegistered();
    }
}

namespace App\Listeners;

use App\Models\Pass;
use LauLamanApps\ApplePassbookLaravel\Events\DeviceUnregisteredEvent;

final class UnregisterDevice
{
    public function handle(DeviceUnregisteredEvent $event): void
    {
        $pass = Pass::where('serial_number', $event->getSerialNumber())->first();

        if ($pass === null || !$event->isAuthenticatedBy($pass->getAuthToken())) {
            $event->notAuthorized();

            return;
        }

        $pass->registrations()
            ->where('device_library_identifier', $event->getDeviceLibraryIdentifier())
            ->delete();

        $event->deviceUnregistered();
    }
}

namespace App\Listeners;

use App\Models\Pass;
use LauLamanApps\ApplePassbookLaravel\Events\DeviceRequestUpdatedPassesEvent;

final class ListUpdatedPasses
{
    public function handle(DeviceRequestUpdatedPassesEvent $event): void
    {
        $passes = Pass::registeredTo($event->getDeviceLibraryIdentifier())
            ->where('pass_type_identifier', $event->getPassTypeIdentifier())
            ->when($event->getPassesUpdatedSince(), fn ($query, $since) => $query->where('updated_at', '>', $since))
            ->get();

        if ($passes->isEmpty()) {
            $event->notFound();

            return;
        }

        $event->setSerialNumbers(
            $passes->pluck('serial_number')->all(),
            $passes->max('updated_at')->toDateTimeImmutable(),
        );
    }
}

namespace App\Listeners;

use App\Models\Pass;
use LauLamanApps\ApplePassbookLaravel\Events\RetrieveUpdatedPassbookEvent;

final class RetrieveUpdatedPassbook
{
    public function handle(RetrieveUpdatedPassbookEvent $event): void
    {
        $pass = Pass::where('serial_number', $event->getSerialNumber())->first();

        if ($pass === null) {
            $event->notFound();

            return;
        }

        if (!$event->isAuthenticatedBy($pass->getAuthToken())) {
            $event->notAuthorized();

            return;
        }

        $updatedAt = $pass->updated_at->toDateTimeImmutable();

        if ($event->getUpdatedSince() !== null && $updatedAt <= $event->getUpdatedSince()) {
            $event->notModified();

            return;
        }

        $event->setPassbook($pass->toPassbook(), $updatedAt);
    }
}
bash
php artisan vendor:publish --tag=apple-passbook-config