alt

Important information

The API reference is now available here.
The deprecated API reference is available here.

Unzer

Accept Unzer Prepayment with UI components

Use Unzer UI component to add Unzer Prepayment payment method to your checkout page.

Overview

Using UI components for Unzer Prepayment you create a payment type resource that will be used to make the payment. You do not need any form fields for this payment method. You can check a demo version of the component here. Basic steps for integrating using UI components are the same for all payment methods and you can read about them here.

Before you begin

Step 1: Add UI components to your payment page
client side

First, you need to initiate our UI components library and add the payment form to your payment page.

Initiate UI component

Load our JS script and CSS stylesheet

Include Unzer’s script and stylesheet on your website.

Always load the script and stylesheet directly from Unzer:

<link rel="stylesheet" href="https://static.unzer.com/v1/unzer.css" />
<script type="text/javascript" src="https://static.unzer.com/v1/unzer.js"></script>
icon
Faster load time
To make your website load faster, insert JavaScript scripts at the bottom of your HTML document.
icon
Importing Unzer styles
To minimize the risk of Unzer styles affecting the styles of your webpage, we suggest putting unzer.css on top of other imported CSS files.

Create an Unzer instance

Create an unzer instance with your public key:

// Creating an unzer instance with your public key
var unzerInstance = new unzer('s-pub-xxxxxxxxxx');
icon
Placeholder keys
In the previous example, we used a placeholder API key for example purposes. You should replace it with your public key.

Localization and languages

We support localization with locale option parameters. Check the Localization page for a list of all the supported locales.

The auto option (applied by default) uses the client’s browser language.

Here you can see how to set the language, in our case ‘de-DE’ - to do so, add a comma separated parameter to your unzer instance:

// Creating an unzer instance with your public key
var unzerInstance = new unzer('s-pub-xxxxxxxxxx', {locale: 'de-DE'});

Implement the payment form

Because no additional data is required for this payment method, you just need to create a simple HTML button on the client side:

<form id="payment-form" class="unzerUI form" novalidate>
    <div id="error-holder" class="field" style="color: #9f3a38">
        <!-- Errors will be inserted here -->
    </div>
    <button class="unzerUI primary button fluid" id="submit-button" type="submit">Pay</button>
</form>

Matching IDs

When choosing your ID for a UI component, make sure that the names in your HTML and JavaScript match.

For example:

<div id="payment-form"> matches the ID used in the script: "payment-form".

Create a payment type resource

Call the method unzerInstance.Prepayment() to create an instance of the payment type Prepayment.

// Creating a Prepayment instance
var prepayment = unzerInstance.Prepayment()

Add an event listener for “submit” of the form.

Inside, create a Promise on the Prepayment object. The Promise gets either resolved or rejected:

let form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
    event.preventDefault();

    prepayment.createResource()
        .then(function(result) {
            // Submit the ID of the created payment type to your
            // backend to perform the payment transaction with it.
            let typeId = result.id;
        })
        .catch(function(error) {
            // handle errors
            document.getElementById('error-holder').innerText = error.customerMessage || error.message || 'Error'
        })
});

Step 2: Make a payment
server side

Make a charge transaction

Now, make a charge transaction with the Prepayment typeId that you created. With a successful charge transaction a payment resource is created. At this point no money has been transferred. The customer has to transfer the amount to the bank account which information are provided in the charge transaction response.

POST https://api.unzer.com/v1/payments/charges

{
  "amount" : "50",
  "currency" : "EUR",
  "returnUrl" : "https://www.my-shop-url.de/returnhandler",
  "resources" : {
    "typeId" : "s-ppy-ptz1cm2u4x7i"
  }
}
$unzer = new Unzer('s-priv-xxxxxxxxxx');
$prepayment = $unzer->fetchPaymentType('s-ppy-ptz1cm2u4x7i');
$charge = $prepayment->charge(20.0, 'EUR', 'https://www.my-shop-url.de/returnhandler');
Unzer unzer = new Unzer("s-priv-xxxxxxxxxx");
Prepayment charge = unzer.charge(BigDecimal.ONE, Currency.getInstance("CHF"), "s-ppy-ptz1cm2u4x7i", new URL("https://www.my-shop-url.de/returnhandler"));

