PHP code example of erilshk / vinti4net

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

    

erilshk / vinti4net example snippets




rilshk\Sisp\Vinti4Net;
use Erilshk\Sisp\Billing;
use Erilshk\Sisp\Exceptions\Vinti4Exception;

// Credenciais POS fornecidas pela SISP.
$vinti4 = new Vinti4Net(
    posID: getenv('VINTI4_POS_ID'),
    posAuthCode: getenv('VINTI4_AUTH_CODE'),
);

// Dados de faturação para a compra 3DS.
$billing = Billing::from([
    'email' => '[email protected]',
    'country' => '132',
    'city' => 'Praia',
    'address' => 'Avenida Cidade de Lisboa',
    'postalCode' => '7600',
]);

try {
    // Referência única da tentativa de pagamento.
    $reference = Vinti4Net::generateMerchantRef();
    $vinti4->setMerchant(reference: $reference);

    // Montante em CVE, obtido na aplicação.
    $vinti4->preparePurchase(amount: '1500', billing: $billing);

    // Encaminhar o cliente para a página de pagamento da SISP.
    echo $vinti4->createPaymentForm(
        responseUrl: 'https://example.cv/pagamentos/callback',
        lang: 'pt',
    );

} catch (Vinti4Exception $exception) {
    error_log($exception->getMessage());
    http_response_code(400);
}

use Erilshk\Sisp\Entity;
$vinti4
    ->setMerchant($reference)
    ->prepareServicePayment(
        amount: 2500,
        entity: Entity::SERVICE_AGUAS_SANTIAGO,
        number: '123456789',
    );

use Erilshk\Sisp\Entity;
$vinti4
    ->setMerchant($reference)
    ->prepareRecharge(
        amount: 500,
        entity: Entity::RECHARGE_ALOU,
        number: '9912345',
    );

$vinti4
    ->setMerchant($reference)
    ->prepareRefund(
        amount: 1500,
        transactionID: '3456',
        clearingPeriod: '2411',
    );

echo $vinti4->createPaymentForm(
    'https://example.cv/pagamentos/refund-callback'
);



rilshk\Sisp\Exceptions\Vinti4Exception;
use Erilshk\Sisp\Vinti4Net;

// Use as mesmas credenciais POS do pedido.
$vinti4 = new Vinti4Net(
    getenv('VINTI4_POS_ID'),
    getenv('VINTI4_AUTH_CODE'),
);

try {
    // Validar os dados enviados pela SISP.
    $response = $vinti4->processResponse($_POST);

    // Rejeitar uma resposta com assinatura inválida.
    if ($response->hasInvalidFingerprint()) {
        http_response_code(400);
        exit('Resposta inválida.');
    }

    if ($response->isSuccess()) {
        $transactionId = $response->getTransactionId();
        $merchantRef = $response->getMerchantRef();
        $amount = $response->getAmount();

        // Confirme a referência e o valor esperados e torne a atualização idempotente.
    } elseif ($response->isCancelled()) {
        // O cliente cancelou a operação.
    } else {
        // A operação foi recusada ou falhou.
    }

    http_response_code(200);
} catch (Vinti4Exception $exception) {
    error_log($exception->getMessage());
    http_response_code(400);
}

$vinti4->setMerchant(Vinti4Net::generateMerchantRef());
$vinti4->prepareTokenRequest('100');

echo $vinti4->createPaymentForm('https://example.cv/tokens/resposta');

// Código da operação iniciada, recuperado do pedido guardado.
$response = $vinti4->processTokenResponse($_POST, $expectedTransactionCode);

if ($response->isSuccess()) {
    // Confira a referência e a sessão com o pedido guardado antes de persistir.
    $token = $response->getToken();
    $limits = $response->getTokenLimits();

    // Guarde o token, os limites e o consentimento associados ao cliente.
}

$vinti4->setMerchant(Vinti4Net::generateMerchantRef());
$vinti4->prepareTokenPurchase($token, '500');

// Guarde o pedido antes de apresentar o formulário.
echo $vinti4->createPaymentForm('https://example.cv/tokens/pagamento-resposta');

use Erilshk\Sisp\Entity;

// Pagamento de serviço: token, montante, entidade e referência da fatura.
$vinti4->prepareTokenServicePayment(
    token: $token,
    amount: '2500',
    entity: Entity::SERVICE_AGUAS_SANTIAGO,
    number: '123456789',
);

use Erilshk\Sisp\Entity;

// Recarga: token, montante, entidade e número a recarregar.
$vinti4->prepareTokenRecharge(
    token: $token,
    amount: '500',
    entity: Entity::RECHARGE_ALOU,
    number: '9912345',
);

// Cancelamento: apenas o token a cancelar.
$vinti4->prepareTokenCancellation(token: $token);

echo $response->renderReceipt(data: [
    'companyName' => 'Minha Empresa, Lda.',
    'logo' => '/assets/logo.svg',
]);

echo $response->renderRefundReceipt(
    amount: '1500',
    originalTransactionId: '3456',
    data: ['companyName' => 'Minha Empresa, Lda.'],
);

echo $response->renderReceipt(
    template: __DIR__ . '/templates/receipt.php',
    data: ['supportEmail' => '[email protected]'],
);

if ($response->isDccEnabled()) {
    echo $response->renderDccReceipt();
}

$response->generateReceiptHtml('Minha Empresa');
$response->generateReceiptText('Minha Empresa');

use Erilshk\Sisp\Exceptions\Vinti4Exception;

try {
    // integração
} catch (Vinti4Exception $exception) {
    error_log($exception->getMessage());
    // Registe o erro e apresente uma mensagem adequada ao cliente.
}