1: <?php
2: namespace Opencart\Catalog\Controller\Checkout;
3: /**
4: * Class Confirm
5: *
6: * @package Opencart\Catalog\Controller\Checkout
7: */
8: class Confirm extends \Opencart\System\Engine\Controller {
9: /**
10: * @return string
11: */
12: public function index(): string {
13: $this->load->language('checkout/confirm');
14:
15: // Order Totals
16: $totals = [];
17: $taxes = $this->cart->getTaxes();
18: $total = 0;
19:
20: $this->load->model('checkout/cart');
21:
22: ($this->model_checkout_cart->getTotals)($totals, $taxes, $total);
23:
24: $status = ($this->customer->isLogged() || !$this->config->get('config_customer_price'));
25:
26: // Validate customer data is set
27: if (!isset($this->session->data['customer'])) {
28: $status = false;
29: }
30:
31: // Validate cart has products and has stock.
32: if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
33: $status = false;
34: }
35:
36: // Validate minimum quantity requirements.
37: $products = $this->model_checkout_cart->getProducts();
38:
39: foreach ($products as $product) {
40: if (!$product['minimum']) {
41: $status = false;
42:
43: break;
44: }
45: }
46:
47: // Shipping
48: if ($this->cart->hasShipping()) {
49: // Validate shipping address
50: if (!isset($this->session->data['shipping_address']['address_id'])) {
51: $status = false;
52: }
53:
54: // Validate shipping method
55: if (!isset($this->session->data['shipping_method'])) {
56: $status = false;
57: }
58: } else {
59: unset($this->session->data['shipping_address']);
60: unset($this->session->data['shipping_method']);
61: unset($this->session->data['shipping_methods']);
62: }
63:
64: // Validate has payment address if required
65: if ($this->config->get('config_checkout_payment_address') && !isset($this->session->data['payment_address'])) {
66: $status = false;
67: }
68:
69: // Validate payment methods
70: if (!isset($this->session->data['payment_method'])) {
71: $status = false;
72: }
73:
74: // Validate checkout terms
75: if ($this->config->get('config_checkout_id') && empty($this->session->data['agree'])) {
76: $status = false;
77: }
78:
79: // Generate order if payment method is set
80: if ($status) {
81: $order_data = [];
82:
83: $order_data['invoice_prefix'] = $this->config->get('config_invoice_prefix');
84:
85: // Store Details
86: $order_data['store_id'] = $this->config->get('config_store_id');
87: $order_data['store_name'] = $this->config->get('config_name');
88: $order_data['store_url'] = $this->config->get('config_url');
89:
90: // Customer Details
91: $order_data['customer_id'] = $this->session->data['customer']['customer_id'];
92: $order_data['customer_group_id'] = $this->session->data['customer']['customer_group_id'];
93: $order_data['firstname'] = $this->session->data['customer']['firstname'];
94: $order_data['lastname'] = $this->session->data['customer']['lastname'];
95: $order_data['email'] = $this->session->data['customer']['email'];
96: $order_data['telephone'] = $this->session->data['customer']['telephone'];
97: $order_data['custom_field'] = $this->session->data['customer']['custom_field'];
98:
99: // Payment Details
100: if ($this->config->get('config_checkout_payment_address')) {
101: $order_data['payment_address_id'] = $this->session->data['payment_address']['address_id'];
102: $order_data['payment_firstname'] = $this->session->data['payment_address']['firstname'];
103: $order_data['payment_lastname'] = $this->session->data['payment_address']['lastname'];
104: $order_data['payment_company'] = $this->session->data['payment_address']['company'];
105: $order_data['payment_address_1'] = $this->session->data['payment_address']['address_1'];
106: $order_data['payment_address_2'] = $this->session->data['payment_address']['address_2'];
107: $order_data['payment_city'] = $this->session->data['payment_address']['city'];
108: $order_data['payment_postcode'] = $this->session->data['payment_address']['postcode'];
109: $order_data['payment_zone'] = $this->session->data['payment_address']['zone'];
110: $order_data['payment_zone_id'] = $this->session->data['payment_address']['zone_id'];
111: $order_data['payment_country'] = $this->session->data['payment_address']['country'];
112: $order_data['payment_country_id'] = $this->session->data['payment_address']['country_id'];
113: $order_data['payment_address_format'] = $this->session->data['payment_address']['address_format'];
114: $order_data['payment_custom_field'] = $this->session->data['payment_address']['custom_field'] ?? [];
115: } else {
116: $order_data['payment_address_id'] = 0;
117: $order_data['payment_firstname'] = '';
118: $order_data['payment_lastname'] = '';
119: $order_data['payment_company'] = '';
120: $order_data['payment_address_1'] = '';
121: $order_data['payment_address_2'] = '';
122: $order_data['payment_city'] = '';
123: $order_data['payment_postcode'] = '';
124: $order_data['payment_zone'] = '';
125: $order_data['payment_zone_id'] = 0;
126: $order_data['payment_country'] = '';
127: $order_data['payment_country_id'] = 0;
128: $order_data['payment_address_format'] = '';
129: $order_data['payment_custom_field'] = [];
130: }
131:
132: $order_data['payment_method'] = $this->session->data['payment_method'];
133:
134: // Shipping Details
135: if ($this->cart->hasShipping()) {
136: $order_data['shipping_address_id'] = $this->session->data['shipping_address']['address_id'];
137: $order_data['shipping_firstname'] = $this->session->data['shipping_address']['firstname'];
138: $order_data['shipping_lastname'] = $this->session->data['shipping_address']['lastname'];
139: $order_data['shipping_company'] = $this->session->data['shipping_address']['company'];
140: $order_data['shipping_address_1'] = $this->session->data['shipping_address']['address_1'];
141: $order_data['shipping_address_2'] = $this->session->data['shipping_address']['address_2'];
142: $order_data['shipping_city'] = $this->session->data['shipping_address']['city'];
143: $order_data['shipping_postcode'] = $this->session->data['shipping_address']['postcode'];
144: $order_data['shipping_zone'] = $this->session->data['shipping_address']['zone'];
145: $order_data['shipping_zone_id'] = $this->session->data['shipping_address']['zone_id'];
146: $order_data['shipping_country'] = $this->session->data['shipping_address']['country'];
147: $order_data['shipping_country_id'] = $this->session->data['shipping_address']['country_id'];
148: $order_data['shipping_address_format'] = $this->session->data['shipping_address']['address_format'];
149: $order_data['shipping_custom_field'] = $this->session->data['shipping_address']['custom_field'] ?? [];
150:
151: $order_data['shipping_method'] = $this->session->data['shipping_method'];
152: } else {
153: $order_data['shipping_address_id'] = 0;
154: $order_data['shipping_firstname'] = '';
155: $order_data['shipping_lastname'] = '';
156: $order_data['shipping_company'] = '';
157: $order_data['shipping_address_1'] = '';
158: $order_data['shipping_address_2'] = '';
159: $order_data['shipping_city'] = '';
160: $order_data['shipping_postcode'] = '';
161: $order_data['shipping_zone'] = '';
162: $order_data['shipping_zone_id'] = 0;
163: $order_data['shipping_country'] = '';
164: $order_data['shipping_country_id'] = 0;
165: $order_data['shipping_address_format'] = '';
166: $order_data['shipping_custom_field'] = [];
167:
168: $order_data['shipping_method'] = [];
169: }
170:
171: if (isset($this->session->data['comment'])) {
172: $order_data['comment'] = $this->session->data['comment'];
173: } else {
174: $order_data['comment'] = '';
175: }
176:
177: $total_data = [
178: 'totals' => $totals,
179: 'taxes' => $taxes,
180: 'total' => $total
181: ];
182:
183: $order_data = array_merge($order_data, $total_data);
184:
185: $order_data['affiliate_id'] = 0;
186: $order_data['commission'] = 0;
187: $order_data['marketing_id'] = 0;
188: $order_data['tracking'] = '';
189:
190: if (isset($this->session->data['tracking'])) {
191: $subtotal = $this->cart->getSubTotal();
192:
193: // Affiliate
194: if ($this->config->get('config_affiliate_status')) {
195: $this->load->model('account/affiliate');
196:
197: $affiliate_info = $this->model_account_affiliate->getAffiliateByTracking($this->session->data['tracking']);
198:
199: if ($affiliate_info) {
200: $order_data['affiliate_id'] = $affiliate_info['customer_id'];
201: $order_data['commission'] = ($subtotal / 100) * $affiliate_info['commission'];
202: $order_data['tracking'] = $this->session->data['tracking'];
203: }
204: }
205:
206: $this->load->model('marketing/marketing');
207:
208: $marketing_info = $this->model_marketing_marketing->getMarketingByCode($this->session->data['tracking']);
209:
210: if ($marketing_info) {
211: $order_data['marketing_id'] = $marketing_info['marketing_id'];
212: $order_data['tracking'] = $this->session->data['tracking'];
213: }
214: }
215:
216: $order_data['language_id'] = $this->config->get('config_language_id');
217: $order_data['language_code'] = $this->config->get('config_language');
218:
219: $order_data['currency_id'] = $this->currency->getId($this->session->data['currency']);
220: $order_data['currency_code'] = $this->session->data['currency'];
221: $order_data['currency_value'] = $this->currency->getValue($this->session->data['currency']);
222:
223: $order_data['ip'] = $this->request->server['REMOTE_ADDR'];
224:
225: if (!empty($this->request->server['HTTP_X_FORWARDED_FOR'])) {
226: $order_data['forwarded_ip'] = $this->request->server['HTTP_X_FORWARDED_FOR'];
227: } elseif (!empty($this->request->server['HTTP_CLIENT_IP'])) {
228: $order_data['forwarded_ip'] = $this->request->server['HTTP_CLIENT_IP'];
229: } else {
230: $order_data['forwarded_ip'] = '';
231: }
232:
233: if (isset($this->request->server['HTTP_USER_AGENT'])) {
234: $order_data['user_agent'] = $this->request->server['HTTP_USER_AGENT'];
235: } else {
236: $order_data['user_agent'] = '';
237: }
238:
239: if (isset($this->request->server['HTTP_ACCEPT_LANGUAGE'])) {
240: $order_data['accept_language'] = $this->request->server['HTTP_ACCEPT_LANGUAGE'];
241: } else {
242: $order_data['accept_language'] = '';
243: }
244:
245: // Products
246: $order_data['products'] = [];
247:
248: foreach ($products as $product) {
249: $option_data = [];
250:
251: foreach ($product['option'] as $option) {
252: $option_data[] = [
253: 'product_option_id' => $option['product_option_id'],
254: 'product_option_value_id' => $option['product_option_value_id'],
255: 'option_id' => $option['option_id'],
256: 'option_value_id' => $option['option_value_id'],
257: 'name' => $option['name'],
258: 'value' => $option['value'],
259: 'type' => $option['type']
260: ];
261: }
262:
263: $subscription_data = [];
264:
265: if ($product['subscription']) {
266: $subscription_data = [
267: 'subscription_plan_id' => $product['subscription']['subscription_plan_id'],
268: 'name' => $product['subscription']['name'],
269: 'trial_price' => $product['subscription']['trial_price'],
270: 'trial_tax' => $this->tax->getTax($product['subscription']['trial_price'], $product['tax_class_id']),
271: 'trial_frequency' => $product['subscription']['trial_frequency'],
272: 'trial_cycle' => $product['subscription']['trial_cycle'],
273: 'trial_duration' => $product['subscription']['trial_duration'],
274: 'trial_remaining' => $product['subscription']['trial_remaining'],
275: 'trial_status' => $product['subscription']['trial_status'],
276: 'price' => $product['subscription']['price'],
277: 'tax' => $this->tax->getTax($product['subscription']['price'], $product['tax_class_id']),
278: 'frequency' => $product['subscription']['frequency'],
279: 'cycle' => $product['subscription']['cycle'],
280: 'duration' => $product['subscription']['duration']
281: ];
282: }
283:
284: $order_data['products'][] = [
285: 'product_id' => $product['product_id'],
286: 'master_id' => $product['master_id'],
287: 'name' => $product['name'],
288: 'model' => $product['model'],
289: 'option' => $option_data,
290: 'subscription' => $subscription_data,
291: 'download' => $product['download'],
292: 'quantity' => $product['quantity'],
293: 'subtract' => $product['subtract'],
294: 'price' => $product['price'],
295: 'total' => $product['total'],
296: 'tax' => $this->tax->getTax($product['price'], $product['tax_class_id']),
297: 'reward' => $product['reward']
298: ];
299: }
300:
301: // Gift Voucher
302: $order_data['vouchers'] = [];
303:
304: if (!empty($this->session->data['vouchers'])) {
305: $order_data['vouchers'] = $this->session->data['vouchers'];
306: }
307:
308: $this->load->model('checkout/order');
309:
310: if (!isset($this->session->data['order_id'])) {
311: $this->session->data['order_id'] = $this->model_checkout_order->addOrder($order_data);
312: } else {
313: $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
314:
315: if ($order_info && !$order_info['order_status_id']) {
316: $this->model_checkout_order->editOrder($this->session->data['order_id'], $order_data);
317: }
318: }
319: }
320:
321: // Display prices
322: if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
323: $price_status = true;
324: } else {
325: $price_status = false;
326: }
327:
328: $this->load->model('tool/upload');
329:
330: $data['products'] = [];
331:
332: foreach ($products as $product) {
333: if ($product['option']) {
334: foreach ($product['option'] as $key => $option) {
335: $product['option'][$key]['value'] = (oc_strlen($option['value']) > 20 ? oc_substr($option['value'], 0, 20) . '..' : $option['value']);
336: }
337: }
338:
339: $description = '';
340:
341: if ($product['subscription']) {
342: if ($product['subscription']['trial_status']) {
343: $trial_price = $this->currency->format($this->tax->calculate($product['subscription']['trial_price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
344: $trial_cycle = $product['subscription']['trial_cycle'];
345: $trial_frequency = $this->language->get('text_' . $product['subscription']['trial_frequency']);
346: $trial_duration = $product['subscription']['trial_duration'];
347:
348: $description .= sprintf($this->language->get('text_subscription_trial'), $trial_price, $trial_cycle, $trial_frequency, $trial_duration);
349: }
350:
351: $price = $this->currency->format($this->tax->calculate($product['subscription']['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
352: $cycle = $product['subscription']['cycle'];
353: $frequency = $this->language->get('text_' . $product['subscription']['frequency']);
354: $duration = $product['subscription']['duration'];
355:
356: if ($duration) {
357: $description .= sprintf($this->language->get('text_subscription_duration'), $price_status ? $price : '', $cycle, $frequency, $duration);
358: } else {
359: $description .= sprintf($this->language->get('text_subscription_cancel'), $price_status ? $price : '', $cycle, $frequency);
360: }
361: }
362:
363: $data['products'][] = [
364: 'cart_id' => $product['cart_id'],
365: 'product_id' => $product['product_id'],
366: 'name' => $product['name'],
367: 'model' => $product['model'],
368: 'option' => $product['option'],
369: 'subscription' => $description,
370: 'quantity' => $product['quantity'],
371: 'price' => $price_status ? $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']) : '',
372: 'total' => $price_status ? $this->currency->format($this->tax->calculate($product['total'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']) : '',
373: 'reward' => $product['reward'],
374: 'href' => $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $product['product_id'])
375: ];
376: }
377:
378: // Gift Voucher
379: $data['vouchers'] = [];
380:
381: $vouchers = $this->model_checkout_cart->getVouchers();
382:
383: foreach ($vouchers as $voucher) {
384: $data['vouchers'][] = [
385: 'description' => $voucher['description'],
386: 'amount' => $this->currency->format($voucher['amount'], $this->session->data['currency'])
387: ];
388: }
389:
390: $data['totals'] = [];
391:
392: foreach ($totals as $total) {
393: $data['totals'][] = [
394: 'title' => $total['title'],
395: 'text' => $this->currency->format($total['value'], $this->session->data['currency'])
396: ];
397: }
398:
399: // Validate if payment method has been set.
400: if (isset($this->session->data['payment_method'])) {
401: $code = oc_substr($this->session->data['payment_method']['code'], 0, strpos($this->session->data['payment_method']['code'], '.'));
402: } else {
403: $code = '';
404: }
405:
406: $extension_info = $this->model_setting_extension->getExtensionByCode('payment', $code);
407:
408: if ($status && $extension_info) {
409: $data['payment'] = $this->load->controller('extension/' . $extension_info['extension'] . '/payment/' . $extension_info['code']);
410: } else {
411: $data['payment'] = '';
412: }
413:
414: // Validate if payment method has been set.
415: return $this->load->view('checkout/confirm', $data);
416: }
417:
418: /**
419: * Confirm
420: *
421: * @return void
422: */
423: public function confirm(): void {
424: $this->response->setOutput($this->index());
425: }
426: }
427: