PHP code example of academe / omnipay-elavon

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

    

academe / omnipay-elavon example snippets


use Omnipay\Omnipay;

$gateway = Omnipay::create('Elavon\Hpp');

$gateway->setMerchantAlias('your-merchant-alias');
$gateway->setApiKey('your-api-key');
$gateway->setRegion('eu'); // 'eu' or 'na'
$gateway->setTestMode(true); // Use sandbox environment

// Step 1: Create the authorization request
$response = $gateway->authorize([
    'amount' => '10.00',
    'currency' => 'USD',
    'transactionId' => 'order-123',
    'returnUrl' => 'https://example.com/payment/return',
    'cancelUrl' => 'https://example.com/payment/cancel',
])->send();

// Step 2: Redirect to Elavon's hosted payment page
if ($response->isRedirect()) {
    $response->redirect();
}

// Step 3: On return, verify the transaction
$response = $gateway->completeAuthorize([
    'transactionReference' => $_GET['transaction'],
])->send();

if ($response->isSuccessful()) {
    $transactionReference = $response->getTransactionReference();
    // Store this reference to capture later
}

$response = $gateway->purchase([
    'amount' => '10.00',
    'currency' => 'USD',
    'transactionId' => 'order-124',
    'returnUrl' => 'https://example.com/payment/return',
    'cancelUrl' => 'https://example.com/payment/cancel',
])->send();

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

// On return, the payment is already captured

$response = $gateway->capture([
    'transactionReference' => $transactionReference,
])->send();

if ($response->isSuccessful()) {
    echo 'Payment captured successfully!';
}

$response = $gateway->refund([
    'transactionReference' => $transactionReference,
    'amount' => '5.00', // Partial or full refund
    'currency' => 'USD',
])->send();

if ($response->isSuccessful()) {
    echo 'Refund processed successfully!';
}

$response = $gateway->void([
    'transactionReference' => $transactionReference,
])->send();

if ($response->isSuccessful()) {
    echo 'Transaction voided successfully!';
}

$response = $gateway->fetchTransaction([
    'transactionReference' => $transactionReference,
])->send();

if ($response->isSuccessful()) {
    echo 'State: ' . $response->getState();
    echo 'Amount: ' . $response->getAmount();
}

$response->isSuccessful();           // Was the transaction successful?
$response->isRedirect();             // Does this er ID
$response->getMessage();             // Response message
$response->getCode();                // Response code

return [
    'merchantAlias' => 'your-merchant-alias',
    'apiKey' => 'your-api-key',
    'region' => 'eu', // or 'na'
    'testMode' => true,
];