payment_method.php (6089B)
1 <?php 2 class ControllerCheckoutPaymentMethod extends Controller { 3 public function index() { 4 $this->load->language('checkout/checkout'); 5 6 if (isset($this->session->data['payment_address'])) { 7 // Totals 8 $totals = array(); 9 $taxes = $this->cart->getTaxes(); 10 $total = 0; 11 12 // Because __call can not keep var references so we put them into an array. 13 $total_data = array( 14 'totals' => &$totals, 15 'taxes' => &$taxes, 16 'total' => &$total 17 ); 18 19 $this->load->model('setting/extension'); 20 21 $sort_order = array(); 22 23 $results = $this->model_setting_extension->getExtensions('total'); 24 25 foreach ($results as $key => $value) { 26 $sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order'); 27 } 28 29 array_multisort($sort_order, SORT_ASC, $results); 30 31 foreach ($results as $result) { 32 if ($this->config->get('total_' . $result['code'] . '_status')) { 33 $this->load->model('extension/total/' . $result['code']); 34 35 // We have to put the totals in an array so that they pass by reference. 36 $this->{'model_extension_total_' . $result['code']}->getTotal($total_data); 37 } 38 } 39 40 // Payment Methods 41 $method_data = array(); 42 43 $this->load->model('setting/extension'); 44 45 $results = $this->model_setting_extension->getExtensions('payment'); 46 47 $recurring = $this->cart->hasRecurringProducts(); 48 49 foreach ($results as $result) { 50 if ($this->config->get('payment_' . $result['code'] . '_status')) { 51 $this->load->model('extension/payment/' . $result['code']); 52 53 $method = $this->{'model_extension_payment_' . $result['code']}->getMethod($this->session->data['payment_address'], $total); 54 55 if ($method) { 56 if ($recurring) { 57 if (property_exists($this->{'model_extension_payment_' . $result['code']}, 'recurringPayments') && $this->{'model_extension_payment_' . $result['code']}->recurringPayments()) { 58 $method_data[$result['code']] = $method; 59 } 60 } else { 61 $method_data[$result['code']] = $method; 62 } 63 } 64 } 65 } 66 67 $sort_order = array(); 68 69 foreach ($method_data as $key => $value) { 70 $sort_order[$key] = $value['sort_order']; 71 } 72 73 array_multisort($sort_order, SORT_ASC, $method_data); 74 75 $this->session->data['payment_methods'] = $method_data; 76 } 77 78 if (empty($this->session->data['payment_methods'])) { 79 $data['error_warning'] = sprintf($this->language->get('error_no_payment'), $this->url->link('information/contact')); 80 } else { 81 $data['error_warning'] = ''; 82 } 83 84 if (isset($this->session->data['payment_methods'])) { 85 $data['payment_methods'] = $this->session->data['payment_methods']; 86 } else { 87 $data['payment_methods'] = array(); 88 } 89 90 if (isset($this->session->data['payment_method']['code'])) { 91 $data['code'] = $this->session->data['payment_method']['code']; 92 } else { 93 $data['code'] = ''; 94 } 95 96 if (isset($this->session->data['comment'])) { 97 $data['comment'] = $this->session->data['comment']; 98 } else { 99 $data['comment'] = ''; 100 } 101 102 $data['scripts'] = $this->document->getScripts(); 103 104 if ($this->config->get('config_checkout_id')) { 105 $this->load->model('catalog/information'); 106 107 $information_info = $this->model_catalog_information->getInformation($this->config->get('config_checkout_id')); 108 109 if ($information_info) { 110 $data['text_agree'] = sprintf($this->language->get('text_agree'), $this->url->link('information/information/agree', 'information_id=' . $this->config->get('config_checkout_id'), true), $information_info['title'], $information_info['title']); 111 } else { 112 $data['text_agree'] = ''; 113 } 114 } else { 115 $data['text_agree'] = ''; 116 } 117 118 if (isset($this->session->data['agree'])) { 119 $data['agree'] = $this->session->data['agree']; 120 } else { 121 $data['agree'] = ''; 122 } 123 124 $this->response->setOutput($this->load->view('checkout/payment_method', $data)); 125 } 126 127 public function save() { 128 $this->load->language('checkout/checkout'); 129 130 $json = array(); 131 132 // Validate if payment address has been set. 133 if (!isset($this->session->data['payment_address'])) { 134 $json['redirect'] = $this->url->link('checkout/checkout', '', true); 135 } 136 137 // Validate cart has products and has stock. 138 if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) { 139 $json['redirect'] = $this->url->link('checkout/cart'); 140 } 141 142 // Validate minimum quantity requirements. 143 $products = $this->cart->getProducts(); 144 145 foreach ($products as $product) { 146 $product_total = 0; 147 148 foreach ($products as $product_2) { 149 if ($product_2['product_id'] == $product['product_id']) { 150 $product_total += $product_2['quantity']; 151 } 152 } 153 154 if ($product['minimum'] > $product_total) { 155 $json['redirect'] = $this->url->link('checkout/cart'); 156 157 break; 158 } 159 } 160 161 if (!isset($this->request->post['payment_method'])) { 162 $json['error']['warning'] = $this->language->get('error_payment'); 163 } elseif (!isset($this->session->data['payment_methods'][$this->request->post['payment_method']])) { 164 $json['error']['warning'] = $this->language->get('error_payment'); 165 } 166 167 if ($this->config->get('config_checkout_id')) { 168 $this->load->model('catalog/information'); 169 170 $information_info = $this->model_catalog_information->getInformation($this->config->get('config_checkout_id')); 171 172 if ($information_info && !isset($this->request->post['agree'])) { 173 $json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']); 174 } 175 } 176 177 if (!$json) { 178 $this->session->data['payment_method'] = $this->session->data['payment_methods'][$this->request->post['payment_method']]; 179 180 $this->session->data['comment'] = strip_tags($this->request->post['comment']); 181 } 182 183 $this->response->addHeader('Content-Type: application/json'); 184 $this->response->setOutput(json_encode($json)); 185 } 186 }