/**
  * Updates a Klarna order for Rest API
  *
  * @since  2.0
  **/
 function update_order_rest($orderid, $itemid = false)
 {
     $order = wc_get_order($orderid);
     $klarna_settings = get_option('woocommerce_klarna_checkout_settings');
     $billing_country = get_post_meta($orderid, '_billing_country', true);
     $updated_order_lines = array();
     $updated_order_total = 0;
     $updated_tax_total = 0;
     foreach ($order->get_items() as $item_key => $order_item) {
         if ($order_item['qty'] && isset($itemid) && $item_key != $itemid) {
             $_product = wc_get_product($order_item['product_id']);
             $item_name = $order_item['name'];
             // Append item meta to the title, if it exists
             if (isset($order_item['item_meta'])) {
                 $item_meta = new WC_Order_Item_Meta($order_item['item_meta']);
                 if ($meta = $item_meta->display(true, true)) {
                     $item_name .= ' (' . $meta . ')';
                 }
             }
             $item_reference = strval($order_item['product_id']);
             $item_price = round(number_format(($order_item['line_subtotal'] + $order_item['line_subtotal_tax']) * 100, 0, '', '') / $order_item['qty']);
             $item_quantity = (int) $order_item['qty'];
             $item_total_amount = round(($order_item['line_total'] + $order_item['line_tax']) * 100);
             if ($order_item['line_subtotal'] > $order_item['line_total']) {
                 $item_discount_amount = ($order_item['line_subtotal'] + $order_item['line_subtotal_tax'] - $order_item['line_total'] - $order_item['line_tax']) * 100;
             } else {
                 $item_discount_amount = 0;
             }
             $item_tax_amount = round($order_item['line_tax'] * 100);
             $item_tax_rate = round($order_item['line_subtotal_tax'] / $order_item['line_subtotal'], 2) * 100 * 100;
             $klarna_item = array('reference' => $item_reference, 'name' => $item_name, 'quantity' => $item_quantity, 'unit_price' => $item_price, 'tax_rate' => $item_tax_rate, 'total_amount' => $item_total_amount, 'total_tax_amount' => $item_tax_amount, 'total_discount_amount' => $item_discount_amount);
             $updated_order_lines[] = $klarna_item;
             $updated_order_total = $updated_order_total + $item_total_amount;
             $updated_tax_total = $updated_tax_total + $item_tax_amount;
         }
     }
     /**
      * Need to send local order to constructor and Klarna order to method
      */
     if ($klarna_settings['testmode'] == 'yes') {
         if ('gb' == strtolower($billing_country)) {
             $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_TEST_BASE_URL;
         } elseif ('us' == strtolower($billing_country)) {
             $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_TEST_BASE_URL;
         }
     } else {
         if ('gb' == strtolower($billing_country)) {
             $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_BASE_URL;
         } elseif ('us' == strtolower($billing_country)) {
             $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_BASE_URL;
         }
     }
     if ('gb' == strtolower($billing_country)) {
         $connector = Klarna\Rest\Transport\Connector::create($klarna_settings['eid_uk'], $klarna_settings['secret_uk'], $klarna_server_url);
     } elseif ('us' == strtolower($billing_country)) {
         $connector = Klarna\Rest\Transport\Connector::create($klarna_settings['eid_us'], $klarna_settings['secret_us'], $klarna_server_url);
     }
     $klarna_order_id = get_post_meta($orderid, '_klarna_order_id', true);
     $k_order = new Klarna\Rest\OrderManagement\Order($connector, $klarna_order_id);
     $k_order->fetch();
     try {
         $k_order->updateAuthorization(array('order_amount' => $updated_order_total, 'order_tax_amount' => $updated_tax_total, 'description' => 'Updating WooCommerce order', 'order_lines' => $updated_order_lines));
         $order->add_order_note(sprintf(__('Klarna order updated.', 'woocommerce-gateway-klarna')));
     } catch (Exception $e) {
         $order->add_order_note(sprintf(__('Klarna order update failed. Error code %s. Error message %s', 'woocommerce-gateway-klarna'), $e->getCode(), utf8_encode($e->getMessage())));
     }
 }