/store/catalog/controller/checkout/payment.php

https://github.com/elleeott/WPOC-boilerplate · PHP · 168 lines · 125 code · 41 blank · 2 comment · 39 complexity · da374071508eaa138c5b588bb4214f68 MD5 · raw file

  1. <?php
  2. class ControllerCheckoutPayment extends Controller {
  3. public function index() {
  4. $this->language->load('checkout/checkout');
  5. $json = array();
  6. $this->load->model('account/address');
  7. if ($this->customer->isLogged()) {
  8. $payment_address = $this->model_account_address->getAddress($this->session->data['payment_address_id']);
  9. } elseif (isset($this->session->data['guest'])) {
  10. $payment_address = $this->session->data['guest']['payment'];
  11. }
  12. if (!isset($payment_address)) {
  13. $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
  14. }
  15. if ((!$this->cart->hasProducts() && (!isset($this->session->data['vouchers']) || !$this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
  16. $json['redirect'] = $this->url->link('checkout/cart');
  17. }
  18. if ($this->request->server['REQUEST_METHOD'] == 'POST') {
  19. if (!$json) {
  20. if (!isset($this->request->post['payment_method'])) {
  21. $json['error']['warning'] = $this->language->get('error_payment');
  22. } else {
  23. if (!isset($this->session->data['payment_methods'][$this->request->post['payment_method']])) {
  24. $json['error']['warning'] = $this->language->get('error_payment');
  25. }
  26. }
  27. if ($this->config->get('config_checkout_id')) {
  28. $this->load->model('catalog/information');
  29. $information_info = $this->model_catalog_information->getInformation($this->config->get('config_checkout_id'));
  30. if ($information_info && !isset($this->request->post['agree'])) {
  31. $json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
  32. }
  33. }
  34. }
  35. if (!$json) {
  36. $this->session->data['payment_method'] = $this->session->data['payment_methods'][$this->request->post['payment_method']];
  37. $this->session->data['comment'] = strip_tags($this->request->post['comment']);
  38. }
  39. } else {
  40. if (!isset($this->session->data['payment_methods'])) {
  41. // Calculate Totals
  42. $total_data = array();
  43. $total = 0;
  44. $taxes = $this->cart->getTaxes();
  45. $this->load->model('setting/extension');
  46. $sort_order = array();
  47. $results = $this->model_setting_extension->getExtensions('total');
  48. foreach ($results as $key => $value) {
  49. $sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
  50. }
  51. array_multisort($sort_order, SORT_ASC, $results);
  52. foreach ($results as $result) {
  53. if ($this->config->get($result['code'] . '_status')) {
  54. $this->load->model('total/' . $result['code']);
  55. $this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
  56. }
  57. }
  58. // Payment Methods
  59. $method_data = array();
  60. $this->load->model('setting/extension');
  61. $results = $this->model_setting_extension->getExtensions('payment');
  62. foreach ($results as $result) {
  63. if ($this->config->get($result['code'] . '_status')) {
  64. $this->load->model('payment/' . $result['code']);
  65. $method = $this->{'model_payment_' . $result['code']}->getMethod($payment_address, $total);
  66. if ($method) {
  67. $method_data[$result['code']] = $method;
  68. }
  69. }
  70. }
  71. $sort_order = array();
  72. foreach ($method_data as $key => $value) {
  73. $sort_order[$key] = $value['sort_order'];
  74. }
  75. array_multisort($sort_order, SORT_ASC, $method_data);
  76. $this->session->data['payment_methods'] = $method_data;
  77. }
  78. $this->data['text_payment_method'] = $this->language->get('text_payment_method');
  79. $this->data['text_comments'] = $this->language->get('text_comments');
  80. $this->data['button_continue'] = $this->language->get('button_continue');
  81. if (isset($this->session->data['payment_methods']) && !$this->session->data['payment_methods']) {
  82. $this->data['error_warning'] = sprintf($this->language->get('error_no_payment'), $this->url->link('information/contact'));
  83. } else {
  84. $this->data['error_warning'] = '';
  85. }
  86. if (isset($this->session->data['payment_methods'])) {
  87. $this->data['payment_methods'] = $this->session->data['payment_methods'];
  88. } else {
  89. $this->data['payment_methods'] = array();
  90. }
  91. if (isset($this->session->data['payment_method']['code'])) {
  92. $this->data['code'] = $this->session->data['payment_method']['code'];
  93. } else {
  94. $this->data['code'] = '';
  95. }
  96. if (isset($this->session->data['comment'])) {
  97. $this->data['comment'] = $this->session->data['comment'];
  98. } else {
  99. $this->data['comment'] = '';
  100. }
  101. if ($this->config->get('config_checkout_id')) {
  102. $this->load->model('catalog/information');
  103. $information_info = $this->model_catalog_information->getInformation($this->config->get('config_checkout_id'));
  104. if ($information_info) {
  105. $this->data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information/info', 'information_id=' . $this->config->get('config_checkout_id'), 'SSL'), $information_info['title'], $information_info['title']);
  106. } else {
  107. $this->data['text_agree'] = '';
  108. }
  109. } else {
  110. $this->data['text_agree'] = '';
  111. }
  112. if (isset($this->session->data['agree'])) {
  113. $this->data['agree'] = $this->session->data['agree'];
  114. } else {
  115. $this->data['agree'] = '';
  116. }
  117. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/checkout/payment.tpl')) {
  118. $this->template = $this->config->get('config_template') . '/template/checkout/payment.tpl';
  119. } else {
  120. $this->template = 'default/template/checkout/payment.tpl';
  121. }
  122. $json['output'] = $this->render();
  123. }
  124. $this->response->setOutput(json_encode($json));
  125. }
  126. }
  127. ?>