1<?php
2/**
3 * 2007-2016 PrestaShop
4 *
5 * thirty bees is an extension to the PrestaShop e-commerce software developed by PrestaShop SA
6 * Copyright (C) 2017-2018 thirty bees
7 *
8 * NOTICE OF LICENSE
9 *
10 * This source file is subject to the Open Software License (OSL 3.0)
11 * that is bundled with this package in the file LICENSE.txt.
12 * It is also available through the world-wide-web at this URL:
13 * http://opensource.org/licenses/osl-3.0.php
14 * If you did not receive a copy of the license and are unable to
15 * obtain it through the world-wide-web, please send an email
16 * to license@thirtybees.com so we can send you a copy immediately.
17 *
18 * DISCLAIMER
19 *
20 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
21 * versions in the future. If you wish to customize PrestaShop for your
22 * needs please refer to https://www.thirtybees.com for more information.
23 *
24 * @author    thirty bees <contact@thirtybees.com>
25 * @author    PrestaShop SA <contact@prestashop.com>
26 * @copyright 2017-2018 thirty bees
27 * @copyright 2007-2016 PrestaShop SA
28 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
29 *  PrestaShop is an internationally registered trademark & property of PrestaShop SA
30 */
31
32/**
33 * Class OrderConfirmationControllerCore
34 *
35 * @since 1.0.0
36 */
37class OrderConfirmationControllerCore extends FrontController
38{
39    // @codingStandardsIgnoreStart
40    /** @var bool $ssl */
41    public $ssl = true;
42    /** @var string $php_self */
43    public $php_self = 'order-confirmation';
44    /** @var int $id_cart */
45    public $id_cart;
46    /** @var int $id_module */
47    public $id_module;
48    /** @var int $id_order */
49    public $id_order;
50    /** @var string $reference */
51    public $reference;
52    /** @var string $secure_key */
53    public $secure_key;
54    // @codingStandardsIgnoreEnd
55
56    /**
57     * Initialize order confirmation controller
58     *
59     * @see   FrontController::init()
60     *
61     * @return void
62     *
63     * @since 1.0.0
64     */
65    public function init()
66    {
67        parent::init();
68
69        $this->id_cart = (int) Tools::getValue('id_cart', 0);
70        $isGuest = false;
71
72        /* check if the cart has been made by a Guest customer, for redirect link */
73        if (Cart::isGuestCartByCartId($this->id_cart)) {
74            $isGuest = true;
75            $redirectLink = 'index.php?controller=guest-tracking';
76        } else {
77            $redirectLink = 'index.php?controller=history';
78        }
79
80        $this->id_module = (int) (Tools::getValue('id_module', 0));
81        $this->id_order = Order::getOrderByCartId((int) ($this->id_cart));
82        $this->secure_key = Tools::getValue('key', false);
83        $order = new Order((int) ($this->id_order));
84        if ($isGuest) {
85            $customer = new Customer((int) $order->id_customer);
86            $redirectLink .= '&id_order='.$order->reference.'&email='.urlencode($customer->email);
87        }
88        if (!$this->id_order || !$this->id_module || !$this->secure_key || empty($this->secure_key)) {
89            Tools::redirect($redirectLink.(Tools::isSubmit('slowvalidation') ? '&slowvalidation' : ''));
90        }
91        $this->reference = $order->reference;
92        if (!Validate::isLoadedObject($order) || $order->id_customer != $this->context->customer->id || $this->secure_key != $order->secure_key) {
93            Tools::redirect($redirectLink);
94        }
95        $module = Module::getInstanceById((int) ($this->id_module));
96        if ($order->module != $module->name) {
97            Tools::redirect($redirectLink);
98        }
99    }
100
101    /**
102     * Assign template vars related to page content
103     *
104     * @see   FrontController::initContent()
105     *
106     * @return void
107     *
108     * @since 1.0.0
109     */
110    public function initContent()
111    {
112        parent::initContent();
113
114        /* variables available in the custom scripts:
115            - list of products with few info
116            - products total
117            - shipping total
118            - total amount
119        */
120
121        $idCart = (int) Tools::getValue('id_cart');
122        $idOrder = Order::getOrderByCartId($idCart);
123        $order = new Order($idOrder);
124        $varProducts = [];
125
126        if (Validate::isLoadedObject($order)) {
127            $products = $order->getProducts();
128            if ($products) {
129                foreach ($products as $product) {
130                    $varProducts[] = [
131                        'id_product' => $product['id_product'],
132                        'name'       => $product['product_name'],
133                        'price'      => $product['product_price'],
134                        'quantity'   => $product['product_quantity'],
135                    ];
136                }
137            }
138        }
139
140        Media::AddJsDef(
141            [
142                'bought_products'          => $varProducts,
143                'total_products_tax_incl'  => $order->total_products_wt,
144                'total_products_tax_excl'  => $order->total_products,
145                'total_shipping_tax_incl'  => $order->total_shipping_tax_incl,
146                'total_shipping_tax_excl'  => $order->total_shipping_tax_excl,
147                'total_discounts_tax_incl' => $order->total_discounts_tax_incl,
148                'total_discounts_tax_excl' => $order->total_discounts_tax_excl,
149                'total_paid_tax_incl'      => $order->total_paid_tax_incl,
150                'total_paid_tax_excl'      => $order->total_paid_tax_excl,
151                'id_customer'              => $this->context->customer->id,
152            ]
153        );
154
155        $this->context->smarty->assign(
156            [
157                'is_guest'                => $this->context->customer->is_guest,
158                'HOOK_ORDER_CONFIRMATION' => $this->displayOrderConfirmation(),
159                'HOOK_PAYMENT_RETURN'     => $this->displayPaymentReturn(),
160            ]
161        );
162
163        if ($this->context->customer->is_guest) {
164            $this->context->smarty->assign(
165                [
166                    'id_order'           => $this->id_order,
167                    'reference_order'    => $this->reference,
168                    'id_order_formatted' => sprintf('#%06d', $this->id_order),
169                    'email'              => $this->context->customer->email,
170                ]
171            );
172            /* If guest we clear the cookie for security reason */
173            $this->context->customer->mylogout();
174        }
175
176        $this->setTemplate(_PS_THEME_DIR_.'order-confirmation.tpl');
177    }
178
179    /**
180     * Execute the hook displayOrderConfirmation
181     *
182     * @return string|array|false
183     *
184     * @since 1.0.0
185     */
186    public function displayOrderConfirmation()
187    {
188        if (Validate::isUnsignedId($this->id_order)) {
189            $params = [];
190            $order = new Order($this->id_order);
191            $currency = new Currency($order->id_currency);
192
193            if (Validate::isLoadedObject($order)) {
194                $params['total_to_pay'] = $order->getOrdersTotalPaid();
195                $params['currency'] = $currency->sign;
196                $params['objOrder'] = $order;
197                $params['currencyObj'] = $currency;
198
199                return Hook::exec('displayOrderConfirmation', $params);
200            }
201        }
202
203        return false;
204    }
205
206    /**
207     * Execute the hook displayPaymentReturn
208     *
209     * @return string|array|false
210     *
211     * @since 1.0.0
212     */
213    public function displayPaymentReturn()
214    {
215        if (Validate::isUnsignedId($this->id_order) && Validate::isUnsignedId($this->id_module)) {
216            $params = [];
217            $order = new Order($this->id_order);
218            $currency = new Currency($order->id_currency);
219
220            if (Validate::isLoadedObject($order)) {
221                $params['total_to_pay'] = $order->getOrdersTotalPaid();
222                $params['currency'] = $currency->sign;
223                $params['objOrder'] = $order;
224                $params['currencyObj'] = $currency;
225
226                return Hook::exec('displayPaymentReturn', $params, $this->id_module);
227            }
228        }
229
230        return false;
231    }
232}
233