PHP code example of nesthus / vipps-laravel

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

    

nesthus / vipps-laravel example snippets


use Nesthus\Vipps\Amount;
use Nesthus\Vipps\Laravel\Facades\Vipps;
use Nesthus\Vipps\Recurring\Interval;
use Nesthus\Vipps\Recurring\NewAgreement;
use Nesthus\Vipps\Recurring\Pricing;

$created = Vipps::recurring()->createAgreement(new NewAgreement(
    pricing: Pricing::legacy(Amount::fromMajor(49)),   // 49.00 NOK per charge
    interval: Interval::months(1),
    productName: 'Premium',
    merchantRedirectUrl: route('subscription.return'),
    merchantAgreementUrl: route('subscription.show'),  // where the user can manage/cancel
), $idempotencyKey);

// Persist $created->agreementId next to your key, THEN:
return redirect()->away($created->vippsConfirmationUrl);

use Nesthus\Vipps\Amount;
use Nesthus\Vipps\Epayment\CreatePayment;
use Nesthus\Vipps\Laravel\Facades\Vipps;

$created = Vipps::epayment()->createPayment(new CreatePayment(
    amount: Amount::fromMajor(249, 50),    // 249.50 NOK — integer minor units, no floats
    reference: 'order-2026-000123',        // your permanent id: 8–64 chars of [a-zA-Z0-9-]
    returnUrl: route('checkout.return'),
), $idempotencyKey);

return redirect()->away($created->redirectUrl);   // null only for flows without a browser hop

// routes/api.php
Route::vippsWebhooks('/vipps/webhooks');

// bootstrap/app.php — or any service provider's boot()
Route::middleware('api')->group(function () {
    Route::vippsWebhooks('/vipps/webhooks');
});

// app/Listeners/RecordChargeCollected.php — auto-discovered by Laravel
namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Nesthus\Vipps\Laravel\Events\ChargeCaptured;

final class RecordChargeCollected implements ShouldQueue
{
    public function handle(ChargeCaptured $event): void
    {
        $eventId = $event->payload['eventId'] ?? null;

        // Dedupe on the event id BEFORE acting — retries of the same
        // delivery fire this listener again.
        if ($eventId === null || $this->alreadyProcessed($eventId)) {
            return;
        }

        // Persist the delivery, then act on $event->payload['agreementId'],
        // $event->payload['chargeId'], …
    }
}

// routes/web.php — the driver keeps OAuth state and the PKCE verifier in the
// session, so these routes need the web middleware group.
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

Route::get('/auth/vipps/redirect', fn () => Socialite::driver('vipps')->redirect());

Route::get('/auth/vipps/callback', function () {
    $vippsUser = Socialite::driver('vipps')->user();

    $user = User::updateOrCreate(
        ['vipps_sub' => $vippsUser->getId()],   // OIDC `sub` — the stable identity; email can change
        [
            'name' => $vippsUser->getName(),
            'email' => $vippsUser->getEmail(),
        ],
    );

    Auth::login($user, remember: true);

    return redirect()->intended('/');
});

use Illuminate\Support\Facades\Event;
use Nesthus\Vipps\Laravel\Events\ChargeCaptured;

// Unit-test a listener by handing it the event:
(new RecordChargeCollected())->handle(new ChargeCaptured([
    'eventType' => 'recurring.charge-captured.v1',
    'eventId' => 'evt-42',
    'agreementId' => 'agr-123',
]));

// Or fake the dispatcher and assert your code fires/handles what it should:
Event::fake([ChargeCaptured::class]);
// … exercise your code …
Event::assertDispatched(ChargeCaptured::class);

use GuzzleHttp\Psr7\HttpFactory;
use Nesthus\Vipps\Vipps;
use Nesthus\Vipps\VippsConfig;

$fakeHttp = new FakeHttpClient();
$factory = new HttpFactory();

$this->app->instance(Vipps::class, new Vipps(
    new VippsConfig('client-id', 'client-secret', 'subscription-key', '123456'),
    $fakeHttp,
    $factory,
    $factory,
));

$fakeHttp->queueJson(201, ['agreementId' => 'agr-1', 'vippsConfirmationUrl' => 'https://…']);

// … exercise your code, then assert on $fakeHttp->lastRequest() —
// a full PSR-7 request: method, URI, Idempotency-Key header, body.
bash
php artisan vendor:publish --tag=vipps-config
bash
php artisan vipps:webhooks register