PHP code example of phpviet / omnipay-vtcpay

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

    

phpviet / omnipay-vtcpay example snippets


use Omnipay\Omnipay;

$gateway = Omnipay::create('VTCPay');
$gateway->initialize([
    'website_id' => 'Do VTCPay cấp',
    'security_code' => 'Do VTCPay cấp',
]);

$response = $gateway->purchase([
    'receiver_account' => '0963465816',
    'reference_number' => microtime(false),
    'amount' => 50000,
    'url_return' => 'https://phpviet.org'
])->send();

if ($response->isRedirect()) {
    $redirectUrl = $response->getRedirectUrl();
    
    // TODO: chuyển khách sang trang VTCPay để thanh toán
}

$response = $gateway->completePurchase()->send();

if ($response->isSuccessful()) {
    // TODO: xử lý kết quả và hiển thị.
    print $response->amount;
    print $response->reference_number;
    
    var_dump($response->getData()); // toàn bộ data do VTCPay gửi sang.
    
} else {

    print $response->getMessage();
}

$response = $gateway->notification()->send();

if ($response->isSuccessful()) {
    // TODO: xử lý kết quả.
    print $response->amount;
    print $response->reference_number;
    
    var_dump($response->getData()); // toàn bộ data do VTCPay gửi sang.
    
} else {

    print $response->getMessage();
}