Update an order

This tutorial is based on the update example file found in Klarna’s client libraries. You must specify the shared secret that you received when you applied for API credentials.

 

 

1. Retrieve the order

Use the checkout order ID stored in the session to fetch the order from Klarna.

$merchantId = 0;
$sharedSecret = 'sharedSecret';
$orderId = '12345';

$connector = \Klarna\Rest\Transport\Connector::create(
    $merchantId,
    $sharedSecret,
    \Klarna\Rest\Transport\ConnectorInterface::EU_TEST_BASE_URL
);

$checkout = new \Klarna\Rest\Checkout\Order($connector, $orderId);

$checkout->fetch();
Client client = DefaultClient.newInstance(
    "merchantId", 
    "sharedSecret", 
    Client.EU_TEST_BASE_URL)

CheckoutOrder checkout = client.newCheckoutOrder("12345");
CheckoutOrderData orderData = checkout.fetch();
IConnector connector = ConnectorFactory.Create(
    "merchantId", 
    "sharedSecret", 
    Client.EuTestBaseUrl);

Client client = new Client(connector);

ICheckoutOrder checkout = client.NewCheckoutOrder("123456");
CheckoutOrderData orderData = checkout.Fetch();

 

2. Update the order

You should now update the checkout order to reflect the changes that the consumer has made to the cart. If you use the example code found in our client libraries, it will update the order with a charge for "express delivery".

$checkout->update([
    "order_amount" => 11000,
    "order_tax_amount" => 2200,
    "order_lines" => [
        [
            "type" => "physical",
            "reference" => "123050",
            "name" => "Tomatoes",
            "quantity" => 10,
            "quantity_unit" => "kg",
            "unit_price" => 600,
            "tax_rate" => 2500,
            "total_amount" => 6000,
            "total_tax_amount" => 1200
        ],
        [
            "type" => "physical",
            "reference" => "543670",
            "name" => "Bananas",
            "quantity" => 1,
            "quantity_unit" => "bag",
            "unit_price" => 5000,
            "tax_rate" => 2500,
            "total_amount" => 4000,
            "total_discount_amount" => 1000,
            "total_tax_amount" => 800
        ],
        [
            "type" => "shipping_fee",
            "name" => "Express delivery",
            "quantity" => 1,
            "unit_price" => 1000,
            "tax_rate" => 2500,
            "total_amount" => 1000,
            "total_tax_amount" => 200
        ]
    ]
]);
final List<OrderLine> lines = new ArrayList<OrderLine>() {
    {
        add(new OrderLine()
                .setType("physical")
                .setReference("123050")
                .setName("Tomatoes")
                .setQuantity(10L)
                .setQuantityUnit("kg")
                .setUnitPrice(600L)
                .setTaxRate(2500)
                .setTotalAmount(6000L)
                .setTotalTaxAmount(1200L));
        add(new OrderLine()
                .setType("physical")
                .setReference("543670")
                .setName("Bananas")
                .setQuantity(1L)
                .setQuantityUnit("bag")
                .setUnitPrice(5000L)
                .setTaxRate(2500)
                .setTotalAmount(4000L)
                .setTotalDiscountAmount(1000L)
                .setTotalTaxAmount(800L));
        add(new OrderLine()
                .setType("shipping_fee")
                .setName("Express delivery")
                .setQuantity(1L)
                .setUnitPrice(1000L)
                .setTaxRate(2500)
                .setTotalAmount(1000L)
                .setTotalTaxAmount(200L));
    }
};

CheckoutOrderData data = new CheckoutOrderData()
{
    {
        setOrderLines(lines);
        setOrderAmount(11000L);
        setOrderTaxAmount(2200L);
    }
};


data = checkout.update(data);
List<OrderLine> lines = new List<OrderLine>();

lines.Add(new OrderLine()
{
    Type = "physical",
    Reference = "123050",
    Name = "Tomatoes",
    Quantity = 10,
    QuantityUnit = "kg",
    UnitPrice = 600,
    TaxRate = 2500,
    TotalAmount = 6000,
    TotalTaxAmount = 1200
});

lines.Add(new OrderLine()
{
    Type = "physical",
    Reference = "543670",
    Name = "Bananas",
    Quantity = 1,
    QuantityUnit = "bag",
    UnitPrice = 5000,
    TaxRate = 2500,
    TotalAmount = 4000,
    TotalDiscountAmount = 1000,
    TotalTaxAmount = 800
});

lines.Add(new OrderLine()
{
    Type = "shipping_fee",
    Name = "Express delivery",
    Quantity = 1,
    UnitPrice = 1000,
    TaxRate = 2500,
    TotalAmount = 1000,
    TotalTaxAmount = 200
});

CheckoutOrderData orderData = new CheckoutOrderData();
orderData.OrderLines = lines;
orderData.OrderAmount = 11000;
orderData.OrderTaxAmount = 2200;
orderData = checkout.Update(orderData);

 

 


Update an ongoing order

If you want to update an order while the customer is on the checkout page you need to suspend the checkout, update the order, and then resume the checkout to make sure the information showed in the checkout is up to date.
The flow is visualized in the below chart:

 

 

1. Suspend the checkout

Suspending the checkout puts it in a waiting state, preventing consumer input. A loading indicator is rendered on top of the iframe indicating to the consumer that the checkout is loading.

You will do this by issuing a Javascript command, as you can see in the code example below:

 

window._klarnaCheckout(function (api) {
    api.suspend();
});

 

2. Update the order

Update the order as you did in the example above.

 

3. Resume the checkout

Resuming the checkout forces it to fetch the most recent order data from Klarna. The loading indicator will disappear and the consumer can continue to fill in the form. The amount presented in the checkout will now reflect the new order total.

window._klarnaCheckout(function (api) {
    api.resume();
});