PHP code example of tomsommer / oauth2-onpay

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

    

tomsommer / oauth2-onpay example snippets


$provider = new TomSommer\OAuth2\Client\Provider\OnPay([
    'clientId'    => 'example.com',                     // your integration's identifier; the domain it runs on is conventional
    'redirectUri' => 'https://example.com/callback',
    'gatewayId'   => 'A5KM3QX7B',                       // optional, scopes authorization to one gateway
]);

if (!isset($_GET['code'])) {

    // Step 1. Send the merchant to OnPay to authorize.
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();

    header('Location: ' . $authUrl);
    exit;

} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    // Step 2. Reject a callback whose state does not match what we sent.
    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

    // Step 3. Exchange the authorization code for an access token.
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code'],
    ]);

    // Step 4. Use the token against the API.
    $request = $provider->getAuthenticatedRequest(
        'GET',
        'https://api.onpay.io/v1/ping',
        $token
    );
}

$newToken = $provider->getAccessToken('refresh_token', [
    'refresh_token' => $token->getRefreshToken(),
]);

if (null === $newToken->getRefreshToken()) {
    $newToken->setRefreshToken($token->getRefreshToken());
}

$provider = new TomSommer\OAuth2\Client\Provider\OnPay([
    'clientId'    => 'example.com',
    'redirectUri' => 'https://example.com/callback',
    'pkceMethod'  => League\OAuth2\Client\Provider\AbstractProvider::PKCE_METHOD_S256,
]);

// Before redirecting, store the verifier alongside the state.
$authUrl = $provider->getAuthorizationUrl();
$_SESSION['oauth2state'] = $provider->getState();
$_SESSION['oauth2pkce']  = $provider->getPkceCode();

// On the callback, restore it before exchanging the code.
$provider->setPkceCode($_SESSION['oauth2pkce']);
$token = $provider->getAccessToken('authorization_code', ['code' => $_GET['code']]);