PHP code example of omnipay-taiwan / omnipay-ecpay

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

    

omnipay-taiwan / omnipay-ecpay example snippets


$gateway = Omnipay::create('ECPay');
$gateway->initialize([
    'MerchantID' => 'your-merchant-id',
    'HashKey' => 'your-hash-key',
    'HashIV' => 'your-hash-iv',
    'testMode' => true,
]);

$response = $gateway->purchase([
    'transactionId' => 'order-number',
    'amount' => '1000',
    'currency' => 'TWD',
    'description' => 'order description',
    'returnUrl' => 'https://example.com/return',
    'notifyUrl' => 'https://example.com/notify',
    'ChoosePayment' => 'Credit', // or ATM, CVS, BARCODE, BNPL, ALL
])->send();

if ($response->isRedirect()) {
    $response->redirect();
}

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

if ($response->isSuccessful()) {
    // mark order as paid
}

echo $response->getReply(); // '1|OK', 

$gateway->refund([
    'transactionId' => 'order-number',
    'transactionReference' => 'ecpay-trade-no',
    'amount' => '1000',
])->send();

$gateway->void([
    'transactionId' => 'order-number',
    'transactionReference' => 'ecpay-trade-no',
    'amount' => '1000',
])->send();

$gateway->fetchTransaction([
    'transactionId' => 'order-number',
])->send();