Beispiel #1
0
 // 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;
         }
     }
     $connector = Klarna\Rest\Transport\Connector::create($eid, $sharedSecret, $klarna_server_url);
 } else {
     $connector = Klarna_Checkout_Connector::create($sharedSecret, $this->klarna_server);
 }
 $klarna_order = null;
 /**
  * Create WooCommerce order
  */
 $orderid = $this->update_or_create_local_order();
 /**
  * Check if Klarna order already exists and if country was changed
  */
 if (WC()->session->get('klarna_checkout') && WC()->session->get('klarna_checkout_country') == WC()->customer->get_country()) {
     include KLARNA_DIR . 'includes/checkout/resume.php';
 }
 // If it doesn't, create Klarna order
 /**
  * Refund order in Klarna system
  *
  * @param  integer $orderid
  * @param  integer $amount
  * @param  string $reason
  *
  * @return bool
  * @since  2.0.0
  */
 public function process_refund($orderid, $amount = null, $reason = '')
 {
     // Check if order was created using this method
     if ($this->id == get_post_meta($orderid, '_payment_method', true)) {
         $order = wc_get_order($orderid);
         if (!$this->can_refund_order($order)) {
             if ($this->debug == 'yes') {
                 $this->log->add('klarna', 'Refund Failed: No Klarna invoice ID.');
             }
             $order->add_order_note(__('This order cannot be refunded. Please make sure it is activated.', 'woocommerce-gateway-klarna'));
             return false;
         }
         if ('v2' == get_post_meta($order->id, '_klarna_api', true)) {
             $country = get_post_meta($orderid, '_billing_country', true);
             $klarna = new Klarna();
             $this->configure_klarna($klarna, $country);
             $invNo = get_post_meta($order->id, '_klarna_invoice_number', true);
             $klarna_order = new WC_Gateway_Klarna_Order($order, $klarna);
             $refund_order = $klarna_order->refund_order($amount, $reason, $invNo);
         } elseif ('rest' == get_post_meta($order->id, '_klarna_api', true)) {
             $country = get_post_meta($orderid, '_billing_country', true);
             /**
              * Need to send local order to constructor and Klarna order to method
              */
             if ($this->testmode == 'yes') {
                 if ('gb' == strtolower($country)) {
                     $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_TEST_BASE_URL;
                 } elseif ('us' == strtolower($country)) {
                     $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_TEST_BASE_URL;
                 }
             } else {
                 if ('gb' == strtolower($country)) {
                     $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::EU_BASE_URL;
                 } elseif ('us' == strtolower($country)) {
                     $klarna_server_url = Klarna\Rest\Transport\ConnectorInterface::NA_BASE_URL;
                 }
             }
             if ('gb' == strtolower($country)) {
                 $connector = Klarna\Rest\Transport\Connector::create($this->eid_uk, $this->secret_uk, $klarna_server_url);
             } elseif ('us' == strtolower($country)) {
                 $connector = Klarna\Rest\Transport\Connector::create($this->eid_us, $this->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();
             $klarna_order = new WC_Gateway_Klarna_Order($order);
             $refund_order = $klarna_order->refund_order_rest($amount, $reason, $k_order);
         }
         if ($refund_order) {
             return true;
         }
     }
     return false;
 }
 /**
  * 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())));
     }
 }