PHP code example of validakey / validakey-php

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

    

validakey / validakey-php example snippets


use Validakey\ValidakeyClient;
use Validakey\ValidakeyConfig;
use Validakey\Request\CreateTokenRequest;

$config = new ValidakeyConfig(
    baseUrl: getenv('VALIDAKEY_BASE_URL') ?: 'https://example.validakey.host/v1',
    apiUUID: getenv('VALIDAKEY_API_UUID') ?: 'your-account-uuid-here',
    userAppId: getenv('VALIDAKEY_USER_APP_ID') ?: 'your-user-app-id-here',
    apiPKey: getenv('VALIDAKEY_API_PKEY') ?: null,
    subject: getenv('VALIDAKEY_SUBJECT') ?: null,
);

$client = new ValidakeyClient($config);

$config = ValidakeyConfig::fromEnv(); // VALIDAKEY_BASE_URL, VALIDAKEY_API_UUID, VALIDAKEY_USER_APP_ID, VALIDAKEY_HOST, VALIDAKEY_API_PKEY, VALIDAKEY_TIMEOUT

use Validakey\Instance\FileInstanceStore;

$client = new ValidakeyClient(
    $config,
    null,
    new FileInstanceStore('/var/lib/myapp/validakey-instance.json'),
);

$client->rotateInstance();

$customerClient = $client->forSubject('customer-42');

$token = $client->createToken(new CreateTokenRequest(
    duration: 3600,
    basisCents: 999,
));

echo $token->token;

$check = $client->verifyToken($token->token);

if (! $check->isValid()) {
    exit($check->reason()); // revoked, expired, depleted, not_found
}

echo $check->expiresAt();     // null when the vKey never expires
echo $check->usesRemaining(); // null when it is not use-limited

$client->verifyToken($token->token, consume: true);

$client->renewToken($token->token, duration: 2592000);
$client->renewToken($token->token, uses: 500);
$client->deleteToken($token->token);

use Validakey\Request\AttachCardRequest;

$client->attachInstanceCard(new AttachCardRequest(
    sourceId: 'cnon:card-nonce-from-square-sdk',
    billingFields: ['postal_code' => '90210', 'country' => 'US'],
));

$link = $client->requestInstancePaymentLink();

echo 'Enter your card at: ' . $link->paymentUrl();

$status = $client->instancePaymentStatus(); // hasCard(), cardBrand(), cardLast4()
$client->detachInstanceCard();

$status = $client->getBillingStatus();
$config = $client->getBillingConfig();
$user   = $client->getUserInfo();

$client->attachAccountCard(new AttachCardRequest(sourceId: 'cnon:...'));
$client->detachAccountCard();

use Validakey\Exception\ApiException;
use Validakey\Exception\PaymentRequiredException;

try {
    $client->createToken(new CreateTokenRequest());
} catch (PaymentRequiredException $e) {
    // HTTP 402 — attach a billing card first
} catch (ApiException $e) {
    echo $e->getMessage();
    echo $e->errorCode;
    echo $e->httpStatus;
}
bash
composer