/** * Pushes Klarna order update in AJAX calls. * * @since 2.0 **/ function ajax_update_klarna_order() { global $woocommerce; // Check if Euro is selected, get correct country if ('EUR' == get_woocommerce_currency() && WC()->session->get('klarna_euro_country')) { $klarna_c = strtolower(WC()->session->get('klarna_euro_country')); $eid = $this->settings["eid_{$klarna_c}"]; $sharedSecret = $this->settings["secret_{$klarna_c}"]; } else { $eid = $this->klarna_eid; $sharedSecret = $this->klarna_secret; } if ($this->is_rest()) { if ($this->testmode == 'yes') { if ('gb' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_TEST_BASE_URL; } elseif ('us' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_TEST_BASE_URL; } } else { if ('gb' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_BASE_URL; } elseif ('us' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_BASE_URL; } } $connector = Klarna\Rest\Transport\Connector::create($eid, $sharedSecret, $klarna_server_url); $klarna_order = new \Klarna\Rest\Checkout\Order($connector, WC()->session->get('klarna_checkout')); } else { $connector = Klarna_Checkout_Connector::create($sharedSecret, $this->klarna_server); $klarna_order = new Klarna_Checkout_Order($connector, WC()->session->get('klarna_checkout')); $klarna_order->fetch(); } // Process cart contents and prepare them for Klarna include_once KLARNA_DIR . 'classes/class-wc-to-klarna.php'; $wc_to_klarna = new WC_Gateway_Klarna_WC2K($this->is_rest(), $this->klarna_country); $cart = $wc_to_klarna->process_cart_contents(); if (0 == count($cart)) { $klarna_order = null; } else { // Reset cart if ($this->is_rest()) { $update['order_lines'] = array(); $klarna_order_total = 0; $klarna_tax_total = 0; foreach ($cart as $item) { $update['order_lines'][] = $item; $klarna_order_total += $item['total_amount']; // Process sales_tax item differently if (array_key_exists('type', $item) && 'sales_tax' == $item['type']) { $klarna_tax_total += $item['total_amount']; } else { $klarna_tax_total += $item['total_tax_amount']; } } $update['order_amount'] = $klarna_order_total; $update['order_tax_amount'] = $klarna_tax_total; } else { $update['cart']['items'] = array(); foreach ($cart as $item) { $update['cart']['items'][] = $item; } } try { $klarna_order->update(apply_filters('kco_update_order', $update)); } catch (Exception $e) { if ($this->debug == 'yes') { $this->log->add('klarna', 'KCO ERROR: ' . var_export($e, true)); error_log('ERROR: ' . var_export($e, true)); } } } }
$result = $woocommerce->cart->check_cart_item_stock(); if (is_wp_error($result)) { return $result->get_error_message(); } // Check if there's anything in the cart if (sizeof($woocommerce->cart->get_cart()) > 0) { // Add button to Standard Checkout Page if this is enabled in the settings if ($this->add_std_checkout_button == 'yes') { echo '<div class="woocommerce"><a href="' . get_permalink(get_option('woocommerce_checkout_page_id')) . '" class="button std-checkout-button">' . $this->std_checkout_button_label . '</a></div>'; } // Get Klarna credentials $eid = $this->klarna_eid; $sharedSecret = $this->klarna_secret; // Process cart contents and prepare them for Klarna include_once KLARNA_DIR . 'classes/class-wc-to-klarna.php'; $wc_to_klarna = new WC_Gateway_Klarna_WC2K($this->is_rest(), $this->klarna_country); $cart = $wc_to_klarna->process_cart_contents(); // Initiate Klarna if ($this->is_rest()) { if ($this->testmode == 'yes') { if ('gb' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_TEST_BASE_URL; } elseif ('us' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_TEST_BASE_URL; } } else { if ('gb' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_BASE_URL; } elseif ('us' == $this->klarna_country) { $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_BASE_URL; }