PHP code example of zaver / sdk

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

    

zaver / sdk example snippets


// URL: https://example.com/checkout

use Zaver\SDK\Checkout;
use Zaver\SDK\Object\MerchantUrls;
use Zaver\SDK\Object\PaymentCreationRequest;
use Zaver\SDK\Object\LineItem;
use Zaver\SDK\Config\ItemType;

const API_KEY = '<your API key>';
const CALLBACK_TOKEN = '<your callback token>';
const IS_TEST_ENVIRONMENT = true;

$api = new Checkout(API_KEY, IS_TEST_ENVIRONMENT);

$item = LineItem::create()
    ->setName('Fancy pants')
    ->setMerchantReference('FANCY-123')
    ->setQuantity(2)
    ->setUnitPrice(1000)
    ->setTotalAmount(2000)
    ->setTaxRatePercent(25)
    ->setTaxAmount(400)
    ->setItemType(ItemType::PHYSICAL);

$shipping = LineItem::create()
    ->setName('DHL')
    ->setQuantity(1)
    ->setUnitPrice(100)
    ->setTotalAmount(100)
    ->setTaxRatePercent(25)
    ->setTaxAmount(20)
    ->setItemType(ItemType::SHIPPING);

$urls = MerchantUrls::create()
    ->setSuccessUrl('https://example.com/thank-you')
    ->setCancelUrl('https://example.com/canceled')
    ->setCallbackUrl('https://example.com/api/payment-callback');

$request = PaymentCreationRequest::create()
    ->setMerchantPaymentReference('123456')
    ->setAmount(2100)
    ->setCurrency('SEK')
    ->setMarket('SE')
    ->setTitle('My fancy payment')
    ->setMerchantUrls($urls)
    ->addLineItem($item)
    ->addLineItem($shipping);

$payment = $api->createPayment($request);

echo $payment->getPaymentStatus();
// Output: CREATED

echo $api->getHtmlSnippet($payment);
// Outputs iframe with Zaver Checkout

// URL: https://example.com/api/payment-callback

use Zaver\SDK\Checkout;

const API_KEY = '<your API key>';
const CALLBACK_TOKEN = '<your callback token>';
const IS_TEST_ENVIRONMENT = true;

$api = new Checkout(API_KEY, IS_TEST_ENVIRONMENT);
$payment = $api->receiveCallback(CALLBACK_TOKEN);

echo $payment->getPaymentStatus();
// Output: SETTLED

use Zaver\SDK\Refund;
use Zaver\SDK\Object\RefundCreationRequest;

const API_KEY = '<your API key>';
const CALLBACK_TOKEN = '<your callback token>';
const IS_TEST_ENVIRONMENT = true;

$api = new Refund(API_KEY, IS_TEST_ENVIRONMENT);

// It does most likely not make any sense to fetch the payment first - this is just for
// showing the relation between a payment and a refund.
$payment = $api->getPaymentStatus('463d6d10-4c0f-424b-b804-8e95114864dd');

$urls = MerchantUrls::create()
    ->setCallbackUrl('https://example.com/api/refund-callback');

$request = RefundCreationRequest::create()
    ->setPaymentId($payment->getPaymentId())
    ->setRefundAmount($payment->getAmount())
    ->setDescription('Mr Fancy Pants changed his mind')
    ->setMerchantUrls($urls);

foreach($payment->getLineItems() as $paymentItem) {
    $refundItem = RefundLineItem::create()
        ->setLineItemId($paymentItem->getId())
        ->setRefundTotalAmount($paymentItem->getTotalAmount())
        ->setRefundTaxAmount($paymentItem->getTaxAmount())
        ->setRefundTaxRatePercent($paymentItem->getTaxRatePercent())
        ->setRefundQuantity($paymentItem->getQuantity())
        ->setRefundUnitPrice($paymentItem->getUnitPrice());
    
    $request->addLineItem($refundItem);
}

$refund = $api->createRefund($request);

echo $refund->getStatus();
// Outputs: PENDING_MERCHANT_APPROVAL
    

// URL: https://example.com/api/refund-callback

use Zaver\SDK\Refund;

const API_KEY = '<your API key>';
const CALLBACK_TOKEN = '<your callback token>';
const IS_TEST_ENVIRONMENT = true;

$api = new Refund(API_KEY, IS_TEST_ENVIRONMENT);
$refund = $api->receiveCallback(CALLBACK_TOKEN);

echo $refund->getStatus();
// Output: PENDING_EXECUTION