1. Go to this page and download the library: Download vgspedro/vivaapi library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespaceApp\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;
useSymfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
classVivaWallet{
private $test_mode; // Boolean private $client_id; // Client ID, Provided by walletprivate $client_secret; // Client Secret, Provided by walletprivate $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 walletprivate $api_key; //Api Key, Provided by walletprivate $headers; //Set the authorization to curlpublicfunction__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
**/publicfunctiongetCardChargeToken(){
$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
**/publicfunctionsetCharge(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
**/publicfunctionsetAutorization(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)
**/publicfunctionsetCapture(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)
**/publicfunctionsetCancel(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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.