PHP code example of davit-vardanyan / ameriabank-vpos-php

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

    

davit-vardanyan / ameriabank-vpos-php example snippets


use GuzzleHttp\Client;

$httpClient = new Client([
    'connect_timeout' => 3.0,  // seconds allowed to establish the connection
    'timeout' => 10.0,         // seconds allowed for the whole request
]);

use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Psr18Client;

$httpClient = new Psr18Client(HttpClient::create([
    'timeout' => 10.0,       // seconds of inactivity before the connection drops
    'max_duration' => 15.0,  // seconds allowed for the whole request
]));

use DavitVardanyan\AmeriabankVpos\Enum\Currency;
use DavitVardanyan\AmeriabankVpos\Money\Amount;
use DavitVardanyan\AmeriabankVpos\Request\InitPaymentRequest;

$request = new InitPaymentRequest(
    amount: Amount::fromDecimalString('10.00', Currency::AMD),
    orderId: 1001,
    backUrl: 'https://merchant.example/vpos/return',
    timeout: 900,  // the payment page stays valid for 15 minutes
);

use DavitVardanyan\AmeriabankVpos\Config\Credentials;
use DavitVardanyan\AmeriabankVpos\Config\Environment;
use DavitVardanyan\AmeriabankVpos\Vpos;

$vpos = new Vpos(
    credentials: new Credentials(
        clientId: (string) getenv('AMERIA_CLIENT_ID'),
        username: (string) getenv('AMERIA_USERNAME'),
        password: (string) getenv('AMERIA_PASSWORD'),
    ),
    environment: Environment::Test,
);

Vpos::PROTOCOL_VERSION; // '3.1' — the wire protocol this client targets

use DavitVardanyan\AmeriabankVpos\Enum\Currency;
use DavitVardanyan\AmeriabankVpos\Money\Amount;
use DavitVardanyan\AmeriabankVpos\Request\InitPaymentRequest;

$init = $vpos->payments()->init(new InitPaymentRequest(
    amount: Amount::fromMinorUnits(1000, Currency::AMD),  // 10.00 AMD
    orderId: 1001,                                        // your own order number
    backUrl: 'https://merchant.example/vpos/return',
    description: 'Order 1001',
    opaque: 'cart-9f3c',                                  // echoed back to you untouched
    timeout: 900,                                         // payment page valid for 15 minutes
));

$paymentId = $init->paymentId ?? '';

use DavitVardanyan\AmeriabankVpos\Enum\Language;

header('Location: ' . $vpos->paymentPageUrl($paymentId, Language::Armenian), true, 303);

use DavitVardanyan\AmeriabankVpos\Callback\VposCallback;
use DavitVardanyan\AmeriabankVpos\Exception\VposExceptionInterface;

try {
    $callback = VposCallback::fromQuery($_GET);   // unsigned, forgeable, identifiers only
    $details  = $vpos->verify($callback);         // the round trip that actually answers
} catch (VposExceptionInterface $e) {
    // An unusable callback, an OrderID that is not the callback's, or a gateway
    // that would not answer. The payment is unconfirmed: reconcile it later and
    // release nothing now. This path is not the exception — see below.
    return;
}

$details->orderId;             // ?string — the order the gateway says this payment is for
$details->orderStatusRaw;      // ?string, exactly as the gateway sent it
$details->orderStatus;         // ?OrderStatus — null when this SDK does not know the raw value
$details->paymentStateRaw;     // ?string — a second, separate status field, also populated
$details->paymentState;        // ?PaymentState — null when this SDK does not know the raw value
$details->approvedAmountRaw;   // ?string, the authorised total — the amount to compare against yours
$details->depositedAmountRaw;  // ?string, the remaining refundable balance, not the captured total
$details->refundedAmountRaw;   // ?string, refunded so far — accumulates across partial refunds
$details->approvedAmount;      // ?Amount — typed companion; null when no currency or a zero value
$details->trxnDescription;     // ?string — the Description *you* submitted
$details->description;         // ?string — the processor's own wording, not yours
$details->rrn;                 // ?string, the acquirer's retrieval reference number

$refund = $vpos->payments()->refund(
    $paymentId,
    Amount::fromMinorUnits(1000, Currency::AMD),
);

$refund->responseCode->asString();  // '00'

use DavitVardanyan\AmeriabankVpos\Exception\ApiException;
use DavitVardanyan\AmeriabankVpos\Exception\IndeterminateStateException;
use DavitVardanyan\AmeriabankVpos\Exception\VposExceptionInterface;

try {
    $refund = $vpos->payments()->refund($paymentId, $amount);
} catch (IndeterminateStateException $e) {
    // The exchange never completed, and the refund may or may not have happened.
    // Do not retry. Reconcile:
    $details = $vpos->payments()->details($e->paymentId() ?? $paymentId);
} catch (ApiException $e) {
    // The gateway answered, and the answer was no.
    $e->responseCode();     // int|string, keeping the type the wire sent
    $e->responseMessage();
} catch (VposExceptionInterface $e) {
    // Everything else this package throws: a field rejected before dispatch,
    // a transport failure, a misconfigured client, a gateway fault, a body
    // that could not be mapped.
}

use DateTimeImmutable;
use DavitVardanyan\AmeriabankVpos\Enum\PaymentType;

$bindings = $vpos->bindings()->all(PaymentType::BindingMainRest);
$vpos->bindings()->activate('card-holder-id', PaymentType::BindingMainRest);
$vpos->bindings()->deactivate('card-holder-id', PaymentType::BindingMainRest);
$vpos->bindings()->pay($makeBindingPaymentRequest);

$pending = $vpos->reports()->pending(
    new DateTimeImmutable('-1 day'),
    new DateTimeImmutable('now'),
);

$restored = unserialize($stored);

$restored->getPrevious();    // always null after a round trip
$restored->chainDropped();   // true, false, or null