Basic usage of the SDK

The Unzer object

The Unzer object is the main object. You always need it, in order to communicate with the Unzer Payment API (PAPI). In addition it is the facade to all functionality in the SDK. It provides…

  • …methods to perform CRUD operations on the resources (payment type, customer, basket, etc.)
  • …methods that allow transactions like authorization, charge, cancellation (i.e.refund and reversal) and shipment

This means you have to create the Unzer object and provide your private-key prior to everything you want to do with the SDK.

To display the potential error messages in the language of the client, you can provide the locale within the second argument of the Unzer class constructor. Go to the localization and languages section for a list of supported locales.

// create the Unzer object
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
// create the Unzer object with language
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx', 'de-DE');

The Payment object

Whenever an authorization or a charge transaction is performed successfully an instance of the Payment class is automatically created.

The Payment object contains all information on the payment process and ties all required resources together. Some of the information the Payment object stores, are:

  • references to resources, such as customer, payment type
  • references to transactions: charges, authorization, shipment
  • the amount information: total, charged, canceled and remaining
  • the currency
  • the state of the payment: pending, canceled, completed, and so on

By fetching the Payment object, you can access all the information on the whole payment process.

Creating a Payment Type resource

In order to perform transactions the API needs to know, which payment type to use. There are two options:

  1. Reference a payment type that already exists (e. g. registered credit card)
  2. Create and reference a new payment type

The following examples show how to create the different payment types.

$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$alipay = new UnzerSDK\Resources\PaymentTypes\Alipay();
$alipay = $unzer->createPaymentType($alipay);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$bancontact = new UnzerSDK\Resources\PaymentTypes\Bancontact();
$bancontact = $unzer->createPaymentType($bancontact);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$pis = new UnzerSDK\Resources\PaymentTypes\PIS();
$pis = $unzer->createPaymentType($pis);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$card = new UnzerSDK\Resources\PaymentTypes\Card('4444333322221111', '03/20');
$card->setCvc('123');
$card = $unzer->createPaymentType($card);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$directDebit = new UnzerSDK\Resources\PaymentTypes\SepaDirectDebit('DE89370400440532013000');
$directDebit = $unzer->createPaymentType($directDebit);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$directDebitGuaranteed = new UnzerSDK\Resources\PaymentTypes\SepaDirectDebitGuaranteed('DE89370400440532013000');
$directDebitGuaranteed = $unzer->createPaymentType($directDebitGuaranteed);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$eps = new UnzerSDK\Resources\PaymentTypes\EPS();
$eps = $unzer->createPaymentType($eps);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$giropay = new UnzerSDK\Resources\PaymentTypes\Giropay();
$giropay = $unzer->createPaymentType($giropay);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$ideal = (new UnzerSDK\Resources\PaymentTypes\Ideal())->setBic('RABONL2U');
$ideal = $unzer->createPaymentType($ideal);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

// get available instalment plans
$today = new \DateTime('now');
$instalmentPlans = $unzer->fetchInstallmentPlans(119.0, 'EUR', 4.99, $today);

// select instalment plan
$selectedPlan = $instalmentPlans->getPlans()[0];

$ins = new UnzerSDK\Resources\PaymentTypes\InstallmentSecured($selectedPlan, 'DE46940594210000012345', 'Manuel Weißmann', $today, 'COBADEFFXXX', $today, $today);
$ins = $unzer->createPaymentType($ins);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$invoice = new UnzerSDK\Resources\PaymentTypes\Invoice();
$invoice = $unzer->createPaymentType($invoice);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$ivf = new UnzerSDK\Resources\PaymentTypes\InvoiceFactoring();
$ivf = $unzer->createPaymentType($ivf);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$paypal = new UnzerSDK\Resources\PaymentTypes\Paypal();
$paypal = $unzer->createPaymentType($paypal);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$prepayment = new UnzerSDK\Resources\PaymentTypes\Prepayment();
$prepayment = $unzer->createPaymentType($prepayment);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$p24 = new UnzerSDK\Resources\PaymentTypes\Przelewy24();
$p24 = $unzer->createPaymentType($p24);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$sofort = new UnzerSDK\Resources\PaymentTypes\Sofort();
$sofort = $unzer->createPaymentType($sofort);
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

$wechatpay = new UnzerSDK\Resources\PaymentTypes\Wechatpay();
$wechatpay = $unzer->createPaymentType($wechatpay);
Restrictions on card types
You cannot create the Card type with the PHP SDK unless you are fully PCI DSS certified.
Please refer to the section Web Integration to learn how to use our UIComponents to handle card information.

Update a Payment Type resource

Properties of the Unzer Instalment payment type can be updated.

// update Unzer Instalment payment type
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');

/** @var UnzerSDK\Resources\PaymentTypes\InstallmentSecured $ins */
$ins = $unzer->fetchPaymentType('s-priv-1');

$yesterday = (new \DateTime())->add(\DateInterval::createFromDateString('yesterday');

$ins->setIban('DE89370400440532013000')
            ->setBic('COBADEFFXXX')
            ->setAccountHolder('Peter Universum')
            ->setInvoiceDate($yesterday)
            ->setInvoiceDueDate($yesterday);

$ins = $unzer->updatePaymentType($ins);