PHP code example of digital-link / saferpay

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

    

digital-link / saferpay example snippets

{.php}
$saferpay = new Saferpay;
{.php}
$saferpay->setHttpClient(new BuzzClient());
{.php}
$amount = 1200;
$currency = 'CHF';

if (getParam('status') == 'success') {
    $payConfirmParameter = $saferpay->verifyPayConfirm(getParam('DATA'), getParam('SIGNATURE'));
    if ($payConfirmParameter->get('AMOUNT') == $amount && $payConfirmParameter->get('CURRENCY') == $currency) {
        $saferpay->payCompleteV2($payConfirmParameter, 'Settlement');
        echo 'payment success!';
    } else {
        $saferpay->payCompleteV2($payConfirmParameter, 'Cancel');
        echo 'payment failed!';
    }
} else {
    $payInitParameter = new PayInitParameter();
    $payInitParameter->setAccountid(PayInitParameter::SAFERPAYTESTACCOUNT_ACCOUNTID);
    $payInitParameter->setAmount($amount);
    $payInitParameter->setCurrency($currency);
    $payInitParameter->setDescription(sprintf('Ordernumber: %s', '000001'));
    $payInitParameter->setSuccesslink(requestUrl() . '?status=success');
    $payInitParameter->setFaillink(requestUrl() . '?status=fail');
    $payInitParameter->setBacklink(requestUrl() . '?status=back');
    $payInitParameter->setDelivery('no'); // hide address form
    header('Location: ' . $saferpay->createPayInit($payInitParameter) , 302);
}
{.php}
$saferpay = new Saferpay;
{.php}
$saferpay->setHttpClient(new BuzzClient());
{.php}
$amount = 1200;
$currency = 'CHF';

if (getParam('status') == 'success') {
    $payConfirmParameter = new PayConfirmParameter;
    $billpayPayConfirmParameter = new BillpayPayConfirmParameter;

    $payConfirmParameterCollection = new Collection($payConfirmParameter->getRequestUrl());
    $payConfirmParameterCollection->addCollectionItem($payConfirmParameter);
    $payConfirmParameterCollection->addCollectionItem($billpayPayConfirmParameter);

    $payConfirmParameterCollection = $this->getSaferpay()->verifyPayConfirm(
        getParam('DATA'),
        getParam('SIGNATURE'),
        $objPayConfirmParameterCollection
    );

    $payCompleteParameter = new PayCompleteParameter;
    $billpayPayCompleteParameter = new BillpayPayCompleteParameter;

    $payCompleteParameterCollection = new Collection($payCompleteParameter->getRequestUrl());
    $payCompleteParameterCollection->addCollectionItem($payCompleteParameter);
    $payCompleteParameterCollection->addCollectionItem($billpayPayCompleteParameter);

    $payCompleteResponse = new PayCompleteResponse;
    $billpayPayCompleteResponse = new BillpayPayCompleteResponse;

    $payCompleteResponseCollection = new Collection($payCompleteResponse->getRequestUrl());
    $payCompleteResponseCollection->addCollectionItem($payCompleteResponse);
    $payCompleteResponseCollection->addCollectionItem($billpayPayCompleteResponse);

    if ($payConfirmParameterCollection->get('AMOUNT') == $amount &&
        $payConfirmParameterCollection->get('CURRENCY') == $currency) {

        $this->getSaferpay()->payCompleteV2(
            $payConfirmParameterCollection,
            'Settlement',
            PayInitParameter::SAFERPAYTESTACCOUNT_SPPASSWORD,
            $payCompleteParameterCollection,
            $payCompleteResponseCollection
        );

        echo 'payment success!';
    } else {
        $this->getSaferpay()->payCompleteV2(
            $payConfirmParameterCollection,
            'Cancel',
            PayInitParameter::SAFERPAYTESTACCOUNT_SPPASSWORD,
            $payCompleteParameterCollection,
            $payCompleteResponseCollection
        );

        echo 'payment failed!';
    }
} else {
    $payInitParameter = new PayInitParameter();
    $payInitParameter->setAccountid(PayInitParameter::SAFERPAYTESTACCOUNT_ACCOUNTID);
    $payInitParameter->setAmount($amount);
    $payInitParameter->setCurrency($currency);
    $payInitParameter->setDescription(sprintf('Ordernumber: %s', '000001'));
    $payInitParameter->setSuccesslink(requestUrl() . '?status=success');
    $payInitParameter->setFaillink(requestUrl() . '?status=fail');
    $payInitParameter->setBacklink(requestUrl() . '?status=back');
    $payInitParameter->setProviderset(array(BillpayPayInitParameter::PROVIDERSET_BILLPAY_INVOICE));

    $billpayPayInitParameter = new BillpayPayInitParameter();
    $billpayPayInitParameter->setLegalform(BillpayPayInitParameter::LEGALFORM_MISC);
    $billpayPayInitParameter->setDeliveryGender(BillpayPayInitParameter::GENDER_COMPANY);
    $billpayPayInitParameter->setDeliveryFirstname('John');
    $billpayPayInitParameter->setDeliveryLastname('Doe');
    $billpayPayInitParameter->setDeliveryStreet('Samplestreet 0');
    $billpayPayInitParameter->setDeliveryZip('00000');
    $billpayPayInitParameter->setDeliveryCity('Samplecity');
    $billpayPayInitParameter->setDeliveryCountry('US');
    $billpayPayInitParameter->setDeliveryPhone('+10000000000');

    $payInitParameterCollection = new Collection($payInitParameter->getRequestUrl());
    $payInitParameterCollection->addCollectionItem($payInitParameter);
    $payInitParameterCollection->addCollectionItem($billpayPayInitParameter);

    header('Location: ' . $saferpay->createPayInit($payInitParameterCollection) , 302);
}
{.php}
function requestUrl()
{
    $protocol = strtolower(substr($_SERVER['SERVER_PROTOCOL'], 0, strpos($_SERVER['SERVER_PROTOCOL'], '/')));
    return $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}

function getParam($key, $default = null)
{
    return array_key_exists($key, $_REQUEST) ? $_REQUEST[$key] : $default;
}