PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/catalog/controller/checkout/confirm.php

https://bitbucket.org/thomaskatalis/opencart
PHP | 351 lines | 285 code | 63 blank | 3 comment | 42 complexity | d12b449151a780e6beb20e9df9d959f2 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. class ControllerCheckoutConfirm extends Controller {
  3. public function index() {
  4. if ((!$this->cart->hasProducts() && (!isset($this->session->data['vouchers']) || !$this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
  5. $json['redirect'] = $this->url->link('checkout/cart');
  6. }
  7. $this->load->model('account/address');
  8. if ($this->customer->isLogged()) {
  9. $payment_address = $this->model_account_address->getAddress($this->session->data['payment_address_id']);
  10. } elseif (isset($this->session->data['guest'])) {
  11. $payment_address = $this->session->data['guest']['payment'];
  12. }
  13. if (!isset($payment_address)) {
  14. $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
  15. }
  16. if (!isset($this->session->data['payment_method'])) {
  17. $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
  18. }
  19. if ($this->cart->hasShipping()) {
  20. $this->load->model('account/address');
  21. if ($this->customer->isLogged()) {
  22. $shipping_address = $this->model_account_address->getAddress($this->session->data['shipping_address_id']);
  23. } elseif (isset($this->session->data['guest'])) {
  24. $shipping_address = $this->session->data['guest']['shipping'];
  25. }
  26. if (!isset($shipping_address)) {
  27. $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
  28. }
  29. if (!isset($this->session->data['shipping_method'])) {
  30. $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
  31. }
  32. } else {
  33. unset($this->session->data['guest']['shipping']);
  34. unset($this->session->data['shipping_address_id']);
  35. unset($this->session->data['shipping_method']);
  36. unset($this->session->data['shipping_methods']);
  37. }
  38. $json = array();
  39. if (!$json) {
  40. $total_data = array();
  41. $total = 0;
  42. $taxes = $this->cart->getTaxes();
  43. $this->load->model('setting/extension');
  44. $sort_order = array();
  45. $results = $this->model_setting_extension->getExtensions('total');
  46. foreach ($results as $key => $value) {
  47. $sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
  48. }
  49. array_multisort($sort_order, SORT_ASC, $results);
  50. foreach ($results as $result) {
  51. if ($this->config->get($result['code'] . '_status')) {
  52. $this->load->model('total/' . $result['code']);
  53. $this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
  54. }
  55. }
  56. $sort_order = array();
  57. foreach ($total_data as $key => $value) {
  58. $sort_order[$key] = $value['sort_order'];
  59. }
  60. array_multisort($sort_order, SORT_ASC, $total_data);
  61. $this->language->load('checkout/checkout');
  62. $data = array();
  63. $data['store_id'] = $this->config->get('config_store_id');
  64. $data['store_name'] = $this->config->get('config_name');
  65. if ($data['store_id']) {
  66. $data['store_url'] = $this->config->get('config_url');
  67. } else {
  68. $data['store_url'] = HTTP_SERVER;
  69. }
  70. if ($this->customer->isLogged()) {
  71. $data['customer_id'] = $this->customer->getId();
  72. $data['customer_group_id'] = $this->customer->getCustomerGroupId();
  73. $data['firstname'] = $this->customer->getFirstName();
  74. $data['lastname'] = $this->customer->getLastName();
  75. $data['email'] = $this->customer->getEmail();
  76. $data['telephone'] = $this->customer->getTelephone();
  77. $data['fax'] = $this->customer->getFax();
  78. $this->load->model('account/address');
  79. $payment_address = $this->model_account_address->getAddress($this->session->data['payment_address_id']);
  80. } elseif (isset($this->session->data['guest'])) {
  81. $data['customer_id'] = 0;
  82. $data['customer_group_id'] = $this->config->get('config_customer_group_id');
  83. $data['firstname'] = $this->session->data['guest']['firstname'];
  84. $data['lastname'] = $this->session->data['guest']['lastname'];
  85. $data['email'] = $this->session->data['guest']['email'];
  86. $data['telephone'] = $this->session->data['guest']['telephone'];
  87. $data['fax'] = $this->session->data['guest']['fax'];
  88. $payment_address = $this->session->data['guest']['payment'];
  89. }
  90. $data['payment_firstname'] = $payment_address['firstname'];
  91. $data['payment_lastname'] = $payment_address['lastname'];
  92. $data['payment_company'] = $payment_address['company'];
  93. $data['payment_address_1'] = $payment_address['address_1'];
  94. $data['payment_address_2'] = $payment_address['address_2'];
  95. $data['payment_city'] = $payment_address['city'];
  96. $data['payment_postcode'] = $payment_address['postcode'];
  97. $data['payment_zone'] = $payment_address['zone'];
  98. $data['payment_zone_id'] = $payment_address['zone_id'];
  99. $data['payment_country'] = $payment_address['country'];
  100. $data['payment_country_id'] = $payment_address['country_id'];
  101. $data['payment_address_format'] = $payment_address['address_format'];
  102. if (isset($this->session->data['payment_method']['title'])) {
  103. $data['payment_method'] = $this->session->data['payment_method']['title'];
  104. } else {
  105. $data['payment_method'] = '';
  106. }
  107. if ($this->cart->hasShipping()) {
  108. if ($this->customer->isLogged()) {
  109. $this->load->model('account/address');
  110. $shipping_address = $this->model_account_address->getAddress($this->session->data['shipping_address_id']);
  111. } elseif (isset($this->session->data['guest'])) {
  112. $shipping_address = $this->session->data['guest']['shipping'];
  113. }
  114. $data['shipping_firstname'] = $shipping_address['firstname'];
  115. $data['shipping_lastname'] = $shipping_address['lastname'];
  116. $data['shipping_company'] = $shipping_address['company'];
  117. $data['shipping_address_1'] = $shipping_address['address_1'];
  118. $data['shipping_address_2'] = $shipping_address['address_2'];
  119. $data['shipping_city'] = $shipping_address['city'];
  120. $data['shipping_postcode'] = $shipping_address['postcode'];
  121. $data['shipping_zone'] = $shipping_address['zone'];
  122. $data['shipping_zone_id'] = $shipping_address['zone_id'];
  123. $data['shipping_country'] = $shipping_address['country'];
  124. $data['shipping_country_id'] = $shipping_address['country_id'];
  125. $data['shipping_address_format'] = $shipping_address['address_format'];
  126. if (isset($this->session->data['shipping_method']['title'])) {
  127. $data['shipping_method'] = $this->session->data['shipping_method']['title'];
  128. } else {
  129. $data['shipping_method'] = '';
  130. }
  131. } else {
  132. $data['shipping_firstname'] = '';
  133. $data['shipping_lastname'] = '';
  134. $data['shipping_company'] = '';
  135. $data['shipping_address_1'] = '';
  136. $data['shipping_address_2'] = '';
  137. $data['shipping_city'] = '';
  138. $data['shipping_postcode'] = '';
  139. $data['shipping_zone'] = '';
  140. $data['shipping_zone_id'] = '';
  141. $data['shipping_country'] = '';
  142. $data['shipping_country_id'] = '';
  143. $data['shipping_address_format'] = '';
  144. $data['shipping_method'] = '';
  145. }
  146. if ($this->cart->hasShipping()) {
  147. $this->tax->setZone($shipping_address['country_id'], $shipping_address['zone_id']);
  148. } else {
  149. $this->tax->setZone($payment_address['country_id'], $payment_address['zone_id']);
  150. }
  151. $product_data = array();
  152. foreach ($this->cart->getProducts() as $product) {
  153. $option_data = array();
  154. foreach ($product['option'] as $option) {
  155. $option_data[] = array(
  156. 'product_option_id' => $option['product_option_id'],
  157. 'product_option_value_id' => $option['product_option_value_id'],
  158. 'product_option_id' => $option['product_option_id'],
  159. 'product_option_value_id' => $option['product_option_value_id'],
  160. 'option_id' => $option['option_id'],
  161. 'option_value_id' => $option['option_value_id'],
  162. 'name' => $option['name'],
  163. 'value' => $option['option_value'],
  164. 'type' => $option['type']
  165. );
  166. }
  167. $product_data[] = array(
  168. 'product_id' => $product['product_id'],
  169. 'name' => $product['name'],
  170. 'model' => $product['model'],
  171. 'option' => $option_data,
  172. 'download' => $product['download'],
  173. 'quantity' => $product['quantity'],
  174. 'subtract' => $product['subtract'],
  175. 'price' => $product['price'],
  176. 'total' => $product['total'],
  177. 'tax' => $this->tax->getRate($product['tax_class_id'])
  178. );
  179. }
  180. // Gift Voucher
  181. if (isset($this->session->data['vouchers']) && $this->session->data['vouchers']) {
  182. foreach ($this->session->data['vouchers'] as $voucher) {
  183. $product_data[] = array(
  184. 'product_id' => 0,
  185. 'name' => $voucher['description'],
  186. 'model' => '',
  187. 'option' => array(),
  188. 'download' => array(),
  189. 'quantity' => 1,
  190. 'subtract' => false,
  191. 'price' => $voucher['amount'],
  192. 'total' => $voucher['amount'],
  193. 'tax' => 0
  194. );
  195. }
  196. }
  197. $data['products'] = $product_data;
  198. $data['totals'] = $total_data;
  199. $data['comment'] = $this->session->data['comment'];
  200. $data['total'] = $total;
  201. $data['reward'] = $this->cart->getTotalRewardPoints();
  202. if (isset($this->request->cookie['tracking'])) {
  203. $this->load->model('affiliate/affiliate');
  204. $affiliate_info = $this->model_affiliate_affiliate->getAffiliateByCode($this->request->cookie['tracking']);
  205. if ($affiliate_info) {
  206. $data['affiliate_id'] = $affiliate_info['affiliate_id'];
  207. $data['commission'] = ($total / 100) * $affiliate_info['commission'];
  208. } else {
  209. $data['affiliate_id'] = 0;
  210. $data['commission'] = 0;
  211. }
  212. } else {
  213. $data['affiliate_id'] = 0;
  214. $data['commission'] = 0;
  215. }
  216. $data['language_id'] = $this->config->get('config_language_id');
  217. $data['currency_id'] = $this->currency->getId();
  218. $data['currency_code'] = $this->currency->getCode();
  219. $data['currency_value'] = $this->currency->getValue($this->currency->getCode());
  220. $data['ip'] = $this->request->server['REMOTE_ADDR'];
  221. $this->load->model('checkout/order');
  222. $this->session->data['order_id'] = $this->model_checkout_order->create($data);
  223. // Gift Voucher
  224. if (isset($this->session->data['vouchers']) && is_array($this->session->data['vouchers'])) {
  225. $this->load->model('checkout/voucher');
  226. foreach ($this->session->data['vouchers'] as $voucher) {
  227. $this->model_checkout_voucher->addVoucher($this->session->data['order_id'], $voucher);
  228. }
  229. }
  230. $this->data['column_name'] = $this->language->get('column_name');
  231. $this->data['column_model'] = $this->language->get('column_model');
  232. $this->data['column_quantity'] = $this->language->get('column_quantity');
  233. $this->data['column_price'] = $this->language->get('column_price');
  234. $this->data['column_total'] = $this->language->get('column_total');
  235. $this->data['products'] = array();
  236. foreach ($this->cart->getProducts() as $product) {
  237. $option_data = array();
  238. foreach ($product['option'] as $option) {
  239. if ($option['type'] != 'file') {
  240. $option_data[] = array(
  241. 'name' => $option['name'],
  242. 'value' => (strlen($option['option_value']) > 20 ? substr($option['option_value'], 0, 20) . '..' : $option['option_value'])
  243. );
  244. } else {
  245. $filename = substr($option['option_value'], 0, strrpos($option['option_value'], '.'));
  246. $option_data[] = array(
  247. 'name' => $option['name'],
  248. 'value' => (strlen($filename) > 20 ? substr($filename, 0, 20) . '..' : $filename)
  249. );
  250. }
  251. }
  252. $this->data['products'][] = array(
  253. 'product_id' => $product['product_id'],
  254. 'name' => $product['name'],
  255. 'model' => $product['model'],
  256. 'option' => $option_data,
  257. 'quantity' => $product['quantity'],
  258. 'subtract' => $product['subtract'],
  259. 'tax' => $this->tax->getRate($product['tax_class_id']),
  260. 'price' => $this->currency->format($product['price']),
  261. 'total' => $this->currency->format($product['total']),
  262. 'href' => $this->url->link('product/product', 'product_id=' . $product['product_id'])
  263. );
  264. }
  265. // Gift Voucher
  266. $this->data['vouchers'] = array();
  267. if (isset($this->session->data['vouchers']) && $this->session->data['vouchers']) {
  268. foreach ($this->session->data['vouchers'] as $key => $voucher) {
  269. $this->data['vouchers'][] = array(
  270. 'description' => $voucher['description'],
  271. 'amount' => $this->currency->format($voucher['amount'])
  272. );
  273. }
  274. }
  275. $this->data['totals'] = $total_data;
  276. $this->data['payment'] = $this->getChild('payment/' . $this->session->data['payment_method']['code']);
  277. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/checkout/confirm.tpl')) {
  278. $this->template = $this->config->get('config_template') . '/template/checkout/confirm.tpl';
  279. } else {
  280. $this->template = 'default/template/checkout/confirm.tpl';
  281. }
  282. $json['output'] = $this->render();
  283. }
  284. $this->load->library('json');
  285. $this->response->setOutput(Json::encode($json));
  286. }
  287. }
  288. ?>