Accept Unzer Installment with UI components
Use Unzer UI component to add Unzer Installment payment to your checkout page.
Overview
Using UI components for Unzer Installment you get a ready-made form with fields necessary to make this type of payment. 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.
With Unzer Installment you need to provide information about the customer via a customer
resource and the purchased products via a basket
resource when you make the transaction. This is required for customer assessment and transaction approval.
Before you begin
Before you begin- Check the basic integration requirements.
- Familiarize yourself with general guide on integrating using UI components.
Step 1: Add UI components to your payment pageclient side
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
Initiate UI componentLoad our JS script and CSS stylesheet
Load our JS script and CSS stylesheetInclude 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>
To make your website load faster, insert JavaScript scripts at the bottom of your HTML document.
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 instanceCreate an unzer
instance with your public key:
// Creating an unzer instance with your public key
var unzerInstance = new unzer('s-pub-xxxxxxxxxx');
In the previous example, we used a placeholder API key for example purposes. You should replace it with your public key.
Localization and languages
Localization and languagesWe 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
Option 1: Implement with the customer form
If you don’t have information about the customer (and accompanying customer ID) or want to update information of an already existing customer, you can do so using the customer form. Check Customer UI component page for more information.
Insert the customer form
To use the customer form, you must insert it into your payment form. We recommend placing it above the payment elements.
<form id="example-payment-form" class="unzerUI form">
<div id="example-customer" class="field">
<!-- The customer form UI elements will be inserted here -->
</div>
<div id="example-Installment-secured">
<!-- The payment form UI elements will be inserted here -->
</div>
<div id="error-holder" class="field" style="color: #d0021b">
<!-- Errors will be inserted here -->
</div>
<!-- Submit button (hidden and disabled) -->
<button class="unzerUI primary button fluid" id="submit-button" type="submit" style="display: none" disabled>Pay</button>
</form>
Create a customer instance and render the form
A customer instance is created like any other UI component. In Order to render the customer form, you will need to call either create()
or update()
method.
In case you wish to initialize the form with the data of an already created customer, you need to call the initFormFields()
function. This has to be done before calling update()
.
// Create a customer instance
let customer = unzerInstance.Customer();
// render the customer form
customer.create({ containerId: 'example-customer' });
// Optional event handling.
let customer = unzerInstance.Customer();
// Initialize the form and pass the ID of the customer
customer.initFormFields({
"lastname": "Universum",
"firstname": "Peter",
"salutation": "mr",
"birthDate": "1987-12-2",
"shippingAddress": {
"street": "Hugo-Junkers-Str. 5",
"state": "DE-BO",
"zip": "60386",
"city": "Frankfurt am Main",
"country": "DE",
"shippingType": "equals-billing"
}
});
// Optional event handling. It has to be called before the update() call
// render the customer form
customer.update('p-cst-xxxxxxxxxxxxx', { containerId: 'example-customer' });
Event handling (optional)
Optionally, you can add a custom event listener that handles the validation result of all customer and payment input fields.
update()
call.var isValidCustomerInput = false
var isValidPaymentInput = false
customer.addEventListener('validate', function(e) {
if (e.success) {
isValidCustomerInput = true
if (isValidPaymentInput) {
// run some code (for example, enable the `pay` button)
submitButton.removeAttribute('disabled');
}
} else if (e.success === false) {
// run some code (for example, disable the `pay` button)
submitButton.setAttribute('disabled', true);
}
})
Option 2: Create the customer on the server side
You can also use the customer data provided by your shop system to create the customer
resource on the server side. This way you can omit rendering the customer form in the front end and just create the resource and use the ID in your transaction request.
For server-side customer resource creation, you just need to create a simple HTML button on the client side:
Create a payment type resource
To create an Unzer Installment Secured instance, call the unzerInstance.InstallmentSecured()
method:
let installmentSecured = unzerInstance.InstallmentSecured();
// Render the payment form
installmentSecured.create({
containerId: 'example-Installment-secured',
amount: 500,
currency: 'EUR',
orderDate: '2019-01-01',
effectiveInterest: 4.5
})
Please make sure to pass valid parameters to the create
function.
Parameter | Type | Description | Default value |
---|---|---|---|
containerId required | String | The HTML ID of the DOM element, where the UI component will be inserted. | - |
amount required | number | Positive amount. | - |
currency required | String | Currency code. | - |
orderDate required | String | Order date. | - |
effectiveInterest required | number | Effective interest | - |
Plan list/details event handling (optional)
Optionally, you can add a custom installmentSecuredEvent
event listener that handles the plan selection and validation result of all customer and payment input fields.
var submitButton = document.getElementById('submit-button')
installmentSecured.addEventListener('installmentSecuredEvent', function(e) {
switch (e.currentStep) {
case 'plan-list':
// run some code (for example, hide and disable the `pay` button)
submitButton.setAttribute('style', 'display: none')
submitButton.setAttribute('disabled', true)
break;
case 'plan-detail':
// run some code (for example, show the `pay` button)
submitButton.setAttribute('style', 'display: block')
break;
default:
break;
}
if (e.action === 'validate' && e.success) {
isValidPaymentInput = true
if (isValidCustomerInput) {
// run some code (for example, enable the `pay` button)
submitButton.removeAttribute('disabled');
}
} else if (e.action === 'validate' && !e.success) {
// run some code (for example, disable the `pay` button)
submitButton.setAttribute('disabled', true)
}
})
Add an event listener and submit the form
Option 1: Without the customer form
Get the HTML form for example, by its unique ID, and add an event listener.
Inside, create a Promise
on the InstallmentSecured
object. The Promise
gets either resolved or rejected:
let form = document.getElementById('example-payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
installmentSecured.createResource()
.then(function(result) {
let typeId = result.id;
// submit the payment type ID to your server-side integration
})
.catch(function(error) {
document.getElementById('error-holder').innerText = error.customerMessage || error.message || 'Error'
})
});
Option 2: With the customer create form
Get the HTML form, for example, using its unique ID, and add an event listener.
Create a Promise
for the InstallmentSecured
object and a second one for the customer
object, and handle both at once using Promise.all()
.
let form = document.getElementById('example-payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
let customerPromise = customer.createCustomer();
let paymentTypePromise = installmentSecured.createResource();
Promise.all([customerPromise, paymentTypePromise])
.then(function(values) {
let customerId = values[0].id;
let typeId = values[1].id;
// Submit the IDs to your server-side integration.
})
.catch(function(error) {
document.getElementById('error-holder').innerText = error.customerMessage || error.message || 'Error'
})
});
let form = document.getElementById('example-payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
let customerPromise = customer.updateCustomer();
let paymentTypePromise = installmentSecured.createResource();
Promise.all([customerPromise, paymentTypePromise])
.then(function(values) {
let customerId = values[0].id;
let typeId = values[1].id;
// Submit the IDs to your server-side integration.
})
.catch(function(error) {
document.getElementById('error-holder').innerText = error.customerMessage || error.message || 'Error'
})
});
When creating the Promises
, be sure you use the following methods:
createResource()
for the payment typecreateCustomer()
orupdateCustomer()
for thecustomer
resource
Step 2: Make a paymentserver side
Step 2: Make a payment [server side]Other than the mandatory step of creating the payment type resource, you also require a customer
and a basket
resource for Unzer Installment payments.
This payment type requires the following workflow:
- Fetch the available installment plans and show them to the customer to chose from.
This is done automatically when creating the payment type in the front end. - Creating the payment type resource using the installment plan details selected by the customer
- Performing an
authorization
transaction to initialize the selected installment plan. - Show the selected installment plan and its conditions to the customer.
- Performing a
charge
transaction after the customer agrees to the conditions. - Perform a
shipment
transaction to finalize and start the payments
Create a customer resource
Do this step only if you didn’t create a customer
resource so far on the client side.
POST: https://api.unzer.com/v1/customers
{
"birthDate": "1974-10-03",
"customerId": "info@unzer.com",
"email": "info@unzer.com",
"firstname": "Max",
"lastname": "Mustermann",
"mobile": "+49123456879",
"phone": "+49123456789",
"salutation": "mr",
"billingAddress": {
"city": "Berlin",
"country": "DE",
"name": "Max Musterfrau",
"street": "Schöneberger Str. 21a",
"zip": "10963"
},
"shippingAddress": {
"city": "Berlin",
"country": "DE",
"name": "Max Musterfrau",
"street": "Schöneberger Str. 21a",
"zip": "10963",
"shippingType": "equals-billing"
}
}
$unzer = new Unzer("s-priv-xxxxxxxxxxx");
$address = (new Address())
->setName('Max Mustermann')
->setStreet('Schöneberger Str. 21a')
->setZip('10963')
->setCity('Berlin')
->setCountry('DE');
$customer = (new Customer())
->setFirstname('Max')
->setLastname('Mustermann')
->setSalutation(Salutations::MR)
->setCompany('Unzer GmbH')
->setBirthDate('1972-12-24')
->setEmail('Max.Mustermann@unzer.com')
->setMobile('+49 123456789')
->setPhone('+49 123456789')
->setBillingAddress($address)
->setShippingAddress($address);
$unzer->createCustomer($customer);
Unzer unzer = new Unzer("s-priv-xxxxxxxxxxx");
Address address = new Address();
address
.setName("Max Mustermann")
.setStreet("Schöneberger Str. 21a")
.setCity("Berlin")
.setZip("10963")
.setCountry("DE");
Customer customer = new Customer("Max", "Mustermann");
customer
.setCustomerId(customerId)
.setSalutation(Salutation.mr)
.setEmail("max.mustermann@unzer.com")
.setMobile("+49123456789")
.setBirthDate(getDate("12.12.2000"))
.setBillingAddress(address)
.setShippingAddress(address);
customer = unzer.createCustomer(customer);
The response looks similar to the following example:
{
"id":"s-cst-23e165524229"
}
The examples show B2C customer creation. For a full description of customer
resource for example, updating and handling B2B customers, For a full description of customer
resource, refer to the relevant server-side integration documentation page: Manage customer (direct API calls), Manage customer (PHP SDK), Manage customer (Java SDK).
Create a basket resource
The basket
resource stores information about the purchased products, used vouchers, shipment costs, and the VAT amounts.
POST: https://api.unzer.com/v1/baskets
{
"amountTotalGross" : 200.00,
"amountTotalDiscount" : 10.00,
"amountTotalVat" : 33.33,
"currencyCode" : "EUR",
"orderId" : "Order-12345",
"note" : "Test Basket",
"basketItems" : [ {
"basketItemReferenceId" : "Item-d030efbd4963",
"unit" : "m",
"quantity" : 10,
"amountDiscount" : 10.00,
"vat" : 0.2,
"amountGross" : 200.00,
"amountVat" : 33.33,
"amountPerUnit" : 16.667,
"amountNet" : 166.67,
"title" : "SDM 6 CABLE",
"subTitle" : "This is brand new Mid 2019 version",
"imageUrl" : "https://static.unzer.com/paypage/static/media/unzer-logo.ee4b3a61.svg",
"type": "goods"
} ]
}
$unzer = new Unzer("s-priv-xxxxxxxxxxx");
$basketItem = (new BasketItem())
->setBasketItemReferenceId('Item-d030efbd4963')
->setQuantity(10)
->setUnit('m')
->setAmountPerUnit(16.667)
->setAmountNet(166.67)
->setAmountDiscount(10.0)
->setVat(0.2)
->setAmountGross(200.0)
->setAmountVat(33.33)
->setTitle('SDM 6 CABLE')
->setSubTitle('This is brand new Mid 2019 version')
->setImageUrl('https://a.storyblok.com/f/91629/x/1ba8deb8cc/unzer_primarylogo__white_rgb.svg')
->setType(BasketItemTypes::GOODS);
$basket = (new Basket())
->setAmountTotalGross(200.00)
->setCurrencyCode('EUR')
->setOrderId('Order-12345')
->setNote('Test Basket')
->addBasketItem($basketItem);
$unzer->createBasket($basket);
Unzer unzer = new Unzer("s-priv-xxxxxxxxxx");
BasketItem basketItem = new BasketItem();
basketItem.setBasketItemReferenceId("Item-d030efbd4963");
basketItem.setUnit("m");
basketItem.setQuantity(10);
basketItem.setAmountDiscount(new BigDecimal("10.0"));
basketItem.setVat(new BigDecimal("0.2"));
basketItem.setAmountGross(new BigDecimal("200.00"));
basketItem.setAmountVat(new BigDecimal("33.33"));
basketItem.setAmountPerUnit(new BigDecimal("16.667"));
basketItem.setAmountNet(new BigDecimal("166.67"));
basketItem.setTitle("SDM 6 CABLE");
basketItem.setSubTitle("This is brand new Mid 2019 version");
basketItem.setImageUrl("https://a.storyblok.com/f/91629/x/1ba8deb8cc/unzer_primarylogo__white_rgb.svg");
basketItem.setType("goods");
Basket basket = new Basket();
basket.setAmountTotalGross(new BigDecimal("200.00"));
basket.setAmountTotalDiscount(new BigDecimal("10.00"));
basket.setAmountTotalVat(new BigDecimal("33.33"));
basket.setCurrencyCode(Currency.getInstance("EUR"));
basket.addBasketItem(basketItem);
basket.setOrderId("Order-12345");
basket.setNode("Test Basket");
Basket basket = unzer.createBasket(basket);
For a full description of basket
resource, refer to the relevant server-side-integration documentation page: Manage basket (direct API calls), Manage basket (PHP SDK) , Manage basket (Java SDK).
Make an authorize transaction
Now, perform an authorize
transaction using the InstallmentSecured
resource that you created.
POST https://api.unzer.com/v1/payments/authorize
{
"amount": 100.0,
"currency": "EUR",
"orderId": "Your Order Id",
"paymentReference": "Test authorize transaction",
"resources": {
"basketId": "s-bsk-xxxxxxxxxx",
"customerId": "s-cst-xxxxxxxxxx",
"typeId": "s-ins-upbgliswcvry"
},
"returnUrl": "https://url.of-your-return-controller.de"
}
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$authorization = new Authorization(100.00, 'EUR', $returnUrl);
$authorization->setOrderId('Your Order Id')
$unzer->performAuthorization($authorization, 's-ins-xxxxxxxxxxx', 's-cst-xxxxxxxxxx', null, 's-bsk-xxxxxxxxxx');
Unzer unzer = new Unzer("s-priv-xxxxxxxxxxx");
Authorize charge = unzer.authorize(
100.00,
Currency.getInstance("EUR"),
's-ins-xxxxxxxxxxx',
new URL("https://url.of-your-return-controller.de"),
"s-cst-xxxxxxxxxxx",
"s-bsk-xxxxxxxxxxx"
);
The response looks similar to the following example:
For a full description of the authorize
transaction, please refer to relevant server-side integration documentation page: authorize a payment (direct API calls), Authorize a payment (PHP SDK), Authorize a payment (Java SDK).
Display the chosen installment plan
After the authorize
transaction is successful, you have to display the information of the chosen installment plan to the customer and display a download link of the PDF supplied in the response. If there was an error during the authorize
transaction, display the corresponding customerMessage
in the frontend instead.
You should also display a button which places the order and triggers the Charge
transaction when clicked.
<div>
<div>
Total Purchase Amount: <!-- provide the total purchase amount and currency from your Backend integration-->
</div>
<div>
Total Interest Amount: <!-- provide the total interest amount and currency from your Backend integration-->
</div>
<div>
Total Amount: <!-- provide the total amount and currency from your Backend integration-->
</div>
</div>
<div>
<strong>Please download your rate plan <a href="<!-- provide the PDF Link from your Backend integration-->">here</a></strong><br/>
</div>
<div id="place_order" class="ui bottom attached primary button" tabindex="0" onclick="<!--trigger a charge transaction in your server-side integration-->">Place order</div>
Make a charge transaction
Now, make a charge
transaction with the InstallmentSecured
resource that you created. With a successful charge
transaction, the amount has been prepared to be insured, and a payment
resource is created.
POST: https://api.unzer.com/v1/payments/{payment_ID}/charges
{
"amount": 100.0,
"currency": "EUR",
"orderId": "Your Order Id",
"paymentReference": "Test charge transaction",
"resources": {
"basketId": "s-bsk-xxxxxxxxxx",
"customerId": "s-cst-xxxxxxxxxx",
"typeId": "s-ins-xxxxxxxxxx"
},
"returnUrl": "https://url.of-your-return-controller.de"
}
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$chargeInstance = new Charge(100.00, 'EUR', $returnUrl);
$chargeInstance->setOrderId('Your Order Id')
->setPaymentReference('Test charge transaction');
$transaction = $unzer->performCharge($chargeInstance,'s-ins-xxxxxxxxxxx', 's-cst-xxxxxxxxxx', null, 's-bsk-xxxxxxxxxx');
Unzer unzer = new Unzer("s-priv-xxxxxxxxxxx");
Charge charge = unzer.charge(
100.00,
Currency.getInstance("EUR"),
's-ins-xxxxxxxxxxx',
new URL("https://url.of-your-return-controller.de"),
"s-cst-xxxxxxxxxxx",
"s-bsk-xxxxxxxxxxx"
);
The response looks similar to the following example:
{
"id": "s-chg-1",
"isSuccess": true,
"isPending": false,
"isError": false,
"message": {
"code": "OK"
},
"amount": "119.0000",
"currency": "EUR",
"date": "2021-06-15 08:24:07",
"resources": {
"customerId": "s-cst-33c5b9bef5a7",
"paymentId": "s-pay-174811",
"basketId": "s-bsk-41794",
"metadataId": "",
"payPageId": "",
"traceId": "23dc97dac8f55b870c93048d251af12f",
"typeId": "s-ins-fiqgzhw6j2su"
},
"paymentReference": "",
"processing": {
"externalOrderId": "8b9718f3-6b5d-40b7-9376-79bf74818cbe",
"zgReferenceId": "4702417365603530003141093",
"uniqueId": "31HA07BC811A66C29D3E9AE7ED47A055",
"shortId": "4876.7184.8553",
"traceId": "23dc97dac8f55b870c93048d251af12f"
}
}
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 paymentserver side
Step 3: Check status of the payment [server side]Once the Payment
is made, you can fetch the Payment
details from the Unzer API to handle the Payment
according to its status.
GET https://api.unzer.com/v1/payments/{payment_ID}
{
"id": "s-pay-1762",
"state": {
"id": 0,
"name": "pending"
},
"amount": {
"total": "119.0000",
"charged": "119.0000",
"canceled": "0.0000",
"remaining": "0.0000"
},
"currency": "EUR",
"orderId": "o932271001622632368",
"invoiceId": "",
"resources": {
"customerId": "s-cst-9d5f19fa17cc",
"paymentId": "s-pay-1762",
"basketId": "s-bsk-1582",
"metadataId": "",
"payPageId": "",
"traceId": "059e6c3d3e1174bf53905f9546ef22f2",
"typeId": "s-ins-u9hir9xkiliz"
},
"transactions": [
{
"date": "2021-06-02 11:12:49",
"type": "authorize",
"status": "success",
"url": "https://api.unzer.com/v1/payments/s-pay-1762/authorize/s-aut-1",
"amount": "119.0000"
},
{
"date": "2021-06-02 11:13:00",
"type": "charge",
"status": "success",
"url": "https://api.unzer.com/v1/payments/s-pay-1762/charges/s-chg-1",
"amount": "119.0000"
}
]
}
Step 4: Display the payment resultclient side
Step 4: Display the payment result [client side]Use the information from the Check status of the payment step to display the payment result to your customer.
This can be the success or error page of your shop. If something went wrong, you can use the client message from the API response and show it to the customer.
Manage paymentserver side
For more details on managing Unzer Installment payments, such as refunding them, see Manage Unzer Installment payments.
Notifications
NotificationsWe 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
Error handlingAll 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
Test & go liveYou 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.