PHP code example of stadem / viva-payments

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

    

stadem / viva-payments example snippets


    'vivaDEMO' => [
        'VIVA_URL' => 'https://demo.vivapayments.com',
        'VIVA_ACCOUNT_URL' => 'https://demo-accounts.vivapayments.com',
        'VIVA_API_URL' => 'https://demo-api.vivapayments.com',
        'VIVA_API_KEY' => '',
        'VIVA_MERCHANT_ID' => '',
        'VIVA_SOURCE_CODE' => '',
        'VIVA_CLIENT_ID' => '',
        'VIVA_CLIENT_SECRET' => '',
        'VIVA_SMARTCHECKOUT_COLOR'=>'0d1bbd',
        'VIVA_DEBUG'=>0,
    ],

'vivaPROD' => [
    'VIVA_URL' => 'https://www.vivapayments.com',
    'VIVA_ACCOUNT_URL' => 'https://accounts.vivapayments.com',
    'VIVA_API_URL' => 'https://api.vivapayments.com',
    'VIVA_API_KEY' => '',
    'VIVA_MERCHANT_ID' => '',
    'VIVA_SOURCE_CODE' => '',
    'VIVA_CLIENT_ID' => '',
    'VIVA_CLIENT_SECRET' => '',
    'VIVA_DEBUG'=>0,
],

return [
    'curlDebug' => 0,
    'defaultProvider' => 'vivaDEMO',
    'vivaDEMO' => [
        'VIVA_URL' => 'https://demo.vivapayments.com',
        'VIVA_ACCOUNT_URL' => 'https://demo-accounts.vivapayments.com',
        'VIVA_API_URL' => 'https://demo-api.vivapayments.com',
        'VIVA_API_KEY' => 'a4bw%t%^',
        'VIVA_MERCHANT_ID' => '123456', 
        'VIVA_SOURCE_CODE' => '2222',
        'VIVA_CLIENT_ID' => '123456.vivapayments.com',
        'VIVA_CLIENT_SECRET' => '123456',
        'VIVA_BANK_TRANSFER_URL_API' => 'https://demo.vivapayments.com/api/wallets/{{platformAccountId}}/commands/banktransfer/{{vendorAccountId}}',
        'VIVA_WALLET_TO_WALLET_URL_API'=>'https://demo.vivapayments.com/api/wallets/{{platformAccountId}}/balancetransfer/{{vendorAccountId}}',
        'VIVA_SMARTCHECKOUT_COLOR'=>'0d1bbd',
        'VIVA_DEBUG'=>0,
    ],

    'vivaPROD' => [
        'VIVA_URL' => 'https://www.vivapayments.com',
        'VIVA_ACCOUNT_URL' => 'https://accounts.vivapayments.com',
        'VIVA_API_URL' => 'https://api.vivapayments.com',
        'VIVA_API_KEY' => '',
        'VIVA_MERCHANT_ID' => '',
        'VIVA_SOURCE_CODE' => '',
        'VIVA_CLIENT_ID' => '',
        'VIVA_CLIENT_SECRET' => '',
        'VIVA_DEBUG'=>0
    ]
];


use Stadem\VivaPayments\Enums;
use Stadem\VivaPayments\Services\AccessToken;
use Stadem\VivaPayments\Request\Customer;
use Stadem\VivaPayments\Request\CreatePaymentOrder;

$requestLang = $_REQUEST['requestLang'] ?? 'Greek';

 $config = new Config();
 $accessToken = new AccessToken($config);


$customer = new Customer(
    $email = '[email protected]',
    $fullName = 'Customer name',
    $phone = '+306941234567',
    $countryCode = 'GR',
    $requestLang = Enums\RequestLang::fromName($requestLang),
);


$CreatePaymentOrder  = [
    'amount'                => 120, 
    'customerTrns'          => 'Test POST - No End Payment', items/services being purchased.
    'paymentTimeout'        => 1800, 
    'preauth'               => false, 
    'allowRecurring'        => false, 
    'maxInstallments'       => 0, 
    'forceMaxInstallments'  => false, 
    'paymentNotification'   => true, 
    'tipAmount'             => 0, 
    'disableExactAmount'    => false, 
    'disableCash'           => true, 
    'disableWallet'         => false, 
    'cardTokens'            => null, Tokens tutorial.
    'merchantTrns'          => 'This is a short description that helps you uniquely identify the transaction', 
    'tags'                  => ['tag1', 'tag2'] 
];


$paymentMethods = [0, 8, 21, 29, 34, 35];


$paymentMethodFees  =  [
    'paymentMethodId' => '35',
    'fee' => 550
];


try {
   	$order = new CreatePaymentOrder($CreatePaymentOrder, $accessToken,$config);
    $order->setCustomer($customer);
    $order->setPaymentMethods($paymentMethods);
    $order->setPaymentMethodsFees($paymentMethodFees);

    $getOrderJson = $order->send();
    echo '<a href="' . $order->redirectUrl(PaymentMethods: Enums\PaymentMethods::fromName('VivaWallet')) . '" target="blank">';
    echo $order->getOrderCode();
    echo '</a>';
} catch (Exception  $e) {
    echo 'An error occured: <b>' . $e->getMessage() . '</b>';
}



use Stadem\VivaPayments\Enums;
use Stadem\VivaPayments\Services\AccessToken;
use Stadem\VivaPayments\Request\Customer;
use Stadem\VivaPayments\Request\CreatePaymentOrder;
use Stadem\VivaPayments\Config\Config;

$config = new Config();
$accessToken = new AccessToken(); // Set the value on config file -> defaultProvider
or
$config = new Config();
$accessToken = new AccessToken(environment: 'vivaDEMO'); // By direct set, mostly for testing purposes 

$Token = $accessToken->getAccessToken();
$statusCode = $accessToken->getStatusCode();
dump($Token.'-'.$statusCode);

$customer = new Customer(
    $email = '[email protected]',
    $fullName = 'Customer name',
    $phone = '+306941234567',
    $countryCode = 'GR',
    $requestLang = Enums\RequestLang::fromName('Greek'),
);

 RequestLang::Greek
 RequestLang::from('el-GR') // by value
 RequestLang::fromName('Greek') // by name
   

$CreatePaymentOrder  = [
    'amount'                => 120, 
    'customerTrns'          => 'Test POST - No End Payment',
    'paymentTimeout'        => 1800, 
    'preauth'               => false, 
    'allowRecurring'        => false, 
    'maxInstallments'       => 0, 
    'forceMaxInstallments'  => false, 
    'paymentNotification'   => true, 
    'tipAmount'             => 0, 
    'disableExactAmount'    => false, 
    'disableCash'           => true, 
    'disableWallet'         => false, 
    'cardTokens'            => null, Tokens tutorial.
    'merchantTrns'          => 'This is a short description that helps you uniquely identify the transaction', 
    'tags'                  => ['tag1', 'tag2'] 
];

	$order = new CreatePaymentOrder($CreatePaymentOrder, $accessToken,$config);




try {
    $order = new CreatePaymentOrder($CreatePaymentOrder, $accessToken,$config);
    $order->setCustomer($customer);
    $order->setPaymentMethods($paymentMethods);
    $order->setPaymentMethodsFees($paymentMethodFees);
    $order->send();
    $redirectUrl = $order->redirectUrl();
} catch (Exception  $e) {
    echo 'An error occured: <b>' . $e->getMessage() . '</b>';
}