payment.php (10003B)
1 <?php 2 class ControllerApiPayment extends Controller { 3 public function address() { 4 $this->load->language('api/payment'); 5 6 // Delete old payment address, payment methods and method so not to cause any issues if there is an error 7 unset($this->session->data['payment_address']); 8 unset($this->session->data['payment_methods']); 9 unset($this->session->data['payment_method']); 10 11 $json = array(); 12 13 if (!isset($this->session->data['api_id'])) { 14 $json['error']['warning'] = $this->language->get('error_permission'); 15 } else { 16 // Add keys for missing post vars 17 $keys = array( 18 'firstname', 19 'lastname', 20 'company', 21 'address_1', 22 'address_2', 23 'postcode', 24 'city', 25 'zone_id', 26 'country_id' 27 ); 28 29 foreach ($keys as $key) { 30 if (!isset($this->request->post[$key])) { 31 $this->request->post[$key] = ''; 32 } 33 } 34 35 if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) { 36 $json['error']['firstname'] = $this->language->get('error_firstname'); 37 } 38 39 if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) { 40 $json['error']['lastname'] = $this->language->get('error_lastname'); 41 } 42 43 if ((utf8_strlen(trim($this->request->post['address_1'])) < 3) || (utf8_strlen(trim($this->request->post['address_1'])) > 128)) { 44 $json['error']['address_1'] = $this->language->get('error_address_1'); 45 } 46 47 if ((utf8_strlen($this->request->post['city']) < 2) || (utf8_strlen($this->request->post['city']) > 32)) { 48 $json['error']['city'] = $this->language->get('error_city'); 49 } 50 51 $this->load->model('localisation/country'); 52 53 $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']); 54 55 if ($country_info && $country_info['postcode_required'] && (utf8_strlen(trim($this->request->post['postcode'])) < 2 || utf8_strlen(trim($this->request->post['postcode'])) > 10)) { 56 $json['error']['postcode'] = $this->language->get('error_postcode'); 57 } 58 59 if ($this->request->post['country_id'] == '') { 60 $json['error']['country'] = $this->language->get('error_country'); 61 } 62 63 if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') { 64 $json['error']['zone'] = $this->language->get('error_zone'); 65 } 66 67 // Custom field validation 68 $this->load->model('account/custom_field'); 69 70 $custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id')); 71 72 foreach ($custom_fields as $custom_field) { 73 if ($custom_field['location'] == 'address') { 74 if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']])) { 75 $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); 76 } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['location']][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) { 77 $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']); 78 } 79 } 80 } 81 82 if (!$json) { 83 $this->load->model('localisation/country'); 84 85 $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']); 86 87 if ($country_info) { 88 $country = $country_info['name']; 89 $iso_code_2 = $country_info['iso_code_2']; 90 $iso_code_3 = $country_info['iso_code_3']; 91 $address_format = $country_info['address_format']; 92 } else { 93 $country = ''; 94 $iso_code_2 = ''; 95 $iso_code_3 = ''; 96 $address_format = ''; 97 } 98 99 $this->load->model('localisation/zone'); 100 101 $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']); 102 103 if ($zone_info) { 104 $zone = $zone_info['name']; 105 $zone_code = $zone_info['code']; 106 } else { 107 $zone = ''; 108 $zone_code = ''; 109 } 110 111 $this->session->data['payment_address'] = array( 112 'firstname' => $this->request->post['firstname'], 113 'lastname' => $this->request->post['lastname'], 114 'company' => $this->request->post['company'], 115 'address_1' => $this->request->post['address_1'], 116 'address_2' => $this->request->post['address_2'], 117 'postcode' => $this->request->post['postcode'], 118 'city' => $this->request->post['city'], 119 'zone_id' => $this->request->post['zone_id'], 120 'zone' => $zone, 121 'zone_code' => $zone_code, 122 'country_id' => $this->request->post['country_id'], 123 'country' => $country, 124 'iso_code_2' => $iso_code_2, 125 'iso_code_3' => $iso_code_3, 126 'address_format' => $address_format, 127 'custom_field' => isset($this->request->post['custom_field']) ? $this->request->post['custom_field'] : array() 128 ); 129 130 $json['success'] = $this->language->get('text_address'); 131 132 unset($this->session->data['payment_method']); 133 unset($this->session->data['payment_methods']); 134 } 135 } 136 137 $this->response->addHeader('Content-Type: application/json'); 138 $this->response->setOutput(json_encode($json)); 139 } 140 141 public function methods() { 142 $this->load->language('api/payment'); 143 144 // Delete past shipping methods and method just in case there is an error 145 unset($this->session->data['payment_methods']); 146 unset($this->session->data['payment_method']); 147 148 $json = array(); 149 150 if (!isset($this->session->data['api_id'])) { 151 $json['error'] = $this->language->get('error_permission'); 152 } else { 153 // Payment Address 154 if (!isset($this->session->data['payment_address'])) { 155 $json['error'] = $this->language->get('error_address'); 156 } 157 158 if (!$json) { 159 // Totals 160 $totals = array(); 161 $taxes = $this->cart->getTaxes(); 162 $total = 0; 163 164 // Because __call can not keep var references so we put them into an array. 165 $total_data = array( 166 'totals' => &$totals, 167 'taxes' => &$taxes, 168 'total' => &$total 169 ); 170 171 $this->load->model('setting/extension'); 172 173 $sort_order = array(); 174 175 $results = $this->model_setting_extension->getExtensions('total'); 176 177 foreach ($results as $key => $value) { 178 $sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order'); 179 } 180 181 array_multisort($sort_order, SORT_ASC, $results); 182 183 foreach ($results as $result) { 184 if ($this->config->get('total_' . $result['code'] . '_status')) { 185 $this->load->model('extension/total/' . $result['code']); 186 187 // We have to put the totals in an array so that they pass by reference. 188 $this->{'model_extension_total_' . $result['code']}->getTotal($total_data); 189 } 190 } 191 192 // Payment Methods 193 $json['payment_methods'] = array(); 194 195 $this->load->model('setting/extension'); 196 197 $results = $this->model_setting_extension->getExtensions('payment'); 198 199 $recurring = $this->cart->hasRecurringProducts(); 200 201 foreach ($results as $result) { 202 if ($this->config->get('payment_' . $result['code'] . '_status')) { 203 $this->load->model('extension/payment/' . $result['code']); 204 205 $method = $this->{'model_extension_payment_' . $result['code']}->getMethod($this->session->data['payment_address'], $total); 206 207 if ($method) { 208 if ($recurring) { 209 if (property_exists($this->{'model_extension_payment_' . $result['code']}, 'recurringPayments') && $this->{'model_extension_payment_' . $result['code']}->recurringPayments()) { 210 $json['payment_methods'][$result['code']] = $method; 211 } 212 } else { 213 $json['payment_methods'][$result['code']] = $method; 214 } 215 } 216 } 217 } 218 219 $sort_order = array(); 220 221 foreach ($json['payment_methods'] as $key => $value) { 222 $sort_order[$key] = $value['sort_order']; 223 } 224 225 array_multisort($sort_order, SORT_ASC, $json['payment_methods']); 226 227 if ($json['payment_methods']) { 228 $this->session->data['payment_methods'] = $json['payment_methods']; 229 } else { 230 $json['error'] = $this->language->get('error_no_payment'); 231 } 232 } 233 } 234 235 $this->response->addHeader('Content-Type: application/json'); 236 $this->response->setOutput(json_encode($json)); 237 } 238 239 public function method() { 240 $this->load->language('api/payment'); 241 242 // Delete old payment method so not to cause any issues if there is an error 243 unset($this->session->data['payment_method']); 244 245 $json = array(); 246 247 if (!isset($this->session->data['api_id'])) { 248 $json['error'] = $this->language->get('error_permission'); 249 } else { 250 // Payment Address 251 if (!isset($this->session->data['payment_address'])) { 252 $json['error'] = $this->language->get('error_address'); 253 } 254 255 // Payment Method 256 if (empty($this->session->data['payment_methods'])) { 257 $json['error'] = $this->language->get('error_no_payment'); 258 } elseif (!isset($this->request->post['payment_method'])) { 259 $json['error'] = $this->language->get('error_method'); 260 } elseif (!isset($this->session->data['payment_methods'][$this->request->post['payment_method']])) { 261 $json['error'] = $this->language->get('error_method'); 262 } 263 264 if (!$json) { 265 $this->session->data['payment_method'] = $this->session->data['payment_methods'][$this->request->post['payment_method']]; 266 267 $json['success'] = $this->language->get('text_method'); 268 } 269 } 270 271 $this->response->addHeader('Content-Type: application/json'); 272 $this->response->setOutput(json_encode($json)); 273 } 274 }