This product, Klarna Payments (kpm) is deprecated. Go to the current version

This section only applies to the regions:
Sweden, Norway

Create an order

This function will allow you to reserve funds with Klarna for a consumer. When this call is executed, Klarna will run fraud and credit check on the consumer and tell you if the purchase is approved. Once the purchase is approved, funds will be captured.

Use case

A consumer continues to the checkout, chooses Klarna as a payment method and clicks “buy”.

Prerequisites

  • You should have already obtained your API credentials.
  • You have initialized the Klarna object
  • You have retrieved the consumer’s address

Add call data

The reserveAmount call, which is used to create an order, requires the mandatory and optional data listed below:

End of regional section
This section only applies to the regions:
Finland, Denmark, Germany, Netherlands, Austria

Create an order

This function will allow you to reserve funds with Klarna for a consumer. When this call is executed, Klarna will run fraud and credit check on the consumer and tell you if the purchase is approved. Once the purchase is approved, funds will be captured.

Use case

A consumer continues to the checkout, chooses Klarna as a payment method and clicks “buy”.

Prerequisites

  • You should have already obtained your API credentials.
  • You should have initialized the Klarna object

Add call data

The reserveAmount call, which is used to create an order, requires the mandatory and optional data listed below:

End of regional section

1. Add cart items

Klarna requires the list of cart items that the consumer purchases in order to approve the purchase. This is facilitated by a helper function within the Klarna library.

1
2
3
4
5
6
7
8
9
10
11
12
Dim flags
flags = INC_VAT + IS_HANDLING ' Handling fee, price including VAT.

' Here we add a normal product to our goods list.
' -Quantity
' -Article number
' -Article name/title
' -Price
' -VAT, 25% VAT
' -Discount
' -Flags, price including VAT
Call kAPI.AddArticle(4, "MG200MMS", "Matrox G200 MMS", 299.99, 25, 0, flags)
1
2
3
4
5
6
7
8
9
api.AddArticle(
    1,                                      // Quantity
    string.Empty,                           // Article Number
    "Handling fee",                         // Product Name
    10.0,                                   // Price
    25.0,                                   // VAT
    0.0,                                    // Discount
    GoodsFlags.Handling | GoodsFlags.IncVAT // Flags (Bitwise OR:ed)
);
1
2
3
4
5
6
7
8
9
10
int flags = Goods.IS_HANDLING.toInt() | Goods.INC_VAT.toInt();
k.addArticle(
    1,              // Quantity
    "HANDLING",     // SKU / Article Number
    "Invoice Fee",  // Title
    15d,            // Price
    25d,            // VAT
    0d,             // Discount
    flags           // Flags
);
1
2
3
4
5
6
7
8
9
10
11
12
use Klarna\XMLRPC\Flags;

$flags = Flags::INC_VAT | Flags::IS_HANDLING;
$k->addArticle(
    4,              // Quantity
    "HANDLING",     // Article number
    "Handling fee", // Article name/title
    50.99,          // Price
    25,             // 25% VAT
    0,              // Discount
    $flags          // Flags
);
1
2
3
4
5
6
7
k.add_article(
    qty=1,
    title="Handling fee",
    price=1.5,
    vat=19,
    discount=0,
    flags=GoodsIs.INC_VAT | GoodsIs.HANDLING)

2. Add Consumer Information

Klarna requires the consumer information in order to approve the purchase. This is done by a helper function in the library.

This section only applies to the regions:
Sweden
1
2
3
4
5
6
7
8
Dim addresses
addresses = kAPI.GetAddresses("410321-9202", null, null)

Dim address
address = addresses(0)

Call kAPI.SetAddress(IS_BILLING, address)
Call kAPI.SetAddress(IS_SHIPPING, address)
1
2
3
4
5
6
7
8
List<Address> addresses = api.GetAddresses("410321-9202");

Address address = addresses[0];

