alt

Important information

There was an update of Unzer TLS/SSL certificates. Learn more

Unzer

Accept Klarna with server-side-only integration

Build your own payment form to add Klarna to your checkout page

Overview

For Klarna payments, you need to provide information about the customer using the customer resource and the purchased products using the basket resource when you make the transaction. This is required for customer assessment and transaction approval.

Before you begin

Step 1: Create a payment type resource
server side

When creating the payment type klarna, you need to send a request to the Unzer API. The response contains an id, this is later referred to as typeId. You will need this typeId to perform the transaction.

POST https://api.unzer.com/v1/types/klarna

{
}
Currently not supported. 
Currently not supported. 

The response looks similar to the following example:

{
    "id": "s-kla-gixitbjxdm5c",
    "method": "klarna",
    "recurring": false,
    "geoLocation": {
        "clientIp": "143.0.0.1",
        "countryIsoA2": "DE"
    }
}

For a full description of Klarna payment type creation, check the API reference.

Step 2: Make a payment 
server side

To make a payment, you need a customer, a basket, and a paymentType resource.

Make an authorize transaction and forward the customer to the redirect URL for completing the payment. Initially the transaction will be pending and a paymentId referring to this payment is created.

Create the customer resource

This step is only applicable if you didn’t create a customer resource on the client side.

B2C customer creation

For B2C transactions, create a customer as described in the example request. To learn more about the customer resource, see the API reference.

POST https://api.unzer.com/v1/customers
{
  "salutation": "mr",
  "firstname": "Rainer",
  "lastname": "Zufall",  
  "birthDate": "1980-06-22",
  "email": "rainer.zufall@domain.de",
  "language": "de",
  "phone": "+49 152 987654321",
  "billingAddress": {
    "name": "Rainer Zufall",
    "street": "Unzerstraße 18",
    "zip": "22767",
    "city": "Hamburg",
    "country": "DE"
  },
  "shippingAddress": {
    "name": "Rainer Zufall",
    "street": "Unzerstraße 18",
    "zip": "22767",
    "city": "Hamburg",
    "country": "DE"
  }
}

The response looks similar to the following example:

{
    "id": "s-cst-b7e502fcbb10"
}

For a full description of customer resource, refer to the relevant server-side-integration documentation page: Manage customer (direct API calls).

Create a basket resource

The basket resource stores information about the purchased products, used vouchers as well as the shipment costs.

For a full description of baskets resource, refer to the relevant server-side-integration documentation page: Manage baskets (v2).

POST https://api.unzer.com/v2/baskets
{
    "totalValueGross": "105.95",
    "currencyCode": "EUR",
    "basketItems": [
        {
            "type": "goods",
            "basketItemReferenceId": "SKU-1783746",
            "title": "Jacket Fleece size M",
            "quantity": 1,
            "amountPerUnitGross": 100,
            "vat": "19",
        },
        {
            "type": "shipment",
            "basketItemReferenceId": "express-shipping",
            "title": "Shipment fee",
            "quantity": 1,                        
            "amountPerUnitGross": 5.95,
            "vat": "19"
        }
    ]
}
$unzer      = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$basketItem = (new UnzerSDK\Resources\EmbeddedResources\BasketItem())
                  ->setTitle('Notebook pro')
                  ->setSubTitle('basket item subtitle')
                  ->setBasketItemReferenceId('item-1')
                  ->setQuantity(1)
                  ->setTotalValueGross(2000)
                  ->setVat(19.0)
                  ->setType(UnzerSDK\Constants\BasketItemTypes::GOODS)
                  ->setUnit('pc');

$basket  = (new UnzerSDK\Resources\Basket())
    ->setTotalValueGross(2000)
    ->setCurrencyCode('EUR')
    ->setOrderId('O-12345-AB')
    ->setNode('basket note')
    ->addBasketItem($basketItem);

$unzer->createBasket($basket);
Currently not supported

The response looks similar to the following example:

{
    "id": "s-bsk-7e772dd4266b"
}

For a full description of basket resource, refer to the relevant server-side-integration documentation page: Direct API integration.

Make an authorize transaction

Now, make an authorize transaction with the klarna resource that you created earlier. The response contains a redirect URL where the customer must be redirected to complete the payment.

