PHP code example of otp-id / otp-id-php

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

    

otp-id / otp-id-php example snippets




tpId\Channel;
use OtpId\Client;

$client = new Client(getenv('OTPID_API_KEY'));

// 1. Send an OTP (code generated by OTP.ID, never returned to you).
$res = $client->requestOtp([
    'channel' => Channel::WHATSAPP,
    'destination' => '6281234567890',
    'brand' => 'MyApp',           // shown in the OTP message; defaults to your merchant brand_name
    'external_id' => 'order-8821', // optional idempotency key
]);
echo "otp_id: {$res->otpId} balance: {$res->lastBalance}\n";

// 2. Later, verify what the user typed.
$v = $client->verifyOtp($res->otpId, '482913');
if ($v->verified) {
    echo "verified!\n";
} else {
    echo "wrong code: {$v->reason}\n"; // "mismatch" — not an error
}

use OtpId\ErrorCode;
use OtpId\Exception\ApiException;

try {
    $res = $client->requestOtp($params);
} catch (ApiException $e) {
    switch ($e->getErrorCode()) {
        case ErrorCode::INSUFFICIENT_BALANCE:
            // top up first
            break;
        case ErrorCode::DUPLICATE_EXTERNAL_ID:
            $existing = $e->getDetails()['existing_otp_id'] ?? null; // recover the original transaction
            break;
        case ErrorCode::RATE_LIMITED:
            // slow down (20 requests per second per API key)
            break;
    }
} catch (\OtpId\Exception\OtpIdException $e) {
    // ApiException (see above) or ConnectionException — a network-layer
    // failure (DNS, connection refused, TLS, timeout).
}

$res = $client->sendOtp('482913', [
    'channel' => Channel::SMS,
    'destination' => '6281234567890',
]);

$res = $client->requestOtp(['channel' => Channel::WHATSAPP_INBOUND]);
echo "Ask the user to tap: {$res->verification->waLink}\n";

$acc = $client->account();
echo "balance: {$acc->saldo}\n";

$topup = $client->createTopup(100000, 3); // 10000 | 100000 | 500000 | 1000000 | 2000000
echo "pay at: {$topup->paymentUrl}\n";



tpId\Exception\OtpIdException;
use OtpId\Webhook;

$body = file_get_contents('php://input');
$timestamp = $_SERVER['HTTP_X_OTPID_TIMESTAMP'] ?? '';
$signature = $_SERVER['HTTP_X_OTPID_SIGNATURE'] ?? '';

try {
    $event = Webhook::parseVerifiedEvent(
        getenv('OTPID_WEBHOOK_SECRET'),
        $timestamp,
        $signature,
        $body,
    );
} catch (OtpIdException $e) {
    http_response_code(401);
    exit;
}

echo "verified: {$event->otpId} external_id: {$event->externalId}\n";
http_response_code(200);

use OtpId\Client;
use OtpId\Transport\TransportInterface;

$client = new Client($apiKey, [
    'base_url' => 'https://api.otp.id', // default
    'timeout' => 10.0,                  // seconds; default 30.0
    'transport' => $customTransport,    // implements TransportInterface; default is CurlTransport
]);