Cancel order

This tutorial will show you how to cancel an order you have created with Klarna.

Note: This flow is is an order cancellation, and not an order refund. This functionality can only be used for orders which have not been activated yet.

Use case

A consumer contacts your customer care and asks to cancel an order, or you realized that the item that was ordered went out of stock and cannot be shipped.

Prerequisites


1. Initialize the Klarna object

Each Klarna library function requires a basic setup of the Klarna object. This configuration specifies how the library communicates with Klarna’s servers.

Follow the code examples below to see how to do this with our different libraries:

use Klarna\XMLRPC\Klarna;
use Klarna\XMLRPC\Country;
use Klarna\XMLRPC\Language;
use Klarna\XMLRPC\Currency;

$k = new Klarna();

$k->config(
    0,              // Merchant ID
    'sharedSecret', // Shared secret
    Country::SE,    // Purchase country
    Language::SV,   // Purchase language
    Currency::SEK,  // Purchase currency
    Klarna::BETA,   // Server
);
Klarna k = new Klarna(
        0,
        "sharedSecret",
        KlarnaCountry.SE,
        KlarnaCurrency.SEK,
        new JsonStorage("/srv/data/pclasses.json"),
        Klarna.BETA
);
config = klarna.Config(
    eid=0,
    secret='sharedSecret',
    country='DE',
    language='DE',
    currency='EUR',
    mode='beta',
    pcstorage='json',
    pcuri='/srv/pclasses.json',
    scheme='https',
    candice=True)

k = klarna.Klarna(config)
k.init()
Configuration configuration = new Configuration(
    Country.Code.SE, Language.Code.SV, Currency.Code.SEK,
    Encoding.Sweden)
{
    Eid = 0,
    Secret = "sharedsecret",
    IsLiveMode = false
};

Api api = new Api(configuration);
Dim kAPI
Set kAPI = GetKlarna(0, "sharedsecret", "SE", "SV", "SEK")
kAPI.SetPort(HTTPS_PORT) ' or HTTP_PORT
kAPI.SetHost(BETA_HOST) ' or LIVE_HOST
kAPI.SetPClassesStorageUri(Server.MapPath("pclasses.xml"))

2. Make the API call

The cancelOrder call requires you to include the reservation number. Once the data is set, initiate the API call to Klarna:

$k->cancelReservation('2347667190');
k.cancelReservation("123456789");
k.cancel_reservation("123456789")
string reservationNumber = "123456";
Boolean canceled = api.CancelReservation(reservationNumber);
Call kAPI.CancelReservation("123456")

The XML-RPC will respond with "ok" as a string, however our libraries will translate this into a boolean return value. In case of an error, an exception containing an error code and message will be thrown.


3. Cancel the order in your system

After receiving the response from Klarna, cancel the order in your system.