1: <?php
2: namespace Opencart\Catalog\Controller\Api\Sale;
3: /**
4: * Class Payment Address
5: *
6: * @package Opencart\Catalog\Controller\Api\Sale
7: */
8: class PaymentAddress extends \Opencart\System\Engine\Controller {
9: /**
10: * @return void
11: */
12: public function index(): void {
13: $this->load->language('api/sale/payment_address');
14:
15: $json = [];
16:
17: // Add keys for missing post vars
18: $keys = [
19: 'firstname',
20: 'lastname',
21: 'company',
22: 'address_1',
23: 'address_2',
24: 'postcode',
25: 'city',
26: 'zone_id',
27: 'country_id'
28: ];
29:
30: foreach ($keys as $key) {
31: if (!isset($this->request->post[$key])) {
32: $this->request->post[$key] = '';
33: }
34: }
35:
36: if ((oc_strlen($this->request->post['firstname']) < 1) || (oc_strlen($this->request->post['firstname']) > 32)) {
37: $json['error']['firstname'] = $this->language->get('error_firstname');
38: }
39:
40: if ((oc_strlen($this->request->post['lastname']) < 1) || (oc_strlen($this->request->post['lastname']) > 32)) {
41: $json['error']['lastname'] = $this->language->get('error_lastname');
42: }
43:
44: if ((oc_strlen($this->request->post['address_1']) < 3) || (oc_strlen($this->request->post['address_1']) > 128)) {
45: $json['error']['address_1'] = $this->language->get('error_address_1');
46: }
47:
48: if ((oc_strlen($this->request->post['city']) < 2) || (oc_strlen($this->request->post['city']) > 128)) {
49: $json['error']['city'] = $this->language->get('error_city');
50: }
51:
52: $this->load->model('localisation/country');
53:
54: $country_info = $this->model_localisation_country->getCountry((int)$this->request->post['country_id']);
55:
56: if ($country_info && $country_info['postcode_required'] && (oc_strlen($this->request->post['postcode']) < 2 || oc_strlen($this->request->post['postcode']) > 10)) {
57: $json['error']['postcode'] = $this->language->get('error_postcode');
58: }
59:
60: if (!$country_info || $this->request->post['country_id'] == '') {
61: $json['error']['country'] = $this->language->get('error_country');
62: }
63:
64: if ($this->request->post['zone_id'] == '') {
65: $json['error']['zone'] = $this->language->get('error_zone');
66: }
67:
68: // Custom field validation
69: $this->load->model('account/custom_field');
70:
71: $custom_fields = $this->model_account_custom_field->getCustomFields((int)$this->config->get('config_customer_group_id'));
72:
73: foreach ($custom_fields as $custom_field) {
74: if ($custom_field['location'] == 'address') {
75: if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
76: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
77: } elseif (($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !preg_match(html_entity_decode($custom_field['validation'], ENT_QUOTES, 'UTF-8'), $this->request->post['custom_field'][$custom_field['custom_field_id']])) {
78: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
79: }
80: }
81: }
82:
83: if (!$json) {
84: if ($country_info) {
85: $country = $country_info['name'];
86: $iso_code_2 = $country_info['iso_code_2'];
87: $iso_code_3 = $country_info['iso_code_3'];
88: $address_format = $country_info['address_format'];
89: } else {
90: $country = '';
91: $iso_code_2 = '';
92: $iso_code_3 = '';
93: $address_format = '';
94: }
95:
96: $this->load->model('localisation/zone');
97:
98: $zone_info = $this->model_localisation_zone->getZone($this->request->post['zone_id']);
99:
100: if ($zone_info) {
101: $zone = $zone_info['name'];
102: $zone_code = $zone_info['code'];
103: } else {
104: $zone = '';
105: $zone_code = '';
106: }
107:
108: $this->session->data['payment_address'] = [
109: 'address_id' => $this->request->post['payment_address_id'],
110: 'firstname' => $this->request->post['firstname'],
111: 'lastname' => $this->request->post['lastname'],
112: 'company' => $this->request->post['company'],
113: 'address_1' => $this->request->post['address_1'],
114: 'address_2' => $this->request->post['address_2'],
115: 'postcode' => $this->request->post['postcode'],
116: 'city' => $this->request->post['city'],
117: 'zone_id' => $this->request->post['zone_id'],
118: 'zone' => $zone,
119: 'zone_code' => $zone_code,
120: 'country_id' => (int)$this->request->post['country_id'],
121: 'country' => $country,
122: 'iso_code_2' => $iso_code_2,
123: 'iso_code_3' => $iso_code_3,
124: 'address_format' => $address_format,
125: 'custom_field' => $this->request->post['custom_field'] ?? []
126: ];
127:
128: $json['success'] = $this->language->get('text_success');
129: }
130:
131: $this->response->addHeader('Content-Type: application/json');
132: $this->response->setOutput(json_encode($json));
133: }
134: }
135: