PHP code example of apility / nets-easy-omnipay

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

    

apility / nets-easy-omnipay example snippets




use Omnipay\Common\GatewayFactory;
use Apility\Omnipay\NetsEasy\Gateway as NetsEasyGateway;

$factory = new GatewayFactory();
$gateway = $factory->create(NetsEasyGateway::class);

$gateway->setMerchantNumber(/* ... */);
$gateway->setSecretKey(/* ... */);
$gateway->setCheckoutKey(/* ... */);

// Some API keys contains the "test-" prefix. If this is present, we automatically enable test mode.
// However, if your API key does not contain this prefix, you can manually toggle test mode using this method:
$gateway->setTestMode(true);



$transaction = $gateway->fetchTransaction(['paymentId' => $_GET['paymentid']])->send();

if (!$transaction->isSuccessful()) {
    die('Failed to fetch transaction: ' . $transaction->getMessage());
}

if ($transaction->getChargedAmount()) {
    header('Location: /receipt.php?paymentid=' . $transaction->getPaymentId());
    return;
}

if ($transaction->getCancelledAmount() > 0) {
    die('Payment was cancelled: ' . $transaction->getPaymentId());
    return;
}

if ($transaction->getRefundedAmount() > 0) {
    die('Payment refunded: ' . $transaction->getPaymentId());
}

if ($transaction->getUnscheduledSubscriptionId()) {
    header('Location: /unscheduledSubscriptionCallback.php?paymentid=' . $transaction->getPaymentId());
    return;
}

header('Location: /capture.php?paymentid=' . $transaction->getPaymentId());



$transaction = $gateway->fetchTransaction(['paymentId' => $_GET['paymentid']])->send();

if (!$transaction->isSuccessful()) {
    die('Failed to fetch transaction: ' . $transaction->getMessage());
}

if ($transaction->getCancelledAmount() > 0) {
    die('Cannot capture payment, already cancelled: ' . $transaction->getPaymentId());
}

if ($transaction->getRefundedAmount() > 0) {
    die('Cannot capture payment, already refunded: ' . $transaction->getPaymentId());
}

if (!$transaction->getReservedAmount()) {
    header('Location: ' . $transaction->getCheckoutUrl());
    return;
}

if ($transaction->getChargedAmount() == $transaction->getReservedAmount()) {
    header('Location: /receipt.php?paymentid=' . $transaction->getPaymentId());
    return;
}

$capture = $gateway->capture([
    'paymentId' => $transaction->getPaymentId(),
    'amount' => $transaction->getReservedAmount(),
])->send();

if (!$capture->isSuccessful()) {
    die('Capture failed: ' . $capture->getMessage());
}

header('Location: /receipt.php&paymentid=' . $transaction->getPaymentId());



use Apility\Omnipay\NetsEasy\Types\Checkout\IntegrationType;

$authorization = $gateway->authorize([
    // ...
    'checkout' => [
        // ...
        'integrationType' => IntegrationType::EmbeddedCheckout
        // ...
    ]
])->send();

if (!$authorization->isSuccessful()) {
    die('Authorization failed: ' . $authorization->getMessage());
}

$paymentId = $authorization->getPaymentId();
$checkoutKey = $gateway->getCheckoutKey();


use Apility\Omnipay\NetsEasy\Types\Checkout\IntegrationType;

$authorization = $gateway->authorize([
    // ...
    'checkout' => [
        // ...
        'returnUrl' => 'http://example.com/callback.php',
        'integrationType' => IntegrationType::HostedPaymentPage
        // ...
    ]
])->send();

if (!$authorization->isSuccessful()) {
    if ($authorization->isRedirect()) {
        return $authorization->redirect();
    }

    die('Authorize failed: ' . $authorization->getMessage());
}

header('Location: /callback.php?paymentid=' . $authorization->getPaymentId());
html
<!-- Production -->
<script src="https://checkout.dibspayment.eu/v1/checkout.js?v=1"></script>
<!-- Test -->
<script src="https://test.checkout.dibspayment.eu/v1/checkout.js?v=1"></script>
<script>
    const checkoutOptions = {
        checkoutKey: " echo $checkoutKey; 
 echo $paymentId;