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

/catalog/controller/api/payment.php

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