Confirm purchase
In this tutorial you will learn how to confirm that you have received and created an order in your system. This confirmation is needed in response to a push notification from Klarna, indicating that the consumer has completed a purchase. Once you have received the push notification you should update the status of the order from <em>checkout_complete</em>
to created
.
Use case
A consumer has completed the purchase and you want to create the order in your system.
Prerequisites
- A consumer has completed a purchase with Klarna Checkout.
1. Retrieve the checkout order
Once a consumer has completed a purchase, Klarna will inform you via a POST request. The POST request will be made to the push notification URI you provided when you created the checkout order. Use the checkout order URI found in the query parameter klarna_order
to fetch the order from Klarna.
1 2 3 4 5 6 7 8 9 10 11
IConnector connector = Connector.create( "sharedSecret", IConnector.TEST_BASE_URL); // This is just a placeholder for the example. // For example in jsp you could do // String orderID = (String) request.getParameter("klarna_order_id"); String orderID = "12345"; Order order = new Order(connector, orderID); order.fetch();
1 2 3 4 5 6 7 8 9 10
$sharedSecret = 'sharedSecret'; $orderID = $_SESSION['klarna_order_id']; $connector = Klarna_Checkout_Connector::create( $sharedSecret, Klarna_Checkout_Connector::BASE_TEST_URL ); $order = new Klarna_Checkout_Order($connector, $orderID); $order->fetch();
1 2 3 4 5 6 7 8 9 10
# Instance of the HTTP library that is being used in the server request = {} order_id = request['klarna_order_id'] connector = klarnacheckout.create_connector('shared_secret', klarnacheckout.BASE_TEST_URL) order = klarnacheckout.Order(connector, order_id) order.fetch()
1 2 3 4 5 6 7 8 9 10 11
' Create connector Dim connector : Set connector = CreateConnector("sharedSecret") connector.SetBaseUri KCO_TEST_BASE_URI ' Retrieve order id from query string. Dim orderID : orderID = Request.QueryString("klarna_order_id") Dim order : Set order = CreateOrder(connector) order.ID orderID order.Fetch
1 2 3 4 5 6 7 8 9
var connector = Connector.Create("sharedSecret", Connector.TestBaseUri); // Retrieve order id from query string. // var orderId = Request.QueryString["klarna_order_id"] as string; var orderId = "1245"; var order = new Order(connector, orderId); order.Fetch();
2. Create an order in your system (optional)
You should now create the order in your system with the order data that you want to store. The full resource structure of an order can be found in the API Reference.
Note: You should always make sure that the order status is checkout_complete before you create the order in your system.
3. Update the order status
You must now update the order status to notify Klarna that you have acknowledged the order and created it in your system.
Note: Klarna will send the push notifications every two hours for a total of 48 hours or until you confirm that you have received the order. The pusher works on 2 hour clock intervals and requires the orde to be at least 2 hours old or maximum 48 hours old to be eligible for pushing. So, if an order is only 1.5 hours old when the pusher runs, it will have to wait until next push, 2 hours later.
1 2 3 4 5 6 7 8 9
if (((String) order.get("status")).equals("checkout_complete")) { Map<String, Object> updateData = new HashMap<String, Object>() { { put("status", "created"); } }; order.update(updateData); }
1 2 3 4 5 6 7 8 9
if ($order['status'] == "checkout_complete") { // At this point make sure the order is created in your system and send a // confirmation email to the customer $update = array(); $update['status'] = 'created'; $order->update($update); }
1 2 3 4 5 6 7 8
if order['status'] == 'checkout_complete': # At this point make sure the order is created in your system and send a # confirmation email to the customer update_data = {} update_data['status'] = 'created' order.update(update_data)
1 2 3 4 5 6 7 8 9 10 11 12 13
Dim resourceData Set resourceData = order.Marshal() If resourceData.status = "checkout_complete" Then ' At this point make sure the order is created in your ' system and send a confirmation email to the customer. Dim data Set data = Server.CreateObject("Scripting.Dictionary") data.Add "status", "created" order.Update data End If
1 2 3 4 5 6 7 8 9 10 11 12
if ((string)order.GetValue("status") == "checkout_complete") { // At this point make sure the order is created in your // system and send a confirmation email to the customer. var data = new Dictionary<string, object> { { "status", "created" } }; order.Update(data); }
Full code example
The complete code example used for acknowledging an order can be found in the example section of our client libraries .
Find order in Klarna Online
Direct links to an order in Klarna Online can be created using your estore id (eid) in any of these three ways:
- Using the reservation number (rno):
https://merchants.klarna.com/stores/[eid]/orders/reservation/[rno]
This link will work only for existing reservations, and not for reservations that are already activated - Using the invoice number (invno) or OCR number (ocrno):
https://merchants.klarna.com/stores/[eid]/orders/invoice/[invno/ocrno]
- Using the order id received from the Klarna Checkout API:
https://merchants.klarna.com/stores/[eid]/orders/checkout/[order_id]
What’s next?
You now know how to create and read a checkout order. We encourage you to take a look at the next step test and go live .
You can also go ahead and take a look at manage orders to integrate your backoffice.