PHP code example of nonz250 / smaregi-api-php

1. Go to this page and download the library: Download nonz250/smaregi-api-php 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/ */

    

nonz250 / smaregi-api-php example snippets



declare(strict_types=1);

use Nonz250\SmaregiApiPhp\Auth\SmaregiClientCredentials;
use Nonz250\SmaregiApiPhp\Auth\SmaregiProvider;

$provider = new SmaregiProvider(
    'SMAREGI_IDP_HOST',
    'SMAREGI_CLIENT_ID',
    'SMAREGI_CLIENT_SECRET',
);

$accessToken = $provider->getAccessToken(new SmaregiClientCredentials(), [
    'contract_id' => 'SMAREGI_CONTRACT_ID',
    'scope' => ['pos.products:read', 'pos.customers:read'],
]);


declare(strict_types=1);

use Nonz250\SmaregiApiPhp\Auth\SmaregiProvider;

session_start();

$provider = new SmaregiProvider(
    'SMAREGI_IDP_HOST',
    'SMAREGI_CLIENT_ID',
    'SMAREGI_CLIENT_SECRET',
    'http://localhost/callback.php',
);

$authorizationUrl = $provider->getAuthorizationUrl([
    'scope' => ['openid', 'email', 'profile', 'offline_access', 'pos.products:read', 'pos.customers:read'],
]);
$_SESSION['sampleState'] = $provider->getState();
$_SESSION['samplePkceCode'] = $provider->getPkceCode();
header('Location: ' . $authorizationUrl);
exit();


declare(strict_types=1);

use Nonz250\SmaregiApiPhp\Auth\SmaregiProvider;

session_start();

$sessionState = (string)($_SESSION['sampleState'] ?? '');

if ($sessionState === '') {
    throw new RuntimeException('Session state is empty.');
}
$sampleState = $_GET['state'] ?? '';

if ($sessionState !== $sampleState) {
    throw new RuntimeException('Invalid state.');
}

$provider = new SmaregiProvider(
    'SMAREGI_IDP_HOST',
    'SMAREGI_CLIENT_ID',
    'SMAREGI_CLIENT_SECRET',
    'http://localhost/callback.php',
);

$code = $_GET['code'] ?? '';

$provider->setPkceCode($_SESSION['samplePkceCode'] ?? '');
$accessToken = $provider->getAccessToken('authorization_code', [
    'code' => $code,
]);

$resourceOwner = $provider->getResourceOwner($accessToken);

$accessToken = $provider->getAccessToken('refresh_token', [
    'refresh_token' => $accessToken->getRefreshToken(),
]);
shell
composer