1: <?php
2: namespace Opencart\Catalog\Controller\Checkout;
3: /**
4: * Class PaymentAddress
5: *
6: * @package Opencart\Catalog\Controller\Checkout
7: */
8: class PaymentAddress extends \Opencart\System\Engine\Controller {
9: /**
10: * @return string
11: */
12: public function index(): string {
13: $this->load->language('checkout/payment_address');
14:
15: $data['error_upload_size'] = sprintf($this->language->get('error_upload_size'), $this->config->get('config_file_max_size'));
16: $data['config_file_max_size'] = ((int)$this->config->get('config_file_max_size') * 1024 * 1024);
17:
18: $this->session->data['upload_token'] = oc_token(32);
19:
20: $data['upload'] = $this->url->link('tool/upload', 'language=' . $this->config->get('config_language') . '&upload_token=' . $this->session->data['upload_token']);
21:
22: $this->load->model('account/address');
23:
24: $data['addresses'] = $this->model_account_address->getAddresses($this->customer->getId());
25:
26: if (isset($this->session->data['payment_address']['address_id'])) {
27: $data['address_id'] = $this->session->data['payment_address']['address_id'];
28: } else {
29: $data['address_id'] = 0;
30: }
31:
32: $this->load->model('localisation/country');
33:
34: $data['countries'] = $this->model_localisation_country->getCountries();
35:
36: // Custom Fields
37: $data['custom_fields'] = [];
38:
39: $this->load->model('account/custom_field');
40:
41: $custom_fields = $this->model_account_custom_field->getCustomFields($this->customer->getGroupId());
42:
43: foreach ($custom_fields as $custom_field) {
44: if ($custom_field['location'] == 'address') {
45: $data['custom_fields'][] = $custom_field;
46: }
47: }
48:
49: $data['language'] = $this->config->get('config_language');
50:
51: return $this->load->view('checkout/payment_address', $data);
52: }
53:
54: /**
55: * Save
56: *
57: * @return void
58: */
59: public function save(): void {
60: $this->load->language('checkout/payment_address');
61:
62: $json = [];
63:
64: // Validate cart has products and has stock.
65: if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
66: $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
67: }
68:
69: // Validate minimum quantity requirements.
70: $products = $this->cart->getProducts();
71:
72: foreach ($products as $product) {
73: if (!$product['minimum']) {
74: $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
75:
76: break;
77: }
78: }
79:
80: // Validate if customer is logged in or customer session data is not set
81: if (!$this->customer->isLogged() || !isset($this->session->data['customer'])) {
82: $json['redirect'] = $this->url->link('account/login', 'language=' . $this->config->get('config_language'), true);
83: }
84:
85: // Validate if payment address is set if required in settings
86: if (!$this->config->get('config_checkout_payment_address')) {
87: $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
88: }
89:
90: if (!$json) {
91: $keys = [
92: 'firstname',
93: 'lastname',
94: 'company',
95: 'address_1',
96: 'address_2',
97: 'city',
98: 'postcode',
99: 'country_id',
100: 'zone_id',
101: 'custom_field'
102: ];
103:
104: foreach ($keys as $key) {
105: if (!isset($this->request->post[$key])) {
106: $this->request->post[$key] = '';
107: }
108: }
109:
110: if ((oc_strlen($this->request->post['firstname']) < 1) || (oc_strlen($this->request->post['firstname']) > 32)) {
111: $json['error']['firstname'] = $this->language->get('error_firstname');
112: }
113:
114: if ((oc_strlen($this->request->post['lastname']) < 1) || (oc_strlen($this->request->post['lastname']) > 32)) {
115: $json['error']['lastname'] = $this->language->get('error_lastname');
116: }
117:
118: if ((oc_strlen($this->request->post['address_1']) < 3) || (oc_strlen($this->request->post['address_1']) > 128)) {
119: $json['error']['address_1'] = $this->language->get('error_address_1');
120: }
121:
122: if ((oc_strlen($this->request->post['city']) < 2) || (oc_strlen($this->request->post['city']) > 128)) {
123: $json['error']['city'] = $this->language->get('error_city');
124: }
125:
126: $this->load->model('localisation/country');
127:
128: $country_info = $this->model_localisation_country->getCountry((int)$this->request->post['country_id']);
129:
130: if ($country_info && $country_info['postcode_required'] && (oc_strlen($this->request->post['postcode']) < 2 || oc_strlen($this->request->post['postcode']) > 10)) {
131: $json['error']['postcode'] = $this->language->get('error_postcode');
132: }
133:
134: if (!$country_info || $this->request->post['country_id'] == '') {
135: $json['error']['country'] = $this->language->get('error_country');
136: }
137:
138: if (!$country_info || $this->request->post['zone_id'] == '') {
139: $json['error']['zone'] = $this->language->get('error_zone');
140: }
141:
142: // Custom field validation
143: $this->load->model('account/custom_field');
144:
145: $custom_fields = $this->model_account_custom_field->getCustomFields($this->customer->getGroupId());
146:
147: foreach ($custom_fields as $custom_field) {
148: if ($custom_field['location'] == 'address') {
149: if ($custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
150: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
151: } 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']])) {
152: $json['error']['custom_field_' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_regex'), $custom_field['name']);
153: }
154: }
155: }
156: }
157:
158: if (!$json) {
159: // If no default address add it
160: $address_id = $this->customer->getAddressId();
161:
162: if (!$address_id) {
163: $this->request->post['default'] = 1;
164: }
165:
166: $this->load->model('account/address');
167:
168: $json['address_id'] = $this->model_account_address->addAddress($this->customer->getId(), $this->request->post);
169:
170: $json['addresses'] = $this->model_account_address->getAddresses($this->customer->getId());
171:
172: $this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getId(), $json['address_id']);
173:
174: $json['success'] = $this->language->get('text_success');
175:
176: // Clear payment and shipping methods
177: unset($this->session->data['shipping_method']);
178: unset($this->session->data['shipping_methods']);
179: unset($this->session->data['payment_method']);
180: unset($this->session->data['payment_methods']);
181: }
182:
183: $this->response->addHeader('Content-Type: application/json');
184: $this->response->setOutput(json_encode($json));
185: }
186:
187: /**
188: * Address
189: *
190: * @return void
191: */
192: public function address(): void {
193: $this->load->language('checkout/payment_address');
194:
195: $json = [];
196:
197: if (isset($this->request->get['address_id'])) {
198: $address_id = (int)$this->request->get['address_id'];
199: } else {
200: $address_id = 0;
201: }
202:
203: if (!isset($this->session->data['customer'])) {
204: $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
205: }
206:
207: // Validate cart has products and has stock.
208: if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
209: $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
210: }
211:
212: // Validate minimum quantity requirements.
213: $products = $this->cart->getProducts();
214:
215: foreach ($products as $product) {
216: if (!$product['minimum']) {
217: $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
218:
219: break;
220: }
221: }
222:
223: // Validate if customer is logged in or customer session data is not set
224: if (!$this->customer->isLogged() || !isset($this->session->data['customer'])) {
225: $json['redirect'] = $this->url->link('account/login', 'language=' . $this->config->get('config_language'), true);
226: }
227:
228: // Validate if payment address is set if required in settings
229: if (!$this->config->get('config_checkout_payment_address')) {
230: $json['redirect'] = $this->url->link('checkout/cart', 'language=' . $this->config->get('config_language'), true);
231: }
232:
233: if (!$json) {
234: $this->load->model('account/address');
235:
236: $address_info = $this->model_account_address->getAddress($this->customer->getId(), $address_id);
237:
238: if (!$address_info) {
239: $json['error'] = $this->language->get('error_address');
240:
241: unset($this->session->data['payment_address']);
242: unset($this->session->data['shipping_method']);
243: unset($this->session->data['shipping_methods']);
244: unset($this->session->data['payment_method']);
245: unset($this->session->data['payment_methods']);
246: }
247: }
248:
249: if (!$json) {
250: $this->session->data['payment_address'] = $address_info;
251:
252: $json['success'] = $this->language->get('text_success');
253:
254: // Clear payment and shipping methods
255: unset($this->session->data['shipping_method']);
256: unset($this->session->data['shipping_methods']);
257: unset($this->session->data['payment_method']);
258: unset($this->session->data['payment_methods']);
259: }
260:
261: $this->response->addHeader('Content-Type: application/json');
262: $this->response->setOutput(json_encode($json));
263: }
264: }
265: