/shop/catalog/controller/checkout/payment_method.php

https://bitbucket.org/jojoluzifer/gold-light-project · PHP · 198 lines · 142 code · 51 blank · 5 comment · 38 complexity · 2186db4a43a4415de1597feec6130e8d MD5 · raw file

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