api.ShippingAddress = address;
api.BillingAddress = address;
api.Email = "youremail@email.com";
api.PhoneNumber = "0765260000";
1
2
3
4
5
6
7
8
9
// Client IP must be set
k.setClientIP("192.168.2.10");

List<KlarnaAddr> addresses = k.getAddresses("4103219202");

KlarnaAddr address = addresses.get(0);

k.setAddress(Address.IS_BILLING, address);
k.setAddress(Address.IS_SHIPPING, address);
1
2
3
4
5
6
7
8
use Klarna\XMLRPC\Flags;

$addrs = $k->getAddresses('410321-9202');

$addr = $addrs[0];

$k->setAddress(Flags::IS_BILLING, $addr);
$k->setAddress(Flags::IS_SHIPPING, $addr);
1
2
3
4
5
6
7
8
9
# Client IP must be set
k.clientip = '83.10.0.5'

addrs = k.get_addresses('410321-9202')

addr = addrs[0]

k.shipping = addr
k.billing = addr
End of regional section
This section only applies to the regions:
Norway, Finland, Denmark, Germany, Netherlands, Austria
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Dim address
Set address = new KlarnaAddr

address.setEmail("always_accepted@klarna.se")
address.setTelno("")
address.setCellno("0765260000")
address.setFirstName("Testperson-se")
address.setLastName("Approved")
address.setCompanyName("")
address.setCareof("")
address.setStreet("Stårgatan 1")
address.setZipCode("12345")
address.setCountry(COUNTRY_SE)
address.setCity("Ankeborg")
address.setHouseNumber("") ' House number (DE/NL only)
address.setHouseExt("")    ' House extension (NL only)

Call kAPI.SetAddress(IS_BILLING, address)
Call kAPI.SetAddress(IS_SHIPPING, address)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Address address = new Address()
{
  Email = "pending_approved@klarna.se",
  PhoneNumber = string.Empty,
  CellPhoneNumber = "0765260000",
  FirstName = "Testperson-se",
  LastName = "Approved",
  Street = "Stårgatan 1",
  CareOf = string.Empty,
  ZipCode = "12345",
  City = "Ankeborg",
  Country = Country.Code.SE,
  HouseNumber = string.Empty,     // Needed for DE and NL
  HouseExtension = string.Empty,  // Needed for NL
  CompanyName = string.Empty
};

api.ShippingAddress = address;
api.BillingAddress = address;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
KlarnaAddr address = new KlarnaAddr(
      "always_accepted@klarna.com",   // Email address
      "",                             // Landline phone
      "4676520000",                   // Mobile number
      "Testperson-se",                // First (given) name
      "Approved",                     // Last (family) name
      "",                             // Care of ( c/o )
      "Stårgatan 1",                  // Street
      "12345",                        // Zip code
      "Ankeborg",                     // City
      KlarnaCountry.SE,               // KlarnaCountry constant
      null,                           // House Number (DE/NL only)
      null                            // House Extension (NL only)
);

k.setAddress(Address.IS_BILLING, address);
k.setAddress(Address.IS_SHIPPING, address);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
use Klarna\XMLRPC\Address;
use Klarna\XMLRPC\Country;
use Klarna\XMLRPC\Flags;

$addr = new Address(
    'always_approved@klarna.com', // Email address
    '',                           // Telephone number, only one phone number is needed
    '0762560000',                 // Cell phone number
    'Testperson-se',              // First name (given name)
    'Approved',                   // Last name (family name)
    '',                           // No care of, C/O
    'Storgatan 1',                // Street address
    '12345',                      // Zip code
    'Ankeborg',                   // City
    Country::SE,                  // Country
    null,                         // House number (DE/NL only)
    null                          // House extension (NL only)
);

