PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/opencart_v1.4/upload/catalog/controller/checkout/payment.php

http://coderstalk.googlecode.com/
PHP | 262 lines | 202 code | 60 blank | 0 comment | 45 complexity | 9a91f955a066c34ede34d528c8914c7e MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-3.0
  1. <?php
  2. class ControllerCheckoutPayment extends Controller {
  3. private $error = array();
  4. public function index() {
  5. if (!$this->cart->hasProducts() || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
  6. $this->redirect($this->url->https('checkout/cart'));
  7. }
  8. if (!$this->customer->isLogged()) {
  9. $this->session->data['redirect'] = $this->url->https('checkout/shipping');
  10. $this->redirect($this->url->https('account/login'));
  11. }
  12. $this->load->model('account/address');
  13. if ($this->cart->hasShipping()) {
  14. if (!isset($this->session->data['shipping_address_id']) || !$this->session->data['shipping_address_id']) {
  15. $this->redirect($this->url->https('checkout/shipping'));
  16. }
  17. if (!isset($this->session->data['shipping_method'])) {
  18. $this->redirect($this->url->https('checkout/shipping'));
  19. }
  20. } else {
  21. unset($this->session->data['shipping_address_id']);
  22. unset($this->session->data['shipping_method']);
  23. unset($this->session->data['shipping_methods']);
  24. $this->tax->setZone($this->config->get('config_country_id'), $this->config->get('config_zone_id'));
  25. }
  26. if (!isset($this->session->data['payment_address_id']) && isset($this->session->data['shipping_address_id']) && $this->session->data['shipping_address_id']) {
  27. $this->session->data['payment_address_id'] = $this->session->data['shipping_address_id'];
  28. }
  29. if (!isset($this->session->data['payment_address_id'])) {
  30. $this->session->data['payment_address_id'] = $this->customer->getAddressId();
  31. }
  32. if (!$this->session->data['payment_address_id']) {
  33. $this->redirect($this->url->https('checkout/address/payment'));
  34. }
  35. $this->load->model('account/address');
  36. $payment_address = $this->model_account_address->getAddress($this->session->data['payment_address_id']);
  37. if (!$payment_address) {
  38. $this->redirect($this->url->https('checkout/address/payment'));
  39. }
  40. $this->load->model('checkout/extension');
  41. $method_data = array();
  42. $results = $this->model_checkout_extension->getExtensions('payment');
  43. foreach ($results as $result) {
  44. $this->load->model('payment/' . $result['key']);
  45. $method = $this->{'model_payment_' . $result['key']}->getMethod($payment_address);
  46. if ($method) {
  47. $method_data[$result['key']] = $method;
  48. }
  49. }
  50. $sort_order = array();
  51. foreach ($method_data as $key => $value) {
  52. $sort_order[$key] = $value['sort_order'];
  53. }
  54. array_multisort($sort_order, SORT_ASC, $method_data);
  55. $this->session->data['payment_methods'] = $method_data;
  56. $this->language->load('checkout/payment');
  57. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
  58. $this->session->data['payment_method'] = $this->session->data['payment_methods'][$this->request->post['payment_method']];
  59. $this->session->data['comment'] = strip_tags($this->request->post['comment']);
  60. $this->redirect($this->url->https('checkout/confirm'));
  61. }
  62. $this->document->title = $this->language->get('heading_title');
  63. $this->document->breadcrumbs = array();
  64. $this->document->breadcrumbs[] = array(
  65. 'href' => $this->url->http('common/home'),
  66. 'text' => $this->language->get('text_home'),
  67. 'separator' => FALSE
  68. );
  69. $this->document->breadcrumbs[] = array(
  70. 'href' => $this->url->http('checkout/cart'),
  71. 'text' => $this->language->get('text_basket'),
  72. 'separator' => $this->language->get('text_separator')
  73. );
  74. $this->document->breadcrumbs[] = array(
  75. 'href' => $this->url->http('checkout/shipping'),
  76. 'text' => $this->language->get('text_shipping'),
  77. 'separator' => $this->language->get('text_separator')
  78. );
  79. $this->document->breadcrumbs[] = array(
  80. 'href' => $this->url->http('checkout/payment'),
  81. 'text' => $this->language->get('text_payment'),
  82. 'separator' => $this->language->get('text_separator')
  83. );
  84. $this->data['heading_title'] = $this->language->get('heading_title');
  85. $this->data['text_payment_to'] = $this->language->get('text_payment_to');
  86. $this->data['text_payment_address'] = $this->language->get('text_payment_address');
  87. $this->data['text_payment_method'] = $this->language->get('text_payment_method');
  88. $this->data['text_payment_methods'] = $this->language->get('text_payment_methods');
  89. $this->data['text_comments'] = $this->language->get('text_comments');
  90. $this->data['button_change_address'] = $this->language->get('button_change_address');
  91. $this->data['button_continue'] = $this->language->get('button_continue');
  92. $this->data['button_back'] = $this->language->get('button_back');
  93. if (isset($this->session->data['error'])) {
  94. $this->data['error_warning'] = $this->session->data['error'];
  95. unset($this->session->data['error']);
  96. } elseif (isset($this->error['warning'])) {
  97. $this->data['error_warning'] = $this->error['warning'];
  98. } else {
  99. $this->data['error_warning'] = '';
  100. }
  101. $this->data['action'] = $this->url->https('checkout/payment');
  102. if ($payment_address['address_format']) {
  103. $format = $payment_address['address_format'];
  104. } else {
  105. $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
  106. }
  107. $find = array(
  108. '{firstname}',
  109. '{lastname}',
  110. '{company}',
  111. '{address_1}',
  112. '{address_2}',
  113. '{city}',
  114. '{postcode}',
  115. '{zone}',
  116. '{country}'
  117. );
  118. $replace = array(
  119. 'firstname' => $payment_address['firstname'],
  120. 'lastname' => $payment_address['lastname'],
  121. 'company' => $payment_address['company'],
  122. 'address_1' => $payment_address['address_1'],
  123. 'address_2' => $payment_address['address_2'],
  124. 'city' => $payment_address['city'],
  125. 'postcode' => $payment_address['postcode'],
  126. 'zone' => $payment_address['zone'],
  127. 'country' => $payment_address['country']
  128. );
  129. $this->data['address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));
  130. $this->data['change_address'] = $this->url->https('checkout/address/payment');
  131. $this->data['payment_methods'] = $this->session->data['payment_methods'];
  132. if (isset($this->request->post['payment_method'])) {
  133. $this->data['payment'] = $this->request->post['payment_method'];
  134. } elseif (isset($this->session->data['payment_method']['id'])) {
  135. $this->data['payment'] = $this->session->data['payment_method']['id'];
  136. } else {
  137. $this->data['payment'] = '';
  138. }
  139. if (isset($this->session->data['comment'])) {
  140. $this->data['comment'] = $this->session->data['comment'];
  141. } else {
  142. $this->data['comment'] = '';
  143. }
  144. if ($this->config->get('config_checkout')) {
  145. $this->load->model('catalog/information');
  146. $information_info = $this->model_catalog_information->getInformation($this->config->get('config_checkout'));
  147. if ($information_info) {
  148. $this->data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->http('information/information&information_id=' . $this->config->get('config_checkout')), $information_info['title']);
  149. } else {
  150. $this->data['text_agree'] = '';
  151. }
  152. } else {
  153. $this->data['text_agree'] = '';
  154. }
  155. if (isset($this->request->post['agree'])) {
  156. $this->data['agree'] = $this->request->post['agree'];
  157. } else {
  158. $this->data['agree'] = '';
  159. }
  160. if ($this->cart->hasShipping()) {
  161. $this->data['back'] = $this->url->https('checkout/shipping');
  162. } else {
  163. $this->data['back'] = $this->url->https('checkout/cart');
  164. }
  165. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/checkout/payment.tpl')) {
  166. $this->template = $this->config->get('config_template') . '/template/checkout/payment.tpl';
  167. } else {
  168. $this->template = 'default/template/checkout/payment.tpl';
  169. }
  170. $this->children = array(
  171. 'common/header',
  172. 'common/footer',
  173. 'common/column_left',
  174. 'common/column_right'
  175. );
  176. $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
  177. }
  178. private function validate() {
  179. if (!isset($this->request->post['payment_method'])) {
  180. $this->error['warning'] = $this->language->get('error_payment');
  181. } else {
  182. if (!isset($this->session->data['payment_methods'][$this->request->post['payment_method']])) {
  183. $this->error['warning'] = $this->language->get('error_payment');
  184. }
  185. }
  186. if ($this->config->get('config_checkout')) {
  187. $this->load->model('catalog/information');
  188. $information_info = $this->model_catalog_information->getInformation($this->config->get('config_checkout'));
  189. if ($information_info) {
  190. if (!isset($this->request->post['agree'])) {
  191. $this->error['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
  192. }
  193. }
  194. }
  195. if (!$this->error) {
  196. return TRUE;
  197. } else {
  198. return FALSE;
  199. }
  200. }
  201. }
  202. ?>