/** * 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; }
/** * Process the payment and return the result * * @since 1.0.0 **/ function process_payment($order_id) { global $woocommerce; $klarna_gender = null; $order = wc_get_order($order_id); // Get values from klarna form on checkout page // Collect the DoB $klarna_pno = $this->collect_dob(); // Store Klarna specific form values in order as post meta update_post_meta($order_id, 'klarna_pno', $klarna_pno); $klarna_pclass = isset($_POST['klarna_invoice_pclass']) ? woocommerce_clean($_POST['klarna_invoice_pclass']) : ''; $klarna_gender = isset($_POST['klarna_invoice_gender']) ? woocommerce_clean($_POST['klarna_invoice_gender']) : ''; $klarna_de_consent_terms = isset($_POST['klarna_de_consent_terms']) ? woocommerce_clean($_POST['klarna_de_consent_terms']) : ''; // Split address into House number and House extension for NL & DE customers $klarna_billing = array(); $klarna_shipping = array(); if (isset($_POST['billing_country']) && ($_POST['billing_country'] == 'NL' || $_POST['billing_country'] == 'DE')) { require_once KLARNA_DIR . 'split-address.php'; // Set up billing address array $klarna_billing_address = $order->billing_address_1; $splitted_address = splitAddress($klarna_billing_address); $klarna_billing['address'] = $splitted_address[0]; $klarna_billing['house_number'] = $splitted_address[1]; $klarna_billing['house_extension'] = $splitted_address[2]; // Set up shipping address array $klarna_shipping_address = $order->shipping_address_1; $splitted_address = splitAddress($klarna_shipping_address); $klarna_shipping['address'] = $splitted_address[0]; $klarna_shipping['house_number'] = $splitted_address[1]; $klarna_shipping['house_extension'] = $splitted_address[2]; } else { $klarna_billing['address'] = $order->billing_address_1; $klarna_billing['house_number'] = ''; $klarna_billing['house_extension'] = ''; $klarna_shipping['address'] = $order->shipping_address_1; $klarna_shipping['house_number'] = ''; $klarna_shipping['house_extension'] = ''; } $klarna = new Klarna(); /** * Setup Klarna configuration */ $country = $this->klarna_helper->get_klarna_country(); $this->configure_klarna($klarna, $country); $klarna_order = new WC_Gateway_Klarna_Order($order, $klarna); $klarna_order->prepare_order($klarna_billing, $klarna_shipping, $this->ship_to_billing_address); // Set store specific information so you can e.g. search and associate invoices with order numbers. $klarna->setEstoreInfo($orderid1 = ltrim($order->get_order_number(), '#'), $orderid2 = $order_id, $user = ''); try { // Transmit all the specified data, from the steps above, to Klarna. $result = $klarna->reserveAmount($klarna_pno, $klarna_gender, -1, KlarnaFlags::NO_FLAG, $klarna_pclass); // Prepare redirect url $redirect_url = $order->get_checkout_order_received_url(); // Store the selected pclass in the order update_post_meta($order_id, '_klarna_order_pclass', $klarna_pclass); // Retreive response $invno = $result[0]; switch ($result[1]) { case KlarnaFlags::ACCEPTED: $order->add_order_note(__('Klarna payment completed. Klarna Invoice number: ', 'woocommerce-gateway-klarna') . $invno); if ($this->debug == 'yes') { $this->log->add('klarna', __('Klarna payment completed. Klarna Invoice number: ', 'woocommerce-gateway-klarna') . $invno); } update_post_meta($order_id, '_klarna_order_reservation', $invno); update_post_meta($order_id, '_transaction_id', $invno); $order->payment_complete(); // Payment complete $woocommerce->cart->empty_cart(); // Remove cart // Return thank you redirect return array('result' => 'success', 'redirect' => $redirect_url); break; case KlarnaFlags::PENDING: update_post_meta($order_id, '_klarna_order_reservation', $invno); wp_schedule_single_event(time() + 7200, 'check_klarna_pending', array($order_id)); $order->add_order_note(__('Order is PENDING APPROVAL by Klarna. Please visit Klarna Online for the latest status on this order. Klarna reservation number: ', 'woocommerce-gateway-klarna') . $invno); if ($this->debug == 'yes') { $this->log->add('klarna', __('Order is PENDING APPROVAL by Klarna. Please visit Klarna Online for the latest status on this order. Klarna reservation number: ', 'woocommerce-gateway-klarna') . $invno); } $order->update_status('on-hold'); // Change order status to On Hold $woocommerce->cart->empty_cart(); // Remove cart // Return thank you redirect return array('result' => 'success', 'redirect' => $redirect_url); break; case KlarnaFlags::DENIED: // Order is denied, store it in a database. $order->add_order_note(__('Klarna payment denied.', 'woocommerce-gateway-klarna')); if ($this->debug == 'yes') { $this->log->add('klarna', __('Klarna payment denied.', 'woocommerce-gateway-klarna')); } wc_add_notice(__('Klarna payment denied.', 'woocommerce-gateway-klarna'), 'error'); return; break; default: // Unknown response, store it in a database. $order->add_order_note(__('Unknown response from Klarna.', 'woocommerce-gateway-klarna')); if ($this->debug == 'yes') { $this->log->add('klarna', __('Unknown response from Klarna.', 'woocommerce-gateway-klarna')); } wc_add_notice(__('Unknown response from Klarna.', 'woocommerce-gateway-klarna'), 'error'); return; break; } } catch (Exception $e) { // The purchase was denied or something went wrong, print the message: wc_add_notice(sprintf(__('%s (Error code: %s)', 'woocommerce-gateway-klarna'), utf8_encode($e->getMessage()), $e->getCode()), 'error'); if ($this->debug == 'yes') { $this->log->add('klarna', sprintf(__('%s (Error code: %s)', 'woocommerce-gateway-klarna'), utf8_encode($e->getMessage()), $e->getCode())); } return; } }