Accept Unzer Invoice with UI components
Use Unzer UI component to add Unzer Invoice 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.
Although Unzer API doesn’t require any customer data for this payment method, you should ask for the customer’s details during the checkout so that you can create the invoice document.
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
Implement the payment formBecause 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
To create a Payment Type resource – an Invoice
instance, call the unzerInstance.Invoice()
method:
let invoice = unzerInstance.Invoice();
Add an event listener and submit the form
Get the HTML form by its unique ID and add an event listener.
Inside, create a Promise
on the Invoice
object. The Promise
gets either resolved or rejected:
let form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
invoice.createResource()
.then(function(result) {
let paymentTypeId = 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'
})
});
Step 2: Make a paymentserver side
Make a Charge transaction
Make a charge
transaction with the Invoice
resource that you created. With a successful charge
transaction the amount has been reserved 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
{
"amount": 100.0,
"currency": "EUR",
"resources": {
"typeId": "s-ivc-xxxxxxxxxxx"
}
}
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$charge = new Charge(100.00, 'EUR', $returnUrl);
$typeId = 's-ivc-xxxxxxxxxxx';
$unzer->performCharge($charge, $typeId);
Unzer unzer = new Unzer("s-priv-xxxxxxxxxx");
Charge charge = unzer.charge(100.00, Currency.getInstance("EUR"), 's-ivc-xxxxxxxxxxx');
The response looks similar to the following example:
{
"id": "s-chg-1",
"isSuccess": true,
"isPending": false,
"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://www.unzer.com",
"date": "2018-09-26 13:31:12",
"invoiceId": "any-iv-id",
"resources": {
"paymentId": "s-pay-7785",
"typeId": "s-ivc-xxxxxxxxxxx"
},
"processing": {
"iban": "DExxxxxxxxxxx",
"bic": "COBADEFFXXX",
"uniqueId": "31HA07BC8137E7B23DE93994780BD507",
"shortId": "4018.9507.2850",
"descriptor": "4018.9507.2850",
"holder": "Merchant"
}
}
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.
Unlike most of the other payment types the initial Charge
will always be on status pending.
GET https://api.unzer.com/v1/payments/{payment_ID}
{
"id": "s-pay-172422",
"state": {
"id": 0,
"name": "pending"
},
"amount": {
"total": "1.0000",
"charged": "0.0000",
"canceled": "0.0000",
"remaining": "1.0000"
},
"currency": "EUR",
"orderId": "",
"invoiceId": "",
"resources": {
"customerId": "",
"paymentId": "s-pay-172422",
"basketId": "",
"metadataId": "",
"payPageId": "",
"traceId": "b158b315a531f7e16096db237d03c71c",
"typeId": "s-ivc-alshpwtoaae6"
},
"transactions": [
{
"date": "2021-06-02 10:04:10",
"type": "charge",
"status": "pending",
"url": "https://api.unzer.com/v1/payments/s-pay-172422/charges/s-chg-1",
"amount": "1.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.
It 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 paymentserver side
For more details on managing Unzer invoice payments, see Manage Unzer invoice 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.