PHP code example of schaefersoft / laravel-swiss-eid

1. Go to this page and download the library: Download schaefersoft/laravel-swiss-eid 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/ */

    

schaefersoft / laravel-swiss-eid example snippets


use SchaeferSoft\SwissEid\Facades\SwissEid;

$pending = SwissEid::verify()
    ->ageOver18()
    ->forUser(auth()->id())
    ->create();

// $pending->qrCode()    → SVG string, embed with {!! !!}
// $pending->deeplink    → universal link to open the wallet app directly
// $pending->statusUrl() → JSON polling endpoint for your frontend
// $pending->id          → UUID to look up the result later

use App\Models\User;
use SchaeferSoft\SwissEid\Events\VerificationCompleted;

Event::listen(VerificationCompleted::class, function ($event) {
    $result = $event->verification->toResult();

    if ($result->isSuccessful() && $result->isAdult()) {
        User::find($event->verification->user_id)?->update(['verified_at' => now()]);
    }
});

// config/swiss-eid.php
'credentials' => [
    'trust_anchors' => [
        [
            'did' => 'did:webvh:...anchor-did...',
            'trust_registry_uri' => 'https://trust-reg.trust-infra.swiyu-int.admin.ch',
        ],
    ],
],

// Or per request:
SwissEid::verify()
    ->trustAnchors([
        ['did' => 'did:webvh:...', 'trust_registry_uri' => 'https://trust-reg.trust-infra.swiyu-int.admin.ch'],
    ])
    ->ageOver18()
    ->create();

use SchaeferSoft\SwissEid\Facades\SwissEid;

$pending = SwissEid::verify()
    ->ageOver18()
    ->forUser($user->id)
    ->metadata(['order_id' => $order->id])
    ->create();

use SchaeferSoft\SwissEid\Enums\CredentialField;

$pending = SwissEid::verify()
    ->fields([
        CredentialField::GivenName,
        CredentialField::FamilyName,
        CredentialField::DateOfBirth,
        CredentialField::Nationality,
    ])
    ->create();

SwissEid::verify()->field('given_name');        // ['given_name']
SwissEid::verify()->field('$.custom_path');     // ['custom_path']
SwissEid::verify()->field('address.street');    // ['address', 'street']

SwissEid::verify()
    ->credentialType('your-credential-type')
    ->acceptedIssuers([
        'did:webvh:QmPEZ...your-trusted-issuer',
    ])
    ->ageOver18()
    ->create();

SwissEid::verify()
    ->ageOver18()
    ->purpose('com.example.age_check', 'Age verification', 'Required for checkout')
    ->create();

// Localized variants take arrays instead of strings:
SwissEid::verify()
    ->purpose('com.example.age_check',
        ['default' => 'Age verification', 'de-ch' => 'Altersverifikation'],
        ['default' => 'Required for checkout'])
    ->ageOver18()
    ->create();

// lang/vendor/swiss-eid/de/states.php
return [
    'pending' => 'Wartet auf Bestätigung',   // custom wording
    'success' => 'Erfolgreich',
    'failed'  => 'Fehlgeschlagen',
    'expired' => 'Abgelaufen',
];

use SchaeferSoft\SwissEid\Enums\VerificationState;

$state = VerificationState::Pending;
echo $state->label();  // "Ausstehend" (de), "Pending" (en), …

$result = SwissEid::getVerification($pending->id); // or the verifier ID

if ($result->isSuccessful()) {
    $firstName = $result->get('given_name');
    $isAdult   = $result->isAdult();          // age_over_18 convenience
    $raw       = $result->credentialData;     // decrypted array, or null
}

$result = SwissEid::refresh($pending->id); // or the verifier ID

use SchaeferSoft\SwissEid\Events\VerificationCompleted;
use SchaeferSoft\SwissEid\Events\VerificationFailed;
use SchaeferSoft\SwissEid\Events\VerificationExpired;

Event::listen(VerificationCompleted::class, function ($event) {
    $user = User::find($event->verification->user_id);
    $user->update(['verified_at' => now()]);
});

EidVerification::pending()->get();
EidVerification::expired()->get();
EidVerification::forUser($user->id)->latest()->first();

$schedule->command('swiss-eid:cleanup --days=30')->daily();

use SchaeferSoft\SwissEid\Facades\SwissEid;
use SchaeferSoft\SwissEid\SwissEidFake;

it('starts a verification', function () {
    $fake = SwissEid::fake();

    // ... code that calls SwissEid::verify()->ageOver18()->create()

    $fake->assertVerificationStarted();
});

it('reacts to a completed verification', function () {
    $result = SwissEidFake::fakeVerification(state: 'success', data: [
        'given_name'  => 'Anna',
        'age_over_18' => true,
    ]);

    $fake = SwissEid::fake([$result->id => $result]);

    SwissEid::getVerification($result->id);

    $fake->assertVerificationCompleted(fn ($r) => $r->get('given_name') === 'Anna');
});
blade
{{-- resources/views/verify.blade.php --}}
{!! $pending->qrCode(300) !!}
<a href="{{ $pending->deeplink }}">Open in Swiss Wallet App</a>
bash
php artisan swiss-eid:doctor
bash
php artisan swiss-eid:install
bash
php artisan vendor:publish --tag=swiss-eid-config
php artisan vendor:publish --tag=swiss-eid-migrations
php artisan migrate
blade
{{-- Plain Blade, no JS framework }
    <a href="{{ $pending->deeplink }}">Open in Swiss Wallet App</a>
    <small>Polling: {{ $pending->statusUrl() }}</small>
</div>
bash
php artisan vendor:publish --tag=swiss-eid-lang
bash
php artisan swiss-eid:doctor