1. Go to this page and download the library: Download coretrekas/vipps 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/ */
coretrekas / vipps example snippets
use Coretrek\Vipps\VippsClient;
$client = new VippsClient(
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
subscriptionKey: 'your-subscription-key',
merchantSerialNumber: 'your-msn',
testMode: true // Set to false for production
);
$client = new VippsClient(
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
subscriptionKey: 'your-subscription-key',
merchantSerialNumber: 'your-msn',
testMode: true,
options: [
'systemName' => 'MyEcommercePlatform',
'systemVersion' => '2.1.0',
'pluginName' => 'VippsPaymentPlugin',
'pluginVersion' => '1.5.3',
]
);
// Or set it later
$client->setSystemInfo(
systemName: 'MyEcommercePlatform',
systemVersion: '2.1.0',
pluginName: 'VippsPaymentPlugin',
pluginVersion: '1.5.3'
);
// Simple payment creation
$payment = $client->epayment()->createSimplePayment(
reference: 'order-12345',
amount: 10000, // Amount in minor units (100.00 NOK)
currency: 'NOK',
userFlow: 'WEB_REDIRECT',
options: [
'returnUrl' => 'https://example.com/order/12345/complete',
'paymentDescription' => 'Order #12345',
]
);
// Redirect user to payment page
header('Location: ' . $payment['redirectUrl']);
// Price campaign - reduced price until a date
$agreement = $client->recurring()
->buildAgreement()
->legacyPricing(3900, 'NOK')
->interval('MONTH', 1)
->product('News Subscription')
->merchantUrls('https://example.com/redirect', 'https://example.com/manage')
->phoneNumber('4712345678')
->priceCampaign(100, '2024-12-31T23:59:59Z') // 1 NOK until end of year
->create();
// Period campaign - fixed price for a period
$agreement = $client->recurring()
->buildAgreement()
->legacyPricing(3900, 'NOK')
->interval('MONTH', 1)
->product('News Subscription')
->merchantUrls('https://example.com/redirect', 'https://example.com/manage')
->phoneNumber('4712345678')
->periodCampaign(100, 'WEEK', 4) // 1 NOK for 4 weeks
->initialCharge(100, 'NOK', 'Campaign activation', 'DIRECT_CAPTURE')
->create();
$agreement = $client->recurring()
->buildAgreement()
->variablePricing(5000, 'NOK') // User can be charged up to 50 NOK
->interval('MONTH', 1)
->product('Usage-based Service')
->merchantUrls('https://example.com/redirect', 'https://example.com/manage')
->phoneNumber('4712345678')
->create();
use Coretrek\Vipps\Login\AuthorizationUrlBuilder;
// Generate secure random values
$state = AuthorizationUrlBuilder::generateState();
$nonce = AuthorizationUrlBuilder::generateNonce();
$codeVerifier = AuthorizationUrlBuilder::generateCodeVerifier();
// Build authorization URL
$authUrl = $client->login()
->buildAuthorizationUrl()
->clientId('your-client-id')
->redirectUri('https://example.com/vipps/callback')
->scope(['openid', 'name', 'email', 'phoneNumber', 'address'])
->state($state)
->nonce($nonce)
->pkce($codeVerifier, 'S256')
->build();
// Store state, nonce, and code_verifier in session
$_SESSION['oauth_state'] = $state;
$_SESSION['oauth_nonce'] = $nonce;
$_SESSION['oauth_code_verifier'] = $codeVerifier;
// Redirect user to Vipps login
header('Location: ' . $authUrl);
use Coretrek\Vipps\Login\AuthorizationUrlBuilder;
// Generate secure random values
$state = AuthorizationUrlBuilder::generateState();
$nonce = AuthorizationUrlBuilder::generateNonce();
$codeVerifier = AuthorizationUrlBuilder::generateCodeVerifier();
// Build authorization URL with app-to-app flow
$authUrl = $client->login()
->buildAuthorizationUrl()
->clientId('your-client-id')
->redirectUri('https://example.com/vipps/callback')
->scope(['openid', 'name', 'email', 'phoneNumber'])
->state($state)
->nonce($nonce)
->pkce($codeVerifier, 'S256')
->requestedFlow('app_to_app')
->appCallbackUri('myapp://oauth/callback/vipps')
->build();
// Store state, nonce, and code_verifier securely
// Then open the auth URL in the device browser or use a deep link
// In your callback handler
$code = $_GET['code'];
$returnedState = $_GET['state'];
// Verify state to prevent CSRF
if ($returnedState !== $_SESSION['oauth_state']) {
throw new Exception('Invalid state');
}
// Exchange code for tokens
$tokens = $client->login()->exchangeCodeForTokens(
code: $code,
redirectUri: 'https://example.com/vipps/callback',
options: [
'code_verifier' => $_SESSION['oauth_code_verifier'],
]
);
// Get user information
$userInfo = $client->login()->getUserInfo($tokens['access_token']);
echo "Welcome, " . $userInfo['name'];
echo "Email: " . $userInfo['email'];
echo "Phone: " . $userInfo['phone_number'];
// Check if user exists
$userExists = $client->login()->checkUserExists('4712345678');
if ($userExists['exists']) {
// Initiate authentication
$auth = $client->login()->initiateCibaAuth(
loginHint: '4712345678',
options: [
'scope' => 'openid name email',
'bindingMessage' => 'Login to Example App',
'requested_expiry' => 300,
]
);
// Poll for token (with proper interval)
$interval = $auth['interval'];
$authReqId = $auth['auth_req_id'];
while (true) {
sleep($interval);
try {
$tokens = $client->login()->pollCibaToken($authReqId);
// User has authenticated
break;
} catch (VippsException $e) {
// Still waiting for user to approve
continue;
}
}
$userInfo = $client->login()->getUserInfo($tokens['access_token']);
}