$this->load->model('checkout/order');
$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
if ($order_info) {
$this->language->load('payment/klarna_account');
$this->data['text_information'] = $this->language->get('text_information');
$this->data['text_additional'] = $this->language->get('text_additional');
$this->data['text_payment_option'] = $this->language->get('text_payment_option');
$this->data['text_wait'] = $this->language->get('text_wait');
$this->data['text_day'] = $this->language->get('text_day');
$this->data['text_month'] = $this->language->get('text_month');
$this->data['text_year'] = $this->language->get('text_year');
$this->data['text_male'] = $this->language->get('text_male');
$this->data['text_female'] = $this->language->get('text_female');
$this->data['entry_pno'] = $this->language->get('entry_pno');
$this->data['entry_dob'] = $this->language->get('entry_dob');
$this->data['entry_gender'] = $this->language->get('entry_gender');
$this->data['entry_street'] = $this->language->get('entry_street');
$this->data['entry_house_no'] = $this->language->get('entry_house_no');
$this->data['entry_house_ext'] = $this->language->get('entry_house_ext');
$this->data['entry_phone_no'] = $this->language->get('entry_phone_no');
$this->data['entry_company'] = $this->language->get('entry_company');
$this->data['button_confirm'] = $this->language->get('button_confirm');
$this->data['days'] = array();
for ($i = 1; $i <= 31; $i++) {
$this->data['days'][] = array(
'text' => sprintf('%02d', $i),
'value' => $i
);
}
$this->data['months'] = array();
for ($i = 1; $i <= 12; $i++) {
$this->data['months'][] = array(
'text' => sprintf('%02d', $i),
'value' => $i
);
}
$this->data['years'] = array();
for ($i = date('Y'); $i >= 1900; $i--) {
$this->data['years'][] = array(
'text' => $i,
'value' => $i
);
}
// Store Taxes to send to Klarna
$total_data = array();
$total = 0;
$taxes = $this->cart->getTaxes();
$this->load->model('setting/extension');
$sort_order = array();
$results = $this->model_setting_extension->getExtensions('total');
foreach ($results as $key => $value) {
$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
}
array_multisort($sort_order, SORT_ASC, $results);
$klarna_tax = array();
foreach ($results as $result) {
if ($this->config->get($result['code'] . '_status')) {
$this->load->model('total/' . $result['code']);
$taxes = array();
$this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
$amount = 0;
foreach ($taxes as $tax_id => $value) {
$amount += $value;
}
$klarna_tax[$result['code']] = $amount;
}
}
foreach ($total_data as $key => $value) {
$sort_order[$key] = $value['sort_order'];
if (isset($klarna_tax[$value['code']])) {
if ($klarna_tax[$value['code']]) {
$total_data[$key]['tax_rate'] = abs($klarna_tax[$value['code']] / $value['value'] * 100);
} else {
$total_data[$key]['tax_rate'] = 0;
}
} else {
$total_data[$key]['tax_rate'] = '0';
}
}
$this->session->data['klarna'][$this->session->data['order_id']] = $total_data;
// Order must have identical shipping and billing address or have no shipping address at all
if ($this->cart->hasShipping() && !($order_info['payment_firstname'] == $order_info['shipping_firstname'] && $order_info['payment_lastname'] == $order_info['shipping_lastname'] && $order_info['payment_address_1'] == $order_info['shipping_address_1'] && $order_info['payment_address_2'] == $order_info['shipping_address_2'] && $order_info['payment_postcode'] == $order_info['shipping_postcode'] && $order_info['payment_city'] == $order_info['shipping_city'] && $order_info['payment_zone_id'] == $order_info['shipping_zone_id'] && $order_info['payment_zone_code'] == $order_info['shipping_zone_code'] && $order_info['payment_country_id'] == $order_info['shipping_country_id'] && $order_info['payment_country'] == $order_info['shipping_country'] && $order_info['payment_iso_code_3'] == $order_info['shipping_iso_code_3'])) {
$this->data['error_warning'] = $this->language->get('error_address_match');
} else {
$this->data['error_warning'] = '';
}
// The title stored in the DB gets truncated which causes order_info.tpl to not be displayed properly
$this->db->query("UPDATE `" . DB_PREFIX . "order` SET `payment_method` = '" . $this->db->escape($this->language->get('text_title')) . "' WHERE `order_id` = " . (int)$this->session->data['order_id']);
$klarna_account = $this->config->get('klarna_account');
$this->data['merchant'] = $klarna_account[$order_info['payment_iso_code_3']]['merchant'];
$this->data['phone_number'] = $order_info['telephone'];
$country_to_currency = array(
'NOR' => 'NOK',
'SWE' => 'SEK',
'FIN' => 'EUR',
'DNK' => 'DKK',
'DEU' => 'EUR',
'NLD' => 'EUR',
);
if ($order_info['payment_iso_code_3'] == 'DEU' || $order_info['payment_iso_code_3'] == 'NLD') {
$address = $this->splitAddress($order_info['payment_address_1']);
$this->data['street'] = $address[0];
$this->data['street_number'] = $address[1];
$this->data['street_extension'] = $address[2];
if ($order_info['payment_iso_code_3'] == 'DEU') {
$this->data['street_number'] = trim($address[1] . ' ' . $address[2]);
}
} else {
$this->data['street'] = '';
$this->data['street_number'] = '';
$this->data['street_extension'] = '';
}
$this->data['company'] = $order_info['payment_company'];
$this->data['iso_code_2'] = $order_info['payment_iso_code_2'];
$this->data['iso_code_3'] = $order_info['payment_iso_code_3'];
$payment_option = array();
$total = $this->currency->format($order_info['total'], $country_to_currency[$order_info['payment_iso_code_3']], '', false);
$pclasses = $this->config->get('klarna_account_pclasses');
if (isset($pclasses[$order_info['payment_iso_code_3']])) {
$pclasses = $pclasses[$order_info['payment_iso_code_3']];
} else {
$pclasses = array();
}
foreach ($pclasses as $pclass) {
// 0 - Campaign
// 1 - Account
// 2 - Special
// 3 - Fixed
if (!in_array($pclass['type'], array(0, 1, 3))) {
continue;
}
if ($pclass['type'] == 2) {
$monthly_cost = -1;
} else {
if ($total < $pclass['minamount']) {
continue;
}
if ($pclass['type'] == 3) {
continue;
} else {
$sum = $total;
$lowest_payment = $this->getLowestPaymentAccount($order_info['payment_iso_code_3']);
$monthly_cost = 0;
$monthly_fee = $pclass['invoicefee'];
$start_fee = $pclass['startfee'];
$sum += $start_fee;
$base = ($pclass['type'] == 1);
$minimum_payment = ($pclass['type'] === 1) ? $this->getLowestPaymentAccount($order_info['payment_iso_code_3']) : 0;
if ($pclass['months'] == 0) {
$payment = $sum;
} elseif ($pclass['interestrate'] == 0) {
$payment = $sum / $pclass['months'];
} else {
$interest = $pclass['interestrate'] / (100.0 * 12);
$payment = $sum * $interest / (1 - pow((1 + $interest), -$pclass['months']));
}
$payment += $monthly_fee;
$balance = $sum;
$pay_data = array();
$months = $pclass['months'];
while (($months != 0) && ($balance > 0.01)) {
$interest = $balance * $pclass['interestrate'] / (100.0 * 12);
$new_balance = $balance + $interest + $monthly_fee;
if ($minimum_payment >= $new_balance || $payment >= $new_balance) {
$pay_data[] = $new_balance;
break;
}
$new_payment = max($payment, $minimum_payment);
if ($base) {
$new_payment = max($new_payment, $balance / 24.0 + $monthly_fee + $interest);
}
$balance = $new_balance - $new_payment;
$pay_data[] = $new_payment;
$months -= 1;
}
$monthly_cost = round(isset($pay_data[0]) ? ($pay_data[0]) : 0, 2);
if ($monthly_cost < 0.01) {
continue;
}
if ($pclass['type'] == 1 && $monthly_cost < $lowest_payment) {
$monthly_cost = $lowest_payment;
}
if ($pclass['type'] == 0 && $monthly_cost < $lowest_payment) {
continue;
}
}
}
$payment_option[$pclass['id']]['pclass_id'] = $pclass['id'];
$payment_option[$pclass['id']]['title'] = $pclass['description'];
$payment_option[$pclass['id']]['months'] = $pclass['months'];
$payment_option[$pclass['id']]['monthly_cost'] = $monthly_cost;
}
$sort_order = array();
foreach ($payment_option as $key => $value) {
$sort_order[$key] = $value['pclass_id'];
}
array_multisort($sort_order, SORT_ASC, $payment_option);
$this->data['payment_options'] = array();
foreach ($payment_option as $payment_option) {
$this->data['payment_options'][] = array(
'code' => $payment_option['pclass_id'],
'title' => sprintf($this->language->get('text_monthly_payment'), $payment_option['title'], $this->currency->format($this->currency->convert($payment_option['monthly_cost'], $country_to_currency[$order_info['payment_iso_code_3']], $this->currency->getCode()), 1, 1))
);
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/klarna_account.tpl')) {
$this->template = $this->config->get('config_template') . '/template/payment/klarna_account.tpl';
} else {
$this->template = 'default/template/payment/klarna_account.tpl';
}
$this->render();
}