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

use Zaver\SDK\Object\PaymentMethodsRequest;

$request = PaymentMethodsRequest::create()
    ->setMarket('SE')
    ->setAmount(1500)
    ->setCurrency('SEK');

foreach($api->getPaymentMethods($request)->getPaymentMethods() as $method) {
    echo $method->getPaymentMethod();         // PAY_LATER
    echo $method->getPaymentMethodVariant();  // 30_DAYS, or null when the merchant has no variants
    echo $method->getTitle();
    // Consumer credit methods listed for an amount carry the texts to show under the method:
    echo $method->getCreditWarningHtml();     // the market's credit warning, HTML; null where none is 

use Zaver\SDK\Config\Language;
use Zaver\SDK\Config\PaymentMethod;
use Zaver\SDK\Object\CreditPricingRequest;

$request = CreditPricingRequest::create()
    ->setAmount(5000)
    ->setCurrency('SEK')
    ->setMarket('SE')
    ->setPaymentMethod(PaymentMethod::INSTALLMENTS)   // optional; every consumer credit method when omitted
    ->setLanguage(Language::SV_SE);                   // optional; the market's language when omitted

$pricing = $api->getCreditPricing($request);

echo $pricing->getLowestMonthlyAmount();              // 237.33, or null when nothing is paid monthly
foreach ($pricing->getOptions() as $option) {         // invoices first, then the terms shortest first
    echo $option->getPaymentMethod();                 // PAY_LATER, INSTALLMENTS, INSTALLMENTS_STATIC_AMOUNT or MONTHLY_INVOICE
    echo $option->getDueInDays();                     // 30 for an invoice, null otherwise
    echo $option->getNumberOfInstallments();          // 24 for a payment plan, null for an invoice
    echo $option->getMonthlyAmount(), $option->getTotalAmount();
    echo $option->getEffectiveAnnualInterestRatePercent();   // "11.63%", for display
    echo $option->getEffectiveAnnualInterestRateFraction();  // 0.1163, for calculation
    echo $option->getRepresentativeExample();         // plain text in the requested language
    echo $option->getRepresentativeExample('en-GB');
    foreach ($option->getPaymentPlanConversionOptions() as $plan) {   // an invoice's payment plan, longest term
        echo $plan->getNumberOfInstallments(), $plan->getMonthlyAmount();
    }
}
echo $pricing->getCreditWarningHtml();                // the warning the market 

use Zaver\SDK\Checkout;
use Zaver\SDK\Utils\UserAgent;

$userAgent = UserAgent::create()
    ->addProduct('Shopware', '6.6.10.6')
    ->addProduct('zaver-shopware6-plugin', '1.0.11');

$api = new Checkout(API_KEY, IS_TEST_ENVIRONMENT, CALLBACK_TOKEN, 15, $userAgent);
// User-Agent: zaver-php-sdk/3.1.0 Shopware/6.6.10.6 zaver-shopware6-plugin/1.0.11 PHP/8.3.12

use Zaver\SDK\Utils\Error;

try {
    $payment = $api->createPayment($request);
}
catch(Error $e) {
    $e->getErrorMessage();     // first error message reported by Zaver, or a generic message
    $e->getErrorCode();        // Zaver error code, e.g. INVALID_AMOUNT (null if none)
    $e->getHttpStatus();       // HTTP status of the response, null if no response was received
    $e->getResponseHeaders();  // lower-cased response headers;