The response looks similar to the following example:

{
    "id": "s-chg-1",
    "isSuccess": true,
    "isPending": false,
    "isError": false,
    "message": {
        "code": "COR.000.100.112",
        "customer": "Request successfully processed in 'Merchant in Connector Test Mode'"
    },
    "amount": "50.6000",
    "currency": "EUR",
    "returnUrl": "https://www.my-shop-url.de/returnhandler",
    "date": "2018-09-26 14:15:13",
    "resources": {
        "paymentId": "s-pay-7786",
        "typeId": "s-ppy-ptz1cm2u4x7i"
    },
    "processing": {
        "iban": "DE89370400440532013000",
        "bic": "COBADEFFXXX",
        "uniqueId": "31HA07BC8137E7B23DE99031E56C4924",
        "shortId": "4018.9771.3370",
        "descriptor": "4018.9771.3370",
        "holder": "Merchant Thuan"
    }
}

For a full description of the charge transaction, refer to the relevant server-side integration documentation page: Charge a payment (direct API calls), Charge a payment (PHP SDK), Charge a payment (Java SDK).

Step 3: Check status of the payment
server side

Once the transaction is made, you can fetch the payment details from the API, by using the resources.paymentId from the charge response above to handle the payment according to its status. Check all possible payment states here.

icon info
Unlike most of the other payment types, the initial Charge will always be in status pending.
GET https://api.unzer.com/v1/payments/{payment_ID}

{
    "id": "s-pay-921",
    "state": {
        "id": 0,
        "name": "pending"
    },
    "amount": {
        "total": "50.6000",
        "charged": "0.0000",
        "canceled": "0.0000",
        "remaining": "50.6000"
    },
    "currency": "EUR",
    "orderId": "",
    "invoiceId": "",
    "resources": {
        "customerId": "",
        "paymentId": "s-pay-921",
        "basketId": "",
        "metadataId": "",
        "payPageId": "",
        "traceId": "b51b892a8896e73c8cfe740803e79531",
        "typeId": "s-ppy-vaq3p5vkfbmm"
    },
    "transactions": [
        {
            "date": "2021-05-17 11:51:32",
            "type": "charge",
            "status": "pending",
            "url": "https://api.heidelpay.com/v1/payments/s-pay-921/charges/s-chg-1",
            "amount": "50.6000"
        }
    ]
}

Step 4. Display payment result
client side

After the transaction is made you should display its result to the customer in the front end. Here you have to display the bank Information, the customer should transfer the money to.

icon
This information should be displayed on the invoice (email/document).
They can also be shown on the success page or in the customer backend of your shop.
sprintf(
    "Please transfer the amount of %f %s to the following account:<br /><br />"
    . "Holder: %s<br/>"
    . "IBAN: %s<br/>"
    . "BIC: %s<br/><br/>"
    . "<i>Please use only this identification number as the descriptor: </i><br/>"
    . "%s",
    $charge->getAmount(),
    $charge->getCurrency(),
    $charge->getHolder(),
    $charge->getIban(),
    $charge->getBic(),
    $charge->getDescriptor()
);

Manage payment
server side

For more details on managing Unzer Prepayment payments, such as refunding them, see Manage Unzer Prepayment payments.

Notifications

We recommend subscribing to the payment event to receive notifications about any changes to the payment resource. As soon as the event is triggered you should fetch the payment and update the order status in your shop according to its status.

  {
    "event":"payment.pending",
    "publicKey":"s-pub-xxxxxxxxxx",
    "retrieveUrl":"https://api.unzer.com/v1/payments/s-pay-774",
    "paymentId":"s-pay-774"
  }

For more details on implementing webhooks to receive notifications, see Notifications page.

Error handling

All requests to the API can result in an error that should be handled. Refer to the Error handling guide to learn more about Unzer API (and other) errors and handling them.

Test & go live

You should always test your integration before going live. First perform test transactions using test data. Next, check against Integration checklist and Go-live checklist to make sure the integration is complete and you’re ready to go live.

See also