$k->setAddress(Flags::IS_BILLING, $addr);
$k->setAddress(Flags::IS_SHIPPING, $addr);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
addr = klarna.Address(
    email='always_accepted@klarna.com',
    telno='',
    cellno='015 2211 3356',
    fname='Testperson-de',
    lname='Approved',
    careof='',
    street='Hellersbergstraße',
    zip='14601',
    city='Neuss',
    country='DE',
    house_number='14',     # For DE and NL we need to specify house_number.
    house_extension=None)  # Only required for NL.

k.shipping = addr
k.billing = addr
End of regional section

3. Add optional data

As part of the reserveAmount call, you may provide Klarna with additional optional data such as travel purchases related information or shipment and tracking information. All libraries provide helper methods to facilitate this. Please contact us if you require this functionality.

Make the API call

Once all the data is set, simply initiate the API call to Klarna.

1
2
3
4
5
6
7
8
9
' Transmit all the specified data
' -pno, Date of birth for DE.
' -gender, The customer is a male.
' -amount, -1 will calculate the amount using the internal goods list.
' -flags, No specific behaviour like RETURN_OCR or TEST_MODE.
' -pclass, -1, notes that this is an invoice purchase, for part payment purchase you will have a pclass object on which you use getId().
' -encoding, null to automatically use it for DE.
Dim result
result = kAPI.ReserveAmount("410321-9202", Null, -1, NO_FLAG, PCLASS_INVOICE, null, true)
1
2
3
4
5
6
7
8
9
10
11
12
// It's important to set the end customers IP address
api.ClientIp = "192.0.2.9";

ReservationResponse response = api.ReserveAmount(
    "410321-9202",  // PNO or date of birth
    null,           // Gender. Not needed for SE/NO/DK/FI.
    null,           // Amount, Null to calculate from cart.
    ReservationFlags.NoFlag,
    PClass.Invoice, // PClass ID
    Encoding.Sweden,
    true            // Clear reservation data after sending the call.
);
1
2
3
4
5
6
7
8
9
10
// Client IP must be set
k.setClientIP("192.168.2.10");

String[] result = k.reserveAmount(
        "4103219202",
        null,   // Gender is only needed for DE, AT, and NL
        -1,     // Use cart value to calculate amount to reserve.
        Reservation.NO_FLAG,
        KlarnaPClass.INVOICE
);
1
2
3
4
5
6
7
8
9
10
use Klarna\XMLRPC\Flags;
use Klarna\XMLRPC\PClass;

$result = $k->reserveAmount(
    '4103219202', // PNO (Date of birth for AT/DE/NL)
    null, // KlarnaFlags::MALE, KlarnaFlags::FEMALE (AT/DE/NL only)
    -1,   // Automatically calculate and reserve the cart total amount
    Flags::NO_FLAG,
    PClass::INVOICE
);
1
2
3
4
5
6
7
8
## Set customer IP
k.clientip = '83.10.0.5'

(reservation_number, order_status) = k.reserve_amount(
    '07071960',
    Gender.MALE,
    pclass=klarna.PClass.Type.INVOICE
)

Klarna will respond with an array, containing two variables: rno - which represents the reservation number. This number will be used to Activate the reservation. InvoiceStatus - this represents the status of the invoice and can have 2 values:

1: this response means that the order is approved and can be activated

2: this response means that the order is in pending status, and cannot be activated yet (don’t worry, you’ll learn how to check the status in the order management section).

1
Array("123456789", 2)
1
2
3
4
new ReservationResponse() {
    ReservationNumber = "123",
    OrderStatus = OrderStatus.Accepted
};
1
["123456789", "2"]
1
2
3
array(
    "123456789", 2
);
1
2
3
4
5
6
7
8
## Set customer IP
k.clientip = '83.10.0.5'

(reservation_number, order_status) = k.reserve_amount(
    '07071960',
    Gender.MALE,
    pclass=klarna.PClass.Type.INVOICE
)

In case of an error, an exception containing an error code and message will be thrown.

Success!

You have placed an order with Klarna. In order to continue towards your first live purchase, here are a few sections to check out as the next step