klarna_checkout.php (34893B)
1 <?php 2 class ControllerExtensionPaymentKlarnaCheckout extends Controller { 3 private $error = array(); 4 5 public function index() { 6 $this->load->language('extension/payment/klarna_checkout'); 7 8 $this->document->setTitle($this->language->get('heading_title')); 9 10 $this->load->model('setting/setting'); 11 12 if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { 13 $this->model_setting_setting->editSetting('payment_klarna_checkout', $this->request->post); 14 15 $this->session->data['success'] = $this->language->get('text_success'); 16 17 $this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true)); 18 } 19 20 $data['user_token'] = $this->session->data['user_token']; 21 22 $this->load->model('localisation/language'); 23 24 $data['languages'] = $this->model_localisation_language->getLanguages(); 25 26 $this->load->model('localisation/country'); 27 28 $data['countries'] = $this->model_localisation_country->getCountries(); 29 30 $this->load->model('localisation/geo_zone'); 31 32 $data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones(); 33 34 $data['api_locations'] = array( 35 array( 36 'name' => 'North America', 37 'code' => 'NA' 38 ), 39 array( 40 'name' => 'Europe', 41 'code' => 'EU' 42 ) 43 ); 44 45 $this->load->model('catalog/information'); 46 47 $data['informations'] = $this->model_catalog_information->getInformations(); 48 49 $this->load->model('localisation/currency'); 50 51 $data['currencies'] = $this->model_localisation_currency->getCurrencies(); 52 53 $this->load->model('localisation/order_status'); 54 55 $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses(); 56 57 if (isset($this->error['warning'])) { 58 $data['error_warning'] = $this->error['warning']; 59 } else { 60 $data['error_warning'] = ''; 61 } 62 63 $this->load->model('extension/payment/klarna_checkout'); 64 65 if ($this->model_extension_payment_klarna_checkout->checkForPaymentTaxes()) { 66 $data['error_tax_warning'] = $this->language->get('error_tax_warning'); 67 } else { 68 $data['error_tax_warning'] = ''; 69 } 70 71 if (isset($this->error['account_warning'])) { 72 $data['error_account_warning'] = $this->error['account_warning']; 73 } else { 74 $data['error_account_warning'] = ''; 75 } 76 77 if (isset($this->error['account'])) { 78 $data['error_account'] = $this->error['account']; 79 } else { 80 $data['error_account'] = array(); 81 } 82 83 if (isset($this->error['settlement_warning'])) { 84 $data['error_settlement_warning'] = $this->error['settlement_warning']; 85 } else { 86 $data['error_settlement_warning'] = ''; 87 } 88 89 $data['breadcrumbs'] = array(); 90 91 $data['breadcrumbs'][] = array( 92 'text' => $this->language->get('text_home'), 93 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true) 94 ); 95 96 $data['breadcrumbs'][] = array( 97 'text' => $this->language->get('text_extension'), 98 'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true) 99 ); 100 101 $data['breadcrumbs'][] = array( 102 'text' => $this->language->get('heading_title'), 103 'href' => $this->url->link('extension/payment/klarna_checkout', 'user_token=' . $this->session->data['user_token'], true) 104 ); 105 106 $data['action'] = $this->url->link('extension/payment/klarna_checkout', 'user_token=' . $this->session->data['user_token'], true); 107 $data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true); 108 109 if (isset($this->request->post['klarna_checkout_debug'])) { 110 $data['klarna_checkout_debug'] = $this->request->post['klarna_checkout_debug']; 111 } else { 112 $data['klarna_checkout_debug'] = $this->config->get('klarna_checkout_debug'); 113 } 114 115 if (isset($this->request->post['klarna_checkout_colour_button'])) { 116 $data['klarna_checkout_colour_button'] = $this->request->post['klarna_checkout_colour_button']; 117 } elseif ($this->config->get('klarna_checkout_colour_button')) { 118 $data['klarna_checkout_colour_button'] = $this->config->get('klarna_checkout_colour_button'); 119 } else { 120 $data['klarna_checkout_colour_button'] = '#0072cc'; 121 } 122 123 if (isset($this->request->post['klarna_checkout_colour_button_text'])) { 124 $data['klarna_checkout_colour_button_text'] = $this->request->post['klarna_checkout_colour_button_text']; 125 } elseif ($this->config->get('klarna_checkout_colour_button_text')) { 126 $data['klarna_checkout_colour_button_text'] = $this->config->get('klarna_checkout_colour_button_text'); 127 } else { 128 $data['klarna_checkout_colour_button_text'] = '#ffffff'; 129 } 130 131 if (isset($this->request->post['klarna_checkout_colour_checkbox'])) { 132 $data['klarna_checkout_colour_checkbox'] = $this->request->post['klarna_checkout_colour_checkbox']; 133 } elseif ($this->config->get('klarna_checkout_colour_checkbox')) { 134 $data['klarna_checkout_colour_checkbox'] = $this->config->get('klarna_checkout_colour_checkbox'); 135 } else { 136 $data['klarna_checkout_colour_checkbox'] = '#0072cc'; 137 } 138 139 if (isset($this->request->post['klarna_checkout_colour_checkbox_checkmark'])) { 140 $data['klarna_checkout_colour_checkbox_checkmark'] = $this->request->post['klarna_checkout_colour_checkbox_checkmark']; 141 } elseif ($this->config->get('klarna_checkout_colour_checkbox_checkmark')) { 142 $data['klarna_checkout_colour_checkbox_checkmark'] = $this->config->get('klarna_checkout_colour_checkbox_checkmark'); 143 } else { 144 $data['klarna_checkout_colour_checkbox_checkmark'] = '#ffffff'; 145 } 146 147 if (isset($this->request->post['klarna_checkout_colour_header'])) { 148 $data['klarna_checkout_colour_header'] = $this->request->post['klarna_checkout_colour_header']; 149 } elseif ($this->config->get('klarna_checkout_colour_header')) { 150 $data['klarna_checkout_colour_header'] = $this->config->get('klarna_checkout_colour_header'); 151 } else { 152 $data['klarna_checkout_colour_header'] = '#434343'; 153 } 154 155 if (isset($this->request->post['klarna_checkout_colour_link'])) { 156 $data['klarna_checkout_colour_link'] = $this->request->post['klarna_checkout_colour_link']; 157 } elseif ($this->config->get('klarna_checkout_colour_link')) { 158 $data['klarna_checkout_colour_link'] = $this->config->get('klarna_checkout_colour_link'); 159 } else { 160 $data['klarna_checkout_colour_link'] = '#0072cc'; 161 } 162 163 if (isset($this->request->post['klarna_checkout_separate_shipping_address'])) { 164 $data['klarna_checkout_separate_shipping_address'] = $this->request->post['klarna_checkout_separate_shipping_address']; 165 } else { 166 $data['klarna_checkout_separate_shipping_address'] = $this->config->get('klarna_checkout_separate_shipping_address'); 167 } 168 169 if (isset($this->request->post['klarna_checkout_dob_mandatory'])) { 170 $data['klarna_checkout_dob_mandatory'] = $this->request->post['klarna_checkout_dob_mandatory']; 171 } else { 172 $data['klarna_checkout_dob_mandatory'] = $this->config->get('klarna_checkout_dob_mandatory'); 173 } 174 175 if (isset($this->request->post['klarna_checkout_title_mandatory'])) { 176 $data['klarna_checkout_title_mandatory'] = $this->request->post['klarna_checkout_title_mandatory']; 177 } else { 178 $data['klarna_checkout_title_mandatory'] = $this->config->get('klarna_checkout_title_mandatory'); 179 } 180 181 if (isset($this->request->post['klarna_checkout_additional_text_box'])) { 182 $data['klarna_checkout_additional_text_box'] = $this->request->post['klarna_checkout_additional_text_box']; 183 } else { 184 $data['klarna_checkout_additional_text_box'] = $this->config->get('klarna_checkout_additional_text_box'); 185 } 186 187 if (isset($this->request->post['klarna_checkout_total'])) { 188 $data['klarna_checkout_total'] = $this->request->post['klarna_checkout_total']; 189 } else { 190 $data['klarna_checkout_total'] = $this->config->get('klarna_checkout_total'); 191 } 192 193 if (isset($this->request->post['klarna_checkout_order_status_authorised_id'])) { 194 $data['klarna_checkout_order_status_authorised_id'] = $this->request->post['klarna_checkout_order_status_authorised_id']; 195 } else { 196 $data['klarna_checkout_order_status_authorised_id'] = $this->config->get('klarna_checkout_order_status_authorised_id'); 197 } 198 199 if (isset($this->request->post['klarna_checkout_order_status_part_captured_id'])) { 200 $data['klarna_checkout_order_status_part_captured_id'] = $this->request->post['klarna_checkout_order_status_part_captured_id']; 201 } else { 202 $data['klarna_checkout_order_status_part_captured_id'] = $this->config->get('klarna_checkout_order_status_part_captured_id'); 203 } 204 205 if (isset($this->request->post['klarna_checkout_order_status_captured_id'])) { 206 $data['klarna_checkout_order_status_captured_id'] = $this->request->post['klarna_checkout_order_status_captured_id']; 207 } else { 208 $data['klarna_checkout_order_status_captured_id'] = $this->config->get('klarna_checkout_order_status_captured_id'); 209 } 210 211 if (isset($this->request->post['klarna_checkout_order_status_cancelled_id'])) { 212 $data['klarna_checkout_order_status_cancelled_id'] = $this->request->post['klarna_checkout_order_status_cancelled_id']; 213 } else { 214 $data['klarna_checkout_order_status_cancelled_id'] = $this->config->get('klarna_checkout_order_status_cancelled_id'); 215 } 216 217 if (isset($this->request->post['klarna_checkout_order_status_refund_id'])) { 218 $data['klarna_checkout_order_status_refund_id'] = $this->request->post['klarna_checkout_order_status_refund_id']; 219 } else { 220 $data['klarna_checkout_order_status_refund_id'] = $this->config->get('klarna_checkout_order_status_refund_id'); 221 } 222 223 if (isset($this->request->post['klarna_checkout_order_status_fraud_rejected_id'])) { 224 $data['klarna_checkout_order_status_fraud_rejected_id'] = $this->request->post['klarna_checkout_order_status_fraud_rejected_id']; 225 } else { 226 $data['klarna_checkout_order_status_fraud_rejected_id'] = $this->config->get('klarna_checkout_order_status_fraud_rejected_id'); 227 } 228 229 if (isset($this->request->post['klarna_checkout_order_status_fraud_pending_id'])) { 230 $data['klarna_checkout_order_status_fraud_pending_id'] = $this->request->post['klarna_checkout_order_status_fraud_pending_id']; 231 } else { 232 $data['klarna_checkout_order_status_fraud_pending_id'] = $this->config->get('klarna_checkout_order_status_fraud_pending_id'); 233 } 234 235 if (isset($this->request->post['klarna_checkout_order_status_fraud_accepted_id'])) { 236 $data['klarna_checkout_order_status_fraud_accepted_id'] = $this->request->post['klarna_checkout_order_status_fraud_accepted_id']; 237 } else { 238 $data['klarna_checkout_order_status_fraud_accepted_id'] = $this->config->get('klarna_checkout_order_status_fraud_accepted_id'); 239 } 240 241 if (isset($this->request->post['klarna_checkout_terms'])) { 242 $data['klarna_checkout_terms'] = $this->request->post['klarna_checkout_terms']; 243 } else { 244 $data['klarna_checkout_terms'] = $this->config->get('klarna_checkout_terms'); 245 } 246 247 if (isset($this->request->post['klarna_checkout_status'])) { 248 $data['klarna_checkout_status'] = $this->request->post['klarna_checkout_status']; 249 } else { 250 $data['klarna_checkout_status'] = $this->config->get('klarna_checkout_status'); 251 } 252 253 if ($this->request->server['REQUEST_METHOD'] == 'POST' && !isset($this->request->post['klarna_checkout_account'])) { 254 $data['klarna_checkout_account'] = array(); 255 } elseif ($this->request->server['REQUEST_METHOD'] == 'POST' && isset($this->request->post['klarna_checkout_account'])) { 256 $data['klarna_checkout_account'] = $this->request->post['klarna_checkout_account']; 257 } elseif ($this->config->get('klarna_checkout_account')) { 258 $data['klarna_checkout_account'] = $this->config->get('klarna_checkout_account'); 259 } else { 260 $data['klarna_checkout_account'] = array(); 261 } 262 263 if (isset($this->request->post['klarna_checkout_sftp_username'])) { 264 $data['klarna_checkout_sftp_username'] = $this->request->post['klarna_checkout_sftp_username']; 265 } else { 266 $data['klarna_checkout_sftp_username'] = $this->config->get('klarna_checkout_sftp_username'); 267 } 268 269 if (isset($this->request->post['klarna_checkout_sftp_password'])) { 270 $data['klarna_checkout_sftp_password'] = $this->request->post['klarna_checkout_sftp_password']; 271 } else { 272 $data['klarna_checkout_sftp_password'] = $this->config->get('klarna_checkout_sftp_password'); 273 } 274 275 if (isset($this->request->post['klarna_checkout_settlement_order_status_id'])) { 276 $data['klarna_checkout_settlement_order_status_id'] = $this->request->post['klarna_checkout_settlement_order_status_id']; 277 } else { 278 $data['klarna_checkout_settlement_order_status_id'] = $this->config->get('klarna_checkout_settlement_order_status_id'); 279 } 280 281 $data['store_url'] = $this->request->server['HTTPS'] ? HTTPS_CATALOG : HTTP_CATALOG; 282 283 // API login 284 $this->load->model('user/api'); 285 286 $api_info = $this->model_user_api->getApi($this->config->get('config_api_id')); 287 288 if ($api_info) { 289 $data['api_id'] = $api_info['api_id']; 290 $data['api_key'] = $api_info['key']; 291 $data['api_ip'] = $this->request->server['REMOTE_ADDR']; 292 } else { 293 $data['api_id'] = ''; 294 $data['api_key'] = ''; 295 $data['api_ip'] = ''; 296 } 297 298 $data['header'] = $this->load->controller('common/header'); 299 $data['column_left'] = $this->load->controller('common/column_left'); 300 $data['footer'] = $this->load->controller('common/footer'); 301 302 $this->response->setOutput($this->load->view('extension/payment/klarna_checkout', $data)); 303 } 304 305 public function order() { 306 $this->load->language('extension/payment/klarna_checkout'); 307 308 $data['user_token'] = $this->session->data['user_token']; 309 310 $data['order_id'] = $this->request->get['order_id']; 311 312 return $this->load->view('extension/payment/klarna_checkout_order', $data); 313 } 314 315 public function getTransaction() { 316 $this->load->language('extension/payment/klarna_checkout'); 317 318 $this->load->model('extension/payment/klarna_checkout'); 319 $this->load->model('sale/order'); 320 321 if (!$this->config->get('klarna_checkout_status') || !isset($this->request->get['order_id'])) { 322 return; 323 } 324 325 $order_reference = $this->model_extension_payment_klarna_checkout->getOrder($this->request->get['order_id']); 326 327 $order_info = $this->model_sale_order->getOrder($this->request->get['order_id']); 328 329 if (!$order_reference || !$order_reference['order_ref'] || !$order_info) { 330 return; 331 } 332 333 list($klarna_account, $connector) = $this->model_extension_payment_klarna_checkout->getConnector($this->config->get('klarna_checkout_account'), $order_info['currency_code']); 334 335 if (!$klarna_account || !$connector) { 336 return; 337 } 338 339 $klarna_order = $this->model_extension_payment_klarna_checkout->omRetrieve($connector, $order_reference['order_ref']); 340 341 if (!$klarna_order) { 342 return; 343 } 344 345 $data['order_ref'] = $order_reference['order_ref']; 346 347 $data['user_token'] = $this->session->data['user_token']; 348 349 $data['order_id'] = $this->request->get['order_id']; 350 351 $data['store_url'] = $this->request->server['HTTPS'] ? HTTPS_CATALOG : HTTP_CATALOG; 352 353 $extend_authorization_action = $cancel_action = $capture_action = $refund_action = $merchant_reference_action = $address_action = $release_authorization_action = false; 354 355 switch (strtoupper($klarna_order['status'])) { 356 case 'AUTHORIZED': 357 $merchant_reference_action = true; 358 $extend_authorization_action = true; 359 $address_action = true; 360 $cancel_action = true; 361 $capture_action = true; 362 break; 363 case 'PART_CAPTURED': 364 $merchant_reference_action = true; 365 $extend_authorization_action = true; 366 $release_authorization_action = true; 367 $address_action = true; 368 $capture_action = true; 369 $refund_action = true; 370 break; 371 case 'CAPTURED': 372 $address_action = true; 373 $merchant_reference_action = true; 374 $refund_action = true; 375 break; 376 case 'CANCELLED': 377 break; 378 case 'EXPIRED': 379 break; 380 case 'CLOSED': 381 break; 382 } 383 384 $format = '{title} {given_name} {family_name}' . "\n" . '{street_address}' . "\n" . '{street_address2}' . "\n" . '{city} {postcode}' . "\n" . '{region}' . "\n" . '{country}' . "\n" . '{email} {phone}'; 385 386 $find = array( 387 '{title}', 388 '{given_name}', 389 '{family_name}', 390 '{street_address}', 391 '{street_address2}', 392 '{city}', 393 '{postcode}', 394 '{region}', 395 '{country}', 396 '{email}', 397 '{phone}', 398 ); 399 400 $replace = array( 401 'title' => $klarna_order['billing_address']['title'], 402 'given_name' => $klarna_order['billing_address']['given_name'], 403 'family_name' => $klarna_order['billing_address']['family_name'], 404 'street_address' => $klarna_order['billing_address']['street_address'], 405 'street_address2' => $klarna_order['billing_address']['street_address2'], 406 'city' => $klarna_order['billing_address']['city'], 407 'postcode' => $klarna_order['billing_address']['postal_code'], 408 'region' => $klarna_order['billing_address']['region'], 409 'country' => $klarna_order['billing_address']['country'], 410 'email' => $klarna_order['billing_address']['email'], 411 'phone' => $klarna_order['billing_address']['phone'] 412 ); 413 414 $billing_address_formatted = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format)))); 415 416 $replace = array( 417 'title' => $klarna_order['shipping_address']['title'], 418 'given_name' => $klarna_order['shipping_address']['given_name'], 419 'family_name' => $klarna_order['shipping_address']['family_name'], 420 'street_address' => $klarna_order['shipping_address']['street_address'], 421 'street_address2' => $klarna_order['shipping_address']['street_address2'], 422 'city' => $klarna_order['shipping_address']['city'], 423 'postcode' => $klarna_order['shipping_address']['postal_code'], 424 'region' => $klarna_order['shipping_address']['region'], 425 'country' => $klarna_order['shipping_address']['country'], 426 'email' => $klarna_order['shipping_address']['email'], 427 'phone' => $klarna_order['shipping_address']['phone'] 428 ); 429 430 $shipping_address_formatted = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format)))); 431 432 $order_lines = array(); 433 434 foreach ($klarna_order['order_lines'] as $order_line) { 435 $order_lines[] = array( 436 'reference' => $order_line['reference'], 437 'type' => $order_line['type'], 438 'quantity' => $order_line['quantity'], 439 'quantity_unit' => $order_line['quantity_unit'], 440 'name' => $order_line['name'], 441 'total_amount' => $this->currency->format($order_line['total_amount'] / 100, $order_info['currency_code'], '1.00000000'), 442 'unit_price' => $this->currency->format($order_line['unit_price'] / 100, $order_info['currency_code'], '1.00000000'), 443 'total_discount_amount' => $this->currency->format($order_line['total_discount_amount'] / 100, $order_info['currency_code'], '1.00000000'), 444 'tax_rate' => ($order_line['tax_rate'] / 100) . '%', 445 'total_tax_amount' => $this->currency->format($order_line['total_tax_amount'] / 100, $order_info['currency_code'], '1.00000000') 446 ); 447 } 448 449 $merchant_id = ''; 450 if ($order_reference['data']) { 451 $klarna_checkout_order_data = json_decode($this->encryption->decrypt($this->config->get('config_encryption'), $order_reference['data']), true); 452 if ($klarna_checkout_order_data && $klarna_checkout_order_data['merchant_id']) { 453 $merchant_id = $klarna_checkout_order_data['merchant_id']; 454 } 455 } 456 457 $data['transaction'] = array( 458 'order_id' => $klarna_order['order_id'], 459 'merchant_id' => $merchant_id, 460 'reference' => $klarna_order['klarna_reference'], 461 'status' => $klarna_order['status'], 462 'fraud_status' => $klarna_order['fraud_status'], 463 'merchant_reference_1' => $klarna_order['merchant_reference1'], 464 'billing_address' => $klarna_order['billing_address'], 465 'shipping_address' => $klarna_order['shipping_address'], 466 'billing_address_formatted' => $billing_address_formatted, 467 'shipping_address_formatted' => $shipping_address_formatted, 468 'order_lines' => $order_lines, 469 'amount' => $this->currency->format($klarna_order['order_amount'] / 100, $order_info['currency_code'], '1.00000000'), 470 'authorization_expiry' => isset($klarna_order['expires_at']) ? date($this->language->get('date_format_short'), strtotime($klarna_order['expires_at'])) : '', 471 'authorization_remaining' => $this->currency->format($klarna_order['remaining_authorized_amount'] / 100, $order_info['currency_code'], '1.00000000'), 472 ); 473 474 $max_capture_amount = $klarna_order['remaining_authorized_amount'] / 100; 475 476 $max_refund_amount = $klarna_order['captured_amount'] / 100; 477 478 $data['captures'] = array(); 479 480 foreach ($klarna_order['captures'] as $capture) { 481 $data['captures'][] = array( 482 'capture_id' => $capture['capture_id'], 483 'shipping_info_title' => sprintf($this->language->get('text_capture_shipping_info_title'), $capture['capture_id']), 484 'billing_address_title' => sprintf($this->language->get('text_capture_billing_address_title'), $capture['capture_id']), 485 'date_added' => date($this->language->get('datetime_format'), strtotime($capture['captured_at'])), 486 'amount' => $this->currency->format($capture['captured_amount'] / 100, $order_info['currency_code'], '1.00000000', true), 487 'reference' => $capture['klarna_reference'], 488 'shipping_info' => $capture['shipping_info'], 489 'billing_address' => $capture['billing_address'], 490 'shipping_address' => $capture['shipping_address'] 491 ); 492 } 493 494 $data['refunds'] = array(); 495 496 foreach ($klarna_order['refunds'] as $capture) { 497 $max_refund_amount -= ($capture['refunded_amount'] / 100); 498 499 $data['refunds'][] = array( 500 'date_added' => date($this->language->get('datetime_format'), strtotime($capture['refunded_at'])), 501 'amount' => $this->currency->format($capture['refunded_amount'] / 100, $order_info['currency_code'], '1.00000000', true) 502 ); 503 } 504 505 if (!$max_capture_amount) { 506 $capture_action = false; 507 } 508 509 if (!$max_refund_amount) { 510 $refund_action = false; 511 } 512 513 $data['allowed_shipping_methods'] = array( 514 'PickUpStore', 515 'Home', 516 'BoxReg', 517 'BoxUnreg', 518 'PickUpPoint', 519 'Own' 520 ); 521 522 $data['extend_authorization_action'] = $extend_authorization_action; 523 $data['cancel_action'] = $cancel_action; 524 $data['capture_action'] = $capture_action; 525 $data['refund_action'] = $refund_action; 526 $data['address_action'] = $address_action; 527 $data['merchant_reference_action'] = $merchant_reference_action; 528 $data['release_authorization_action'] = $release_authorization_action; 529 $data['max_capture_amount'] = $this->currency->format($max_capture_amount, $order_info['currency_code'], '1.00000000', false); 530 $data['max_refund_amount'] = $this->currency->format($max_refund_amount, $order_info['currency_code'], '1.00000000', false); 531 $data['symbol_left'] = $this->currency->getSymbolLeft($order_info['currency_code']); 532 $data['symbol_right'] = $this->currency->getSymbolRight($order_info['currency_code']); 533 534 $this->response->setOutput($this->load->view('extension/payment/klarna_checkout_order_ajax', $data)); 535 } 536 537 public function install() { 538 $this->load->model('extension/payment/klarna_checkout'); 539 $this->model_extension_payment_klarna_checkout->install(); 540 } 541 542 public function uninstall() { 543 $this->load->model('extension/payment/klarna_checkout'); 544 $this->model_extension_payment_klarna_checkout->uninstall(); 545 } 546 547 public function transactionCommand() { 548 $this->load->language('extension/payment/klarna_checkout'); 549 550 $this->load->model('extension/payment/klarna_checkout'); 551 $this->load->model('sale/order'); 552 553 $json = array(); 554 555 $success = $error = ''; 556 557 $order_info = $this->model_sale_order->getOrder($this->request->get['order_id']); 558 559 list($klarna_account, $connector) = $this->model_extension_payment_klarna_checkout->getConnector($this->config->get('klarna_checkout_account'), $order_info['currency_code']); 560 561 if (!$klarna_account || !$connector) { 562 return; 563 } 564 565 $klarna_order = $this->model_extension_payment_klarna_checkout->omRetrieve($connector, $this->request->post['order_ref']); 566 567 if (!$klarna_order) { 568 return; 569 } 570 571 $old_klarna_status = $klarna_order['status']; 572 573 if ($this->request->post['type'] == 'cancel') { 574 $action = $this->model_extension_payment_klarna_checkout->omCancel($connector, $this->request->post['order_ref']); 575 } elseif ($this->request->post['type'] == 'capture' && $this->request->post['data']) { 576 $action = $this->model_extension_payment_klarna_checkout->omCapture($connector, $this->request->post['order_ref'], array( 577 'captured_amount' => $this->request->post['data'] * 100 578 )); 579 } elseif ($this->request->post['type'] == 'refund' && $this->request->post['data']) { 580 $action = $this->model_extension_payment_klarna_checkout->omRefund($connector, $this->request->post['order_ref'], array( 581 'refunded_amount' => $this->request->post['data'] * 100 582 )); 583 } elseif ($this->request->post['type'] == 'extend_authorization') { 584 $action = $this->model_extension_payment_klarna_checkout->omExtendAuthorizationTime($connector, $this->request->post['order_ref']); 585 } elseif ($this->request->post['type'] == 'merchant_reference' && $this->request->post['data']) { 586 $data = array(); 587 parse_str(html_entity_decode($this->request->post['data']), $data); 588 589 $action = $this->model_extension_payment_klarna_checkout->omUpdateMerchantReference($connector, $this->request->post['order_ref'], array( 590 'merchant_reference1' => (string)$data['merchant_reference_1'] 591 )); 592 } elseif (($this->request->post['type'] == 'billing_address' || $this->request->post['type'] == 'shipping_address') && $this->request->post['data']) { 593 if ($this->request->post['type'] == 'billing_address') { 594 $data['billing_address'] = array(); 595 parse_str(html_entity_decode($this->request->post['data']), $data['billing_address']); 596 } else if ($this->request->post['type'] == 'shipping_address') { 597 $data['shipping_address'] = array(); 598 parse_str(html_entity_decode($this->request->post['data']), $data['shipping_address']); 599 } 600 601 $action = $this->model_extension_payment_klarna_checkout->omUpdateAddress($connector, $this->request->post['order_ref'], $data); 602 } elseif ($this->request->post['type'] == 'release_authorization') { 603 $action = $this->model_extension_payment_klarna_checkout->omReleaseAuthorization($connector, $this->request->post['order_ref']); 604 } elseif ($this->request->post['type'] == 'capture_shipping_info' && isset($this->request->post['id'])) { 605 $data = array(); 606 parse_str(html_entity_decode($this->request->post['data']), $data); 607 608 $action = $this->model_extension_payment_klarna_checkout->omShippingInfo($connector, $this->request->post['order_ref'], $this->request->post['id'], $data); 609 } elseif ($this->request->post['type'] == 'capture_billing_address' && isset($this->request->post['id'])) { 610 $data['billing_address'] = array(); 611 parse_str(html_entity_decode($this->request->post['data']), $data['billing_address']); 612 613 $action = $this->model_extension_payment_klarna_checkout->omCustomerDetails($connector, $this->request->post['order_ref'], $this->request->post['id'], $data); 614 } elseif ($this->request->post['type'] == 'trigger_send_out' && isset($this->request->post['id'])) { 615 $action = $this->model_extension_payment_klarna_checkout->omTriggerSendOut($connector, $this->request->post['order_ref'], $this->request->post['id']); 616 } else { 617 $error = true; 618 } 619 620 $klarna_order = $this->model_extension_payment_klarna_checkout->omRetrieve($connector, $this->request->post['order_ref']); 621 622 if (!$klarna_order) { 623 return; 624 } 625 626 $new_klarna_status = $klarna_order['status']; 627 628 $order_status_id = ''; 629 if ($old_klarna_status != $new_klarna_status) { 630 switch ($klarna_order['status']) { 631 case 'AUTHORIZED': 632 $order_status_id = $this->config->get('klarna_checkout_order_status_authorised_id'); 633 634 if ($klarna_order['fraud_status'] == 'PENDING') { 635 $order_status_id = $this->config->get('klarna_checkout_order_status_fraud_pending_id'); 636 } elseif ($klarna_order['fraud_status'] == 'REJECTED') { 637 $order_status_id = $this->config->get('klarna_checkout_order_status_fraud_rejected_id'); 638 } 639 break; 640 case 'PART_CAPTURED': 641 $order_status_id = $this->config->get('klarna_checkout_order_status_part_captured_id'); 642 break; 643 case 'CAPTURED': 644 $order_status_id = $this->config->get('klarna_checkout_order_status_captured_id'); 645 break; 646 case 'CANCELLED': 647 $order_status_id = $this->config->get('klarna_checkout_order_status_cancelled_id'); 648 break; 649 } 650 } elseif ($this->request->post['type'] == 'refund' && ($klarna_order['captured_amount'] - $klarna_order['refunded_amount'] == 0)) { 651 $order_status_id = $this->config->get('klarna_checkout_order_status_refund_id'); 652 } 653 654 if (!$error && $action) { 655 $success = $this->language->get('text_success_action'); 656 } elseif (!$error && $action && isset($action->message)) { 657 $error = sprintf($this->language->get('text_error_settle'), $action->message); 658 } else { 659 $error = $this->language->get('text_error_generic'); 660 } 661 662 $json['success'] = $success; 663 $json['error'] = $error; 664 $json['order_status_id'] = $order_status_id; 665 666 $this->response->addHeader('Content-Type: application/json'); 667 $this->response->setOutput(json_encode($json)); 668 } 669 670 public function downloadSettlementFiles() { 671 $this->load->language('extension/payment/klarna_checkout'); 672 673 $this->load->model('extension/payment/klarna_checkout'); 674 $this->load->model('sale/order'); 675 676 $json = array(); 677 678 $error = array(); 679 680 $klarna_checkout_directory = DIR_UPLOAD . 'klarna_checkout/'; 681 682 if (isset($this->request->post['username'])) { 683 $username = $this->request->post['username']; 684 } else { 685 $username = ''; 686 } 687 688 if (isset($this->request->post['password'])) { 689 $password = html_entity_decode($this->request->post['password']); 690 } else { 691 $password = ''; 692 } 693 694 if (isset($this->request->post['order_status_id'])) { 695 $order_status_id = $this->request->post['order_status_id']; 696 } else { 697 $order_status_id = false; 698 } 699 700 if (!$username || !$password || !$order_status_id) { 701 $error[] = 'Please supply a username, password and order status'; 702 } 703 704 if (!$error) { 705 // Connect to the site via FTP 706 $connection = ftp_connect('mft.klarna.com', '4001'); 707 708 $files = array(); 709 710 if ($connection) { 711 $login = ftp_login($connection, $username, $password); 712 713 if ($login) { 714 $files = ftp_nlist($connection, '.'); 715 716 rsort($files); 717 718 if (!is_dir($klarna_checkout_directory)) { 719 mkdir($klarna_checkout_directory, 0777); 720 } 721 722 // Save all files to local 723 foreach (array_diff($files, array('.', '..')) as $file) { 724 if (!ftp_get($connection, $klarna_checkout_directory . $file, $file, FTP_BINARY)) { 725 $error[] = 'There was a problem saving one or more files'; 726 } 727 } 728 } 729 } 730 } 731 732 $orders_to_process = array(); 733 734 $files = scandir($klarna_checkout_directory); 735 736 if (!$error) { 737 // Loop local files and process 738 foreach (array_diff($files, array('.', '..')) as $file) { 739 $handle = fopen($klarna_checkout_directory . $file, 'r'); 740 741 // Skip first 2 lines, use third as headings 742 fgetcsv($handle); 743 fgetcsv($handle); 744 $headings = fgetcsv($handle); 745 746 while ($data = fgetcsv($handle)) { 747 $row = array_combine($headings, $data); 748 749 if ($row['type'] == 'SALE') { 750 $order_id = $this->encryption->decrypt($this->config->get('config_encryption'), $row['merchant_reference1']); 751 752 $klarna_order_info = $this->model_extension_payment_klarna_checkout->getOrder($order_id); 753 754 $order_info = $this->model_sale_order->getOrder($order_id); 755 756 // Check if order exists in system, if it does, pass back to process 757 if ($klarna_order_info && $order_info && ($order_info['payment_code'] == 'klarna_checkout') && ($order_info['order_status_id'] != $order_status_id)) { 758 $orders_to_process[] = $order_id; 759 } 760 } 761 } 762 763 fclose($handle); 764 } 765 } 766 767 // Delete local files 768 foreach (array_diff($files, array('.', '..')) as $file) { 769 if (!unlink($klarna_checkout_directory . $file)) { 770 $error[] = 'Cannot delete files'; 771 } 772 } 773 774 if ($error) { 775 $orders_to_process = array(); 776 } 777 778 $json['error'] = $error; 779 $json['orders'] = $orders_to_process; 780 781 $this->response->addHeader('Content-Type: application/json'); 782 $this->response->setOutput(json_encode($json)); 783 } 784 785 protected function validate() { 786 $this->load->model('extension/payment/klarna_checkout'); 787 $this->load->model('localisation/geo_zone'); 788 789 if (version_compare(phpversion(), '5.4.0', '<')) { 790 $this->error['warning'] = $this->language->get('error_php_version'); 791 } 792 793 if (!$this->user->hasPermission('modify', 'extension/payment/klarna_checkout')) { 794 $this->error['warning'] = $this->language->get('error_permission'); 795 } 796 797 if (!$this->config->get('config_secure')) { 798 $this->error['warning'] = $this->language->get('error_ssl'); 799 } 800 801 if (empty($this->request->post['klarna_checkout_account'])) { 802 $this->error['account_warning'] = $this->language->get('error_account_minimum'); 803 } else { 804 $currencies = array(); 805 806 foreach ($this->request->post['klarna_checkout_account'] as $key => $account) { 807 if (in_array($account['currency'], $currencies)) { 808 $this->error['account_warning'] = $this->language->get('error_account_currency'); 809 810 break; 811 } else { 812 $currencies[] = $account['currency']; 813 } 814 815 if (!$account['merchant_id']) { 816 $this->error['account'][$key]['merchant_id'] = $this->language->get('error_merchant_id'); 817 } 818 819 if (!$account['secret']) { 820 $this->error['account'][$key]['secret'] = $this->language->get('error_secret'); 821 } 822 823 if (!$account['locale']) { 824 $this->error['account'][$key]['locale'] = $this->language->get('error_locale'); 825 } 826 } 827 } 828 829 if ($this->error && !isset($this->error['warning'])) { 830 $this->error['warning'] = $this->language->get('error_warning'); 831 } 832 833 return !$this->error; 834 } 835 }