Parameter Type Description
amount (required) float The authorization amount
currency (required) string The authorization currency, in the ISO 4217 alpha-3 format
returnURL (required) string The return URL where the customer must be redirected after a charge transaction.
orderID(required) string This is used to send the order ID from your shop to the API
customerId (required) string The ID of the customers resource to be used
basketId (required) string The basket ID for the payment
typeId (required) string The ID of the payment type resource to be used
additionalTransactionData
termsAndConditionUrl (required) string The link to the terms and conditions URL of your webshop.
privacyPolicyUrl (required) string The link to the privacy policy for your webshop.
POST: https://api.unzer.com/v1/payments/authorize
{
    "amount": "105.95",
    "currency": "EUR",
    "returnUrl": "https://www.merchant.com/returnUrl.php",
    "orderId": "{{random_Order_Id}}",
    "resources": {
        "typeId": "s-kla-gixitbjxdm5c",
        "basketId": "s-bsk-7e772dd4266b",
        "customerId": "s-cst-6c8fe4e63bf9"
    },
    "additionalTransactionData": {
        "termsAndConditionUrl": "https://www.merchant.com/terms.html",
        "privacyPolicyUrl": "https://www.merchant.com/privacy-policy.html"
    }
}
Currently not supported.

The response looks similar to the following example:

GET: https://api.unzer.com/v1/payments/{{random_Order_Id}}/authorize/{{auth-Id}}
{
    "id": "s-aut-1",
    "isSuccess": false,
    "isPending": true,
    "isError": false,
    "redirectUrl": "https://payment.unzer.com/v1/redirect/klarna/s-zlfTD9MuLNEC",
    "message": {
        "code": "COR.000.200.000",
        "merchant": "Transaction pending",
        "customer": "Your payment is currently pending. Please contact us for more information."
    },
    "amount": "105.9500",
    "currency": "EUR",
    "returnUrl": "https://www.unzer.com/de/kassenloesungen/",
    "date": "2022-09-05 12:45:52",
    "resources": {
        "customerId": "s-cst-6c8fe4e63bf9",
        "paymentId": "s-pay-173",
        "basketId": "s-bsk-7e772dd4266b",
        "traceId": "58e301db625ca471f3c8b9b78863ecda",
        "typeId": "s-kla-gixitbjxdm5c"
    },
    "additionalTransactionData": {
        "termsAndConditionUrl": "https://www.unzer.com/de",
        "privacyPolicyUrl": "https://www.unzer.com/de"
    },
    "orderId": "order-1662381951790-517",
    "paymentReference": "",
    "processing": {
        "uniqueId": "cfc69e0e-9ca9-4ec2-99fa-d6de718eef58",
        "shortId": "3493.0801.6897",
        "traceId": "58e301db625ca471f3c8b9b78863ecda"
    }
}

Forward the customer to the external payment page

After authorization, implement the following flow:

  1. Redirect the customer to the redirectUrl returned in the authorize response.
  2. The customer is forwarded to the klarna hosted payment page.
  3. After a successful payment or abort on the klarna page, the customer is redirected to the returnUrl specified in the initial authorize call.

When you ship the goods, it’s important to charge the payment. To learn more about shipment and charge, go to Manage Klarna payments.

Step 3: Check status of the payment
server side

Once the transaction is done, you can fetch the payment details from the API, by using the resources.paymentId from the authorize response above to handle the payment according to its status, such as s-pay-403. Check all possible payment states here.

GET: https://api.unzer.com/v1/payments/s-pay-xxxxxxx
{
    "id": "s-pay-403",
    "state": {
        "id": 0,
        "name": "pending"
    },
    "amount": {
        "total": "105.9500",
        "charged": "0.0000",
        "canceled": "0.0000",
        "remaining": "105.9500"
    },
    "currency": "EUR",
    "orderId": "order-1662381951790-517",
    "invoiceId": "",
    "resources": {
        "customerId": "s-cst-6c8fe4e63bf9",
        "paymentId": "s-pay-173",
        "basketId": "s-bsk-7e772dd4266b",
        "traceId": "58e301db625ca471f3c8b9b78863ecda",
        "typeId": "s-kla-gixitbjxdm5c"
    },
    "transactions": [
        {
            "date": "2022-09-28 11:45:20",
            "type": "authorize",
            "status": "success",
            "url": "https://api.unzer.com/v1/payments/s-pay-403/authorize/s-aut-1",
            "amount": "105.9500",
            "additionalTransactionData": {
                    "termsAndConditionUrl": "https://www.merchant.com/terms.html",
                    "privacyPolicyUrl": "https://www.merchant.com/privacy-policy.html" 
            }
        }
    ]
}
Currently not supported
Currently not supported

Step 4: Display the payment result
client side

Use the information from the Check status of the payment step to show 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 payment
server side

For more details on managing Klarna, see Manage Klarna payments.

Once you ship the order to the customer, make a charge transaction to capture the amount of the goods or services.

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