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

/catalog/controller/api/shipping.php

https://gitlab.com/reclamare/mao
PHP | 268 lines | 207 code | 53 blank | 8 comment | 52 complexity | a055b5afde89f6ee7e509f77e1e86a50 MD5 | raw file
  1. <?php
  2. class ControllerApiShipping extends Controller {
  3. public function address() {
  4. $this->load->language('api/shipping');
  5. // Delete old shipping address, shipping methods and method so not to cause any issues if there is an error
  6. unset($this->session->data['shipping_address']);
  7. unset($this->session->data['shipping_methods']);
  8. unset($this->session->data['shipping_method']);
  9. $json = array();
  10. if ($this->cart->hasShipping()) {
  11. if (!isset($this->session->data['api_id'])) {
  12. $json['error']['warning'] = $this->language->get('error_permission');
  13. } else {
  14. // Add keys for missing post vars
  15. $keys = array(
  16. 'firstname',
  17. 'lastname',
  18. 'company',
  19. 'address_1',
  20. 'address_2',
  21. 'postcode',
  22. 'city',
  23. 'zone_id',
  24. 'country_id'
  25. );
  26. foreach ($keys as $key) {
  27. if (!isset($this->request->post[$key])) {
  28. $this->request->post[$key] = '';
  29. }
  30. }
  31. if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) {
  32. $json['error']['firstname'] = $this->language->get('error_firstname');
  33. }
  34. if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) {
  35. $json['error']['lastname'] = $this->language->get('error_lastname');
  36. }
  37. if ((utf8_strlen(trim($this->request->post['address_1'])) < 3) || (utf8_strlen(trim($this->request->post['address_1'])) > 128)) {
  38. $json['error']['address_1'] = $this->language->get('error_address_1');
  39. }
  40. if ((utf8_strlen($this->request->post['city']) < 2) || (utf8_strlen($this->request->post['city']) > 32)) {
  41. $json['error']['city'] = $this->language->get('error_city');
  42. }
  43. $this->load->model('localisation/country');
  44. $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
  45. if ($country_info && $country_info['postcode_required'] && (utf8_strlen(trim($this->request->post['postcode'])) < 2 || utf8_strlen(trim($this->request->post['postcode'])) > 10)) {
  46. $json['error']['postcode'] = $this->language->get('error_postcode');
  47. }
  48. if ($this->request->post['country_id'] == '') {
  49. $json['error']['country'] = $this->language->get('error_country');
  50. }
  51. if (!isset($this->request->post['zone_id']) || $this->request->post['zone_id'] == '') {
  52. $json['error']['zone'] = $this->language->get('error_zone');
  53. }
  54. // Custom field validation
  55. $this->load->model('account/custom_field');
  56. $custom_fields = $this->model_account_custom_field->getCustomFields($this->config->get('config_customer_group_id'));
  57. foreach ($custom_fields as $custom_field) {
  58. if (($custom_field['location'] == 'address') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
  59. $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
  60. }
  61. }
  62. if (!$json) {
  63. $this->load->model('localisation/country');
  64. $country_info = $this->model_localisation_country->getCountry($this->request->post['country_id']);
  65. if ($country_info) {
  66. $country = $country_info['name'];
  67. $iso_code_2 = $country_info['iso_code_2'];
  68. $iso_code_3 = $country_info['iso_code_3'];
  69. $address_format = $country_info['address_format'];
  70. } else {
  71. $country = '';
  72. $iso_code_2 = '';
  73. $iso_code_3 = '';
  74. $address_format = '';
  75. }
  76. $this->load->model('localisation/zone');
  77. $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
  78. if ($zone_info) {
  79. $zone = $zone_info['name'];
  80. $zone_code = $zone_info['code'];
  81. } else {
  82. $zone = '';
  83. $zone_code = '';
  84. }
  85. $this->session->data['shipping_address'] = array(
  86. 'firstname' => $this->request->post['firstname'],
  87. 'lastname' => $this->request->post['lastname'],
  88. 'company' => $this->request->post['company'],
  89. 'address_1' => $this->request->post['address_1'],
  90. 'address_2' => $this->request->post['address_2'],
  91. 'postcode' => $this->request->post['postcode'],
  92. 'city' => $this->request->post['city'],
  93. 'zone_id' => $this->request->post['zone_id'],
  94. 'zone' => $zone,
  95. 'zone_code' => $zone_code,
  96. 'country_id' => $this->request->post['country_id'],
  97. 'country' => $country,
  98. 'iso_code_2' => $iso_code_2,
  99. 'iso_code_3' => $iso_code_3,
  100. 'address_format' => $address_format,
  101. 'custom_field' => isset($this->request->post['custom_field']) ? $this->request->post['custom_field'] : array()
  102. );
  103. $json['success'] = $this->language->get('text_address');
  104. unset($this->session->data['shipping_method']);
  105. unset($this->session->data['shipping_methods']);
  106. }
  107. }
  108. }
  109. if (isset($this->request->server['HTTP_ORIGIN'])) {
  110. $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
  111. $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
  112. $this->response->addHeader('Access-Control-Max-Age: 1000');
  113. $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
  114. }
  115. $this->response->addHeader('Content-Type: application/json');
  116. $this->response->setOutput(json_encode($json));
  117. }
  118. public function methods() {
  119. $this->load->language('api/shipping');
  120. // Delete past shipping methods and method just in case there is an error
  121. unset($this->session->data['shipping_methods']);
  122. unset($this->session->data['shipping_method']);
  123. $json = array();
  124. if (!isset($this->session->data['api_id'])) {
  125. $json['error'] = $this->language->get('error_permission');
  126. } elseif ($this->cart->hasShipping()) {
  127. if (!isset($this->session->data['shipping_address'])) {
  128. $json['error'] = $this->language->get('error_address');
  129. }
  130. if (!$json) {
  131. // Shipping Methods
  132. $json['shipping_methods'] = array();
  133. $this->load->model('extension/extension');
  134. $results = $this->model_extension_extension->getExtensions('shipping');
  135. foreach ($results as $result) {
  136. if ($this->config->get($result['code'] . '_status')) {
  137. $this->load->model('shipping/' . $result['code']);
  138. $quote = $this->{'model_shipping_' . $result['code']}->getQuote($this->session->data['shipping_address']);
  139. if ($quote) {
  140. $json['shipping_methods'][$result['code']] = array(
  141. 'title' => $quote['title'],
  142. 'quote' => $quote['quote'],
  143. 'sort_order' => $quote['sort_order'],
  144. 'error' => $quote['error']
  145. );
  146. }
  147. }
  148. }
  149. $sort_order = array();
  150. foreach ($json['shipping_methods'] as $key => $value) {
  151. $sort_order[$key] = $value['sort_order'];
  152. }
  153. array_multisort($sort_order, SORT_ASC, $json['shipping_methods']);
  154. if ($json['shipping_methods']) {
  155. $this->session->data['shipping_methods'] = $json['shipping_methods'];
  156. } else {
  157. $json['error'] = $this->language->get('error_no_shipping');
  158. }
  159. }
  160. } else {
  161. $json['shipping_methods'] = array();
  162. }
  163. if (isset($this->request->server['HTTP_ORIGIN'])) {
  164. $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
  165. $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
  166. $this->response->addHeader('Access-Control-Max-Age: 1000');
  167. $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
  168. }
  169. $this->response->addHeader('Content-Type: application/json');
  170. $this->response->setOutput(json_encode($json));
  171. }
  172. public function method() {
  173. $this->load->language('api/shipping');
  174. // Delete old shipping method so not to cause any issues if there is an error
  175. unset($this->session->data['shipping_method']);
  176. $json = array();
  177. if (!isset($this->session->data['api_id'])) {
  178. $json['error'] = $this->language->get('error_permission');
  179. } else {
  180. if ($this->cart->hasShipping()) {
  181. // Shipping Address
  182. if (!isset($this->session->data['shipping_address'])) {
  183. $json['error'] = $this->language->get('error_address');
  184. }
  185. // Shipping Method
  186. if (empty($this->session->data['shipping_methods'])) {
  187. $json['error'] = $this->language->get('error_no_shipping');
  188. } elseif (!isset($this->request->post['shipping_method'])) {
  189. $json['error'] = $this->language->get('error_method');
  190. } else {
  191. $shipping = explode('.', $this->request->post['shipping_method']);
  192. if (!isset($shipping[0]) || !isset($shipping[1]) || !isset($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]])) {
  193. $json['error'] = $this->language->get('error_method');
  194. }
  195. }
  196. if (!$json) {
  197. $this->session->data['shipping_method'] = $this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[1]];
  198. $json['success'] = $this->language->get('text_method');
  199. }
  200. } else {
  201. unset($this->session->data['shipping_address']);
  202. unset($this->session->data['shipping_method']);
  203. unset($this->session->data['shipping_methods']);
  204. }
  205. }
  206. if (isset($this->request->server['HTTP_ORIGIN'])) {
  207. $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
  208. $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
  209. $this->response->addHeader('Access-Control-Max-Age: 1000');
  210. $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
  211. }
  212. $this->response->addHeader('Content-Type: application/json');
  213. $this->response->setOutput(json_encode($json));
  214. }
  215. }