Accept Unzer Invoice Secured with UI components
Use Unzer UI component to add Unzer Invoice Secured payment to your checkout page.
Overview
Using UI components for Unzer Invoice 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.
With Unzer Invoice Secured you need to provide information about the customer via customer resource and the purchased products via basket resource when you make the transaction. This is required for customer assessment and transaction approval. You will also need the customer information to create the invoice document. You are responsible for gathering this data during the checkout.
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 Payment form
Option 1: Implement with customer form
If you don’t have information about the customer (and accompanying customerId) or want to update information of already existing customer, you can do so using customer form. Check Customer UI component page for a detailed information.
Insert the customer form
To use the customer form effectively, you must insert it into your payment form. We recommend placing it between the payment elements and the submit button.
Here is a short example:
<form id="payment-form">
<div id="example-invoice-secured"></div>
<div id="customer" class="field">
<!-- The customer form UI element will be inserted here -->
</div>
<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>
Create the Customer resource
A customer resource is created like a payment type with the UI components.
let customer = unzerInstance.Customer();
customer.create({containerId: 'customer'});
However, different steps are taken in case you wish to update an already created Customer:
let customer = unzerInstance.Customer();
customer.update('p-cst-xxxxxxxxxxxxx', {containerId: 'customer'});
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 frontend and just create the resource and use the id in your transaction request. This might not be feasible in case of B2B customer resource creation. Refer to section Create Customer Resource for details on this approach.
In that case you just need to create a simple HTML button on the client side:
<form id="payment-form">
<div id="example-invoice-secured"></div>
<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>
Create a Payment Type resource
To create a payment type resource – an Unzer Invoice Secured instance, call the unzerInstance.InvoiceSecured() method:
let invoiceSecured = unzerInstance.InvoiceSecured();
Add an event listener and submit the form
Option 1: Without customer form
Get the HTML form by its unique ID and add an event listener.
Inside, create a Promise on the invoiceSecured object. The Promise gets either resolved or rejected:
let form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
invoiceSecured.createResource()
.then(function(result) {
// submit the payment type id to your server-side integration
let typeId = result.id;
})
.catch(function(error) {
document.getElementById('error-holder').innerText = error.customerMessage || error.message || 'Error'
})
});
Option 2: With customer form
Get the HTML form by its unique ID and add an event listener.
Create a Promise for the Customer and the invoiceSecured object and handle them with Promise.all().
Please Check Customer form with payment for more information for more information.
let form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
let paymentTypePromise = invoiceSecured.createResource();
let customerPromise = customer.createCustomer();
Promise.all([paymentTypePromise, customerPromise])
.then(function(values) {
// submit the ids to your back end integration
let typeId = values[0];
let customerId = values[1];
})
.catch(function(error) {
document.getElementById('error-holder').innerText = error.customerMessage || error.message || 'Error'
})
});
Step 2: Make a paymentserver side
Besides an always mandatory step of creating the paymentType resource Unzer invoiceSecured requires also a Customer and Basket resource.
Create the customer resource
This step is applicable only if you didn’t create a customer resource so far on the client side.
Crete a B2C customer on the server 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": "Heidelpay",
"country": "DE",
"name": "Max Mustermann",
"street": "Schöneberger Str. 21a",
"zip": "10963"
},
"shippingAddress": {
"city": "Heidelpay",
"country": "DE",
"name": "Max Mustermann",
"street": "Schöneberger Str. 21a",
"zip": "10963",
"shippingType": "equals-billing"
}
}
$unzer = new Unzer('s-priv-xxxxxxxxxx');
$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-xxxxxxxxxx");
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-c552940bca23"
}
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, and the shipment costs.
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-xxxxxxxxxx');
$basketItem = (new BasketItem())
->setBasketItemReferenceId('Item-d030efbd4963')
->setQuantity(10)
->setUnit('m')
->setAmountPerUnitGross(20.00)
->setAmountDiscountPerUnitGross(1.00)
->setVat(19.0)
->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())
->setTotalValueGross(190.00)
->setCurrencyCode('EUR')
->setOrderId('Order-12345')
->setNote('Test Basket')
->addBasketItem($basketItem);
$unzer->createBasket($basket);
BasketItem basketItem = new BasketItem()
.setBasketItemReferenceId("Item-xxxxxxxxxx")
.setQuantity(10)
.setUnit("m")
.setAmountPerUnitGross(BigDecimal.valueOf(20.00))
.setAmountDiscountPerUnitGross(BigDecimal.valueOf(1.00))
.setVat(BigDecimal.valueOf(19.0))
.setTitle("SDM 6 CABLE")
.setSubTitle("This is brand new Mid 2019 version")
.setImageUrl(new URL("https://a.storyblok.com/f/91629/x/1ba8deb8cc/unzer_primarylogo__white_rgb.svg"))
.setType(BasketItem.Type.GOODS);
Basket basket = new Basket()
.setTotalValueGross(BigDecimal.valueOf(190.00))
.setCurrencyCode(Currency.getInstance("EUR"))
.setOrderId("Order-12345")
.setNote("Test Basket")
.addBasketItem(basketItem);
Unzer unzer = new Unzer("s-priv-xxxxxxxxxx");
unzer.createBasket(basket);
The response looks similar to the following example:
{
"id": "s-bsk-1380"
}
For a full description of basket resource, refer to the relevant server-side-integration documentation page: Direct API integration, PHP SDK integration, Java SDK integration
Make a Charge transaction
Now, make a charge transaction with the invoiceSecured resource that you created. With a successful charge transaction the amount has been prepared to be insured and 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
Body:
{
"amount": 100.0,
"currency": "EUR",
"orderId": "Your Order Id",
"paymentReference": "Test charge transaction",
"resources": {
"basketId": "s-bsk-xxxxxxxxxx",
"customerId": "s-cst-xxxxxxxxxx",
"typeId": "s-ivs-upbgliswcvry"
},
"returnUrl": "https://url.of-your-return-controller.de"
}
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$chargeInstance = new Charge(100.00, 'EUR', $returnUrl);
$transaction = $unzer->performCharge(
$chargeInstance,
's-ivs-xxxxxxxxxxx',
's-cst-xxxxxxxxxx',
's-mtd-xxxxxxxxxxx',
's-bsk-xxxxxxxxxx'
);
Charge charge = unzer.charge(
100.00,
Currency.getInstance("EUR"),
's-ivs-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": false,
"isPending": true,
"isError": false,
"message": {
"code": "COR.000.100.112",
"merchant": "Request successfully processed in 'Merchant in Connector Test Mode'",
"customer": "Your payments have been successfully processed in sandbox mode."
},
"amount": "100.0000",
"currency": "EUR",
"returnUrl": "https://url.of-your-return-controller.de",
"date": "2021-06-03 13:44:59",
"resources": {
"customerId": "s-cst-b9b049024bb8",
"paymentId": "s-pay-2111",
"basketId": "s-bsk-1379",
"metadataId": "",
"traceId": "e1bc26c0653c8422d8a44160d8c24d1f",
"typeId": "s-ivs-upbgliswcvry"
},
"orderId": "Your Order Id",
"paymentReference": "Test charge transaction",
"processing": {
"iban": "DE01000000001234567890",
"bic": "HEIDELPAYXY",
"uniqueId": "31HA07BC8184FB9FAC180DEA443A38C1",
"shortId": "4866.5429.1747",
"descriptor": "4866.5429.1747",
"holder": "Merchant Name",
"traceId": "e1bc26c0653c8422d8a44160d8c24d1f"
}
}
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
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.
Charge will always be on status pending.GET: https://api.unzer.com/v1/payments/s-pay-xxxxxxx
$payment = $unzer->fetchPayment('s-pay-xxxxxxxxxx');
Payment payment = unzer.fetchPayment("s-pay-xxxxxxxxxx");
The response looks similar to the following example:
{
"id": "s-pay-xxxxxxx",
"state": {
"id": 0,
"name": "pending"
},
"amount": {
"total": "69.0000",
"charged": "0.0000",
"canceled": "0.0000",
"remaining": "69.0000"
},
"currency": "EUR",
"orderId": "b16227965643249",
"invoiceId": "",
"resources": {
"customerId": "s-cst-0d269aff3552",
"paymentId": "s-pay-172781",
"basketId": "s-bsk-41227",
"metadataId": "",
"payPageId": "",
"traceId": "d271acb67f25789b4da04c99190f2625",
"typeId": "s-ivs-yhgpu0g6xx5c"
},
"transactions": [
{
"date": "2021-06-04 08:49:33",
"type": "charge",
"status": "pending",
"url": "https://api.unzer.com/v1/payments/s-pay-172781/charges/s-chg-1",
"amount": "119.0000"
},
{
"date": "2021-06-04 08:49:34",
"type": "cancel-charge",
"status": "success",
"url": "https://api.unzer.com/v1/payments/s-pay-172781/charges/s-chg-1/cancels/s-cnl-1",
"amount": "50.0000"
}
]
}
Step 4: Display payment resultclient side
After the transaction is made you should display its result to the customer in the front end.
In case of invoice payments you can show the payment information to the customer.
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()
);
Step 5: Activate insurance for Payment
Before you ship the goods to your end customer, you must perform a shipment transaction to Unzer.
Manage paymentserver side
For more details on managing Unzer Invoice Secured payments, see Manage Unzer Invoice Secured 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.
