/** * Activates a Klarna order for Rest API * * @since 2.0 **/ function activate_order_rest($orderid) { $order = wc_get_order($orderid); $klarna_settings = get_option('woocommerce_klarna_checkout_settings'); $billing_country = get_post_meta($orderid, '_billing_country', true); /** * 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(); // Capture full order amount on WooCommerce order completion $data = array('captured_amount' => $k_order['order_amount'], 'description' => __('WooCommerce order marked complete', 'woocommerce-gateway-klarna'), 'order_lines' => $k_order['order_lines']); try { $k_order->createCapture($data); $k_order->fetch(); $order->add_order_note(sprintf(__('Klarna order captured. Invoice number %s.', 'woocommerce-gateway-klarna'), $k_order['captures'][0]['capture_id'])); update_post_meta($orderid, '_klarna_order_activated', time()); update_post_meta($orderid, '_klarna_invoice_number', $k_order['captures'][0]['capture_id']); update_post_meta($orderid, '_transaction_id', $k_order['captures'][0]['capture_id']); } catch (Exception $e) { $order->add_order_note(sprintf(__('Klarna order activation failed. Error code %s. Error message %s', 'woocommerce-gateway-klarna'), $e->getCode(), utf8_encode($e->getMessage()))); } }