PHPackages                             vgspedro/vivaapi - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. vgspedro/vivaapi

ActiveLibrary[API Development](/categories/api)

vgspedro/vivaapi
================

Viva Wallet Native Checkout V2 API

1(5y ago)025MITPHPPHP &gt;=5.6.0CI failing

Since Jul 21Pushed 5y ago1 watchersCompare

[ Source](https://github.com/vgspedro/vivaapi)[ Packagist](https://packagist.org/packages/vgspedro/vivaapi)[ Docs](https://github.com/vgspedro/vivaapi)[ RSS](/packages/vgspedro-vivaapi/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

Viva Wallet Native Checkout V2 API PHP Wrapper Library by Aleksey Kuleshov
==========================================================================

[](#viva-wallet-native-checkout-v2-api-php-wrapper-library-by-aleksey-kuleshov)

This package is based on Aleksey Kuleshov work.
-----------------------------------------------

[](#this-package-is-based-on-aleksey-kuleshov-work)

Has been modded to suit my needs.
---------------------------------

[](#has-been-modded-to-suit-my-needs)

This is a wrapper for Native Checkout V2 API of Viva Wallet:

How to use
----------

[](#how-to-use)

This library is installed via [Composer](http://getcomposer.org/). You will need to require `vgspedro/vivaapi`:

```
composer require vgspedro/vivaapi

```

#### Symfony framework

[](#symfony-framework)

#### Create the Controler

[](#create-the-controler)

src/Controler/Payment.php
=========================

[](#srccontrolerpaymentphp)

```
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

use App\Service\VivaWallet;

class PaymentController extends AbstractController
{
    private $environment;
    private $amount = 36812;

    public function __construct(ParameterBagInterface $environment)
    {
        $this->environment = $environment;
        $this->amount = 36812;
    }

    public function index(VivaWallet $viva)
    {

        return $this->render('admin/payment/native.html', [
            'amount' => $this->amount,
            'viva_token' => $viva->getCardChargeToken(),
            'sf_v' => \Symfony\Component\HttpKernel\Kernel::VERSION,
            'payment_url' => $this->environment->get("kernel.environment") == 'prod' ? 'https://www.vivapayments.com' : 'https://demo-api.vivapayments.com',
        ]);
    }

    /**
    * Make some transations accordind the "action" value from the form
    **/
    public function submit(Request $request, VivaWallet $viva)
    {
        $pre_auth = $request->request->get('action') == 'authorization' ? false : true;

        $client = [
            'email' => $request->request->get('email'),
            'phone' => $request->request->get('phone'),
            'full_name' => $request->request->get('name'),
            'request_lang' => 'pt',
            'country_code' => 'PT'
        ];

        $transaction = [
            'amount' => $this->amount,
            'installments' => 1,
            'charge_token' => $request->request->get('token'),
            'merchant_trans' => 'Information to the Merchant',
            'customer_trans' => 'Information to the Client ' .$request->request->get('action'),
            'tip_amount' => 0,
            'pre_auth' => $pre_auth,
            'currency_code' => 978// https://pt.iban.com/currency-codes
        ];

        if($request->request->get('action') == 'charge'){
            $charge = $viva->setCharge($client, $transaction);
            //Something went wrong send info to user
            if ($charge['status'] == 0)
                return new JsonResponse([
                    'status' => 0,
                    'message' => $charge['data'],
                    'data' => $charge
                ]);

            return new JsonResponse([
                'status' => 1,
                'message' => $charge['data'],
                'data' => $trans
            ]);

        }

        else if($request->request->get('action') == 'authorization'){
            $charge = $viva->setAutorization($client, $transaction);
            //Something went wrong send info to user
            if ($charge['status'] == 0)
                return new JsonResponse([
                    'status' => 0,
                    'message' => $charge['data'],
                    'data' => $charge
                ]);

            return new JsonResponse([
                'status' => 1,
                'message' => $charge['data'],
                'data' => $charge
            ]);

        }

        else if($request->request->get('action') == 'charge_capture'){
            $charge = $viva->setCharge($client, $transaction);
            //Something went wrong send info to user
            if ($charge['status'] == 0)
                return new JsonResponse([
                    'status' => 0,
                    'message' => $charge['data'],
                    'data' => $charge
                ]);

            $capture = $viva->setCapture($charge['data']->transactionId, $transaction['amount']);

            return new JsonResponse([
                'status' => 1,
                'message' => $capture['data'],
                'data' => $capture
            ]);
        }

        else if($request->request->get('action') == 'charge_cancel'){
            $charge = $viva->setCharge($client, $transaction);

            //Something went wrong send info to user
            if ($charge['status'] == 0)
                return new JsonResponse([
                    'status' => 0,
                    'message' => $charge['data'],
                    'data' => $charge
                ]);

            //
            $cancel = $viva->setCancel($charge['data']->transactionId, $transaction['amount']);

            return new JsonResponse([
                'status' => 1,
                'message' => $cancel['data'],
                'data' => $cancel
            ]);

        }

        //Something went wrong send info to user
            return new JsonResponse([
                'status' => 0,
                'message' => 'Not Processed',
                'data' => null
            ]);
    }

}
```

#### Create the Service

[](#create-the-service)

src/Service/VivaWallet.php
==========================

[](#srcservicevivawalletphp)

```
namespace App\Service;

use \VgsPedro\VivaApi\Transaction\Authorization;
use \VgsPedro\VivaApi\Transaction\Url;
use \VgsPedro\VivaApi\Transaction\Customer;
use \VgsPedro\VivaApi\Transaction\Charge;
use \VgsPedro\VivaApi\Transaction\Capture;
use \VgsPedro\VivaApi\Transaction\Cancel;
use \VgsPedro\VivaApi\Transaction\ChargeToken;
use \VgsPedro\VivaApi\Transaction\Installments;

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class VivaWallet
{

	private $test_mode; // Boolean
 	private $client_id; // Client ID, Provided by wallet
 	private $client_secret; // Client Secret, Provided by wallet
    private $url; // Url to make request, sandbox or live (sandbox APP_ENV=dev or test) (live APP_ENV=prod)
    private $merchant_id; //Merchant ID , Provided by wallet
    private $api_key; //Api Key, Provided by wallet
	private $headers; //Set the authorization to curl

    public function __construct(ParameterBagInterface $environment){
		$this->test_mode = true;
 		$this->client_id = 'ef7ee87mrt0grg62dbmwms0xzvu29owz5202f9b03ceo7.apps.vivapayments.com';
		$this->client_secret = '4M7ug3jfUh1wZ2Q442Y0L3MDxHz35E';
		$this->api_key = '71-}w%';
        $this->url = $environment->get("kernel.environment") == 'prod' ? 'https://www.vivapayments.com' : 'https://demo-api.vivapayments.com';
    }

	/**
	* Create an authentication Token to pass to client side js
	* @return string $accessToken
	**/
	public function getCardChargeToken(){

		$baseUrl = Url::getUrl($this->test_mode); //Test mode, default is false
		$accessToken = (new Authorization())
		->setClientId($this->client_id) // Client ID, Provided by wallet
		->setClientSecret($this->client_secret) // Client Secret, Provided by wallet
		->setTestMode($this->test_mode) // Test mode, default is false, can be skipped
		->getAccessToken();
		return $accessToken;
	}

	/**
	* Create a charge transaction
	*@param $client // Information of the user
	*@param $trans // Information of the charge transaction
	**/
	public function setCharge(array $client, array $trans){

		$customer = (new Customer())
			->setEmail($client['email'])
			->setPhone($client['phone'])
			->setFullName($client['full_name'])
	      	->setRequestLang($client['request_lang'])
      		->setCountryCode($client['country_code']);

		$transaction = (new Charge())
			->setClientId($this->client_id) // Client ID, Provided by wallet
			->setClientSecret($this->client_secret) // Client Secret, Provided by wallet
			->setTestMode($this->test_mode) // Test mode, default is false, can be skipped
			->setSourceCode('') // Source code, provided by wallet
			->setAmount($trans['amount']) // The amount to charge in currency's smallest denomination (e.g amount in pounds x 100)
			->setInstallments($trans['installments']) // Installments, can be skipped if not used
			->setChargeToken($trans['charge_token']) // Charge token obtained at front end
 			->setMerchantTrns( $trans['merchant_trans'])
 			->setCustomerTrns($trans['customer_trans'])
			->setTipAmount($trans['tip_amount'])
			->setCustomer($customer)
			->setPreAuth($trans['pre_auth']); //If true, a PreAuth transaction will be performed. This will hold the selected amount as unavailable (without the customer being charged) for a period of time.

		$result = $transaction->send();

		if (!empty($transaction->getError()))
			return [
				'status' => 0,
				'data' => $transaction->getError()
			];

		return [
			'status' => 1,
			'data' => $result
		];
	}

	/**
	* Create a charge transaction, the amount is captured and charged.
	*@param $client // Information of the user
	*@param $trans // Information of the charge transaction
	**/
	public function setAutorization(array $client, array $trans){

		$customer = (new Customer())
			->setEmail($client['email'])
			->setPhone($client['phone'])
			->setFullName($client['full_name'])
	      	->setRequestLang($client['request_lang'])
      		->setCountryCode($client['country_code']);

		$transaction = (new Authorization())
			->setClientId($this->client_id) // Client ID, Provided by wallet
			->setClientSecret($this->client_secret) // Client Secret, Provided by wallet
			->setTestMode($this->test_mode) // Test mode, default is false, can be skipped
			->setSourceCode('') // Source code, provided by wallet
			->setAmount($trans['amount']) // The amount to pre-auth in currency's smallest denomination (e.g amount in pounds x 100)
			->setInstallments($trans['installments']) // Installments, can be skipped if not used
			->setChargeToken($trans['charge_token']) // Charge token obtained at front end
			->setCustomer($customer)
			->setPreAuth($trans['pre_auth']);//If true, a PreAuth transaction will be performed. This will hold the selected amount as unavailable (without the customer being charged) for a period of time.

		$result = $transaction->send();

		if (!empty($transaction->getError()))
			return [
				'status' => 0,
				'data' => $transaction->getError()
			];

		return [
			'status' => 1,
			'data' => $result
		];
	}

	/**
	* Capture a charge transaction
	*@param $t_i // Transaction id of authorization transaction
	*@param $amount // The amount to capture in currency's smallest denomination (e.g amount in pounds x 100)
	**/
	public function setCapture(string $t_i, int $amount){

		$transaction = (new Capture())
			->setClientId($this->client_id) // Client ID, Provided by wallet
			->setClientSecret($this->client_secret) // Client Secret, Provided by wallet
			->setTestMode($this->test_mode) // Test mode, default is false, can be skipped
			->setTransactionId($t_i) // Transaction id of authorization transaction
			->setAmount($amount); // The amount to capture in currency's smallest denomination (e.g amount in pounds x 100)

		$result = $transaction->send();

		if (!empty($transaction->getError()))
			return [
				'status' => 0,
				'data' => $transaction->getError()
			];

		return [
			'status' => 1,
			'data' => $result
		];
	}

	/**
	* Cancel a charge transaction
	*@param  $t_i // Transaction id of authorization transaction
	*@param $amount // The amount to capture in currency's smallest denomination (e.g amount in pounds x 100)
	**/
	public function setCancel(string $t_i, int $amount){

		$transaction = (new Cancel())
			->setClientId($this->client_id) // Client ID, Provided by wallet
			->setClientSecret($this->client_secret) // Client Secret, Provided by wallet
			->setTestMode($this->test_mode) // Test mode, default is false, can be skipped
			->setTransactionId($t_i) // Transaction id of authorization transaction
			->setAmount($amount)// The amount to capture in currency's smallest denomination (e.g amount in pounds x 100)
			->setSourceCode(''); // Source code, provided by wallet

		$result = $transaction->send();

		if (!empty($transaction->getError()))
			return [
				'status' => 0,
				'data' => $transaction->getError()
			];

		return [
			'status' => 1,
			'data' => $result
		];
	}

	/**
	* Is possible to get charge token at backend.
	* It may be required in custom integration, more details can be found here: https://developer.vivawallet.com/online-checkouts/native-checkout-v2/
	* @param $card // All the info of the card to make the charge
	* @param $url_redirect // Url to redirect when authentication session finished
	**/
	public function getChargeTokenAtBackend(array $card, string $url_redirect){

		$transaction = (new ChargeToken())
			->setClientId($this->client_id) // Client ID, Provided by wallet
			->setClientSecret($this->client_secret) // Client Secret, Provided by wallet
			->setTestMode($this->test_mode) // Test mode, default is false, can be skipped
			->setAmount($card['amount']) // The amount in currency's smallest denomination (e.g amount in pounds x 100)
			->setCvc($card['cvc']) // Card cvc code
			->setNumber($card['card_number']) // Card number
			->setHolderName($card['holder_name']) // Card holder name
			->setExpirationYear($card['expiration_year']) // Card expiration year
			->setExpirationMonth($card['expiration_month']) // Card expiration month
			->setSessionRedirectUrl($url_redirect); // Url to redirect when authentication session finished
		$result = $transaction->send();

		if (!empty($transaction->getError()))
			return [
				'status' => 0,
				'data' => $transaction->getError()
			];

		// Get charge token
		// $chargeToken = $result->chargeToken;
		// $redirectToACSForm = $result->redirectToACSForm;
		return [
			'status' => 1,
			'data' => $result
		];

	}

	/**
	* Check for installments
	* Retrieve the maximum number of installments allowed on a card.
	*@param $card_number // Number of the credit card
	**/
	public function getInstalments(string $card_number){

		$transaction = (new Installments())
			->setClientId($this->client_id) // Client ID, Provided by wallet
			->setClientSecret($this->client_secret) // Client Secret, Provided by wallet
			->setTestMode($this->test_mode) // Test mode, default is false, can be skipped
			->setNumber($card_number); // Card number

		$result = $transaction->send();

		if (!empty($transaction->getError()))
			return [
				'status' => 0,
				'data' => $transaction->getError()
			];

		// Get number of installments
		// $installments = $result->maxInstallments;
		return [
			'status' => 1,
			'data' => $result
		];
	}
```

#### Create the Template

[](#create-the-template)

templates/admin/payment/native.html
===================================

[](#templatesadminpaymentnativehtml)

```

          Name

          Phone

          Email

            Cardholder Name

                Card Number

                CVV

            Expiration (MM/YYYY)

           /

         Payment Actions

            Charge Only
            Authorized
            Charge & Capture
            Charge & Cancel

        Submit Payment

	Options
	Charge Only = Create a transaction to be Captured
	Authorized = Create a transaction and Captured the amount
	Charge & Capture = Create a transaction then Capture the amount
	Charge & Cancel = Create a transaction then Cancel the transaction

      $(document).ready(function () {
        VivaPayments.cards.setup({
          baseURL: '{{ payment_url }}',
          authToken: '{{ viva_token }}',
          cardHolderAuthOptions: {
            cardHolderAuthPlaceholderId: 'threed-pane',
              cardHolderAuthInitiated: function () {
                $('#threed-pane').show();
              },
              cardHolderAuthFinished: function () {
                $('#threed-pane').hide();
              }
            },
            installmentsHandler: function (response) {
              if (!response.Error) {
                if (response.MaxInstallments == 0)
                  return;
                $('#drpInstallments').show();
                for (i = 1; i = 400 && code = 500 && code
