/**
  * Post process functionalities.
  *
  * @return 	this
  */
 public function postProcess()
 {
     // get session id
     $session = Tools::getValue('session');
     // if session is not valid
     if (!$session || !isset($session)) {
         Tools::redirect('/');
     }
     // get checkmeout configuration
     $configuration = unserialize(Configuration::get('CHECKMEOUT'));
     // redirect if configuration is not valid
     if (!isset($configuration['CHECKMEOUT_API_TOKEN'], $configuration['CHECKMEOUT_API_SECRET'], $configuration['CHECKMEOUT_REFERENCE'])) {
         Tools::redirect('/');
     }
     // get the token
     $token = $configuration['CHECKMEOUT_API_TOKEN'];
     // get the secret
     $secret = $configuration['CHECKMEOUT_API_SECRET'];
     // initialize base class
     $checkmeout = new CheckMeOutBase($token, $secret);
     // get checkout details
     $details = $checkmeout->getCheckoutDetails($session);
     // get the payment method
     $method = strtoupper($details['transaction_method']);
     // case what status should we used
     $status = Configuration::get('PS_OS_PREPARATION');
     // if method is asiapay
     if ($method == 'ASIAPAY' || $method == 'PAYPAL') {
         $status = Configuration::get('PS_OS_PAYMENT');
     }
     // get cart object
     $cart = $this->context->cart;
     // get customer object
     $customer = new Customer($cart->id_customer);
     // get currency object
     $currency = $this->context->currency;
     // get total cart valie
     $total = (double) $cart->getOrderTotal(true, Cart::BOTH);
     // additional email variables
     $mailVars = array();
     // if cart address is not set, it means this
     // customer is newly registered
     if (is_null($details['address_index']) || !isset($details['address_index']) || empty($details['address_index'])) {
         // create the address
         $address = $this->createAddress($cart, $customer, $details);
         // set delivery address
         $cart->id_address_delivery = $address['id_address'];
         // set invoice address
         $cart->id_address_invoice = $address['id_address'];
     } else {
         // get the address
         $address = $this->updateAddress($cart, $customer, $details);
         // set delivery address
         $cart->id_address_delivery = $address['id_address'];
         // set invoice address
         $cart->id_address_invoice = $address['id_address'];
     }
     // save and validate order
     $this->module->validateOrder($cart->id, $status, $total, $this->module->displayName . '-' . $method, NULL, $mailVars, (int) $currency->id, false, $customer->secure_key);
     // redirect to order confirmation
     Tools::redirect('index.php?controller=order-confirmation&id_cart=' . $cart->id . '&id_module=' . $this->module->id . '&id_order=' . $this->module->currentOrder . '&key=' . $customer->secure_key . '&session=' . $session);
 }
 /**
  * Initialize front controller.
  *
  * @return 	this
  */
 public function init()
 {
     // is the user logged in?
     if (!$this->context->customer->isLogged(true)) {
         // get current url
         $back_url = $this->context->link->getPageLink('order', true, (int) $this->context->language->id, $params);
         // set url parameters
         $params = array('back' => $back_url);
         // redirect to log in page
         Tools::redirect($this->context->link->getPageLink('authentication', true, (int) $this->context->language->id, $params));
     }
     $configuration = unserialize(Configuration::get('CHECKMEOUT'));
     if (!isset($configuration['CHECKMEOUT_API_TOKEN'], $configuration['CHECKMEOUT_API_SECRET'], $configuration['CHECKMEOUT_REFERENCE'])) {
         Tools::redirect('/');
     }
     $token = $configuration['CHECKMEOUT_API_TOKEN'];
     $secret = $configuration['CHECKMEOUT_API_SECRET'];
     $checkmeout = new CheckMeOutBase($token, $secret);
     parent::init();
     $cart = $this->context->cart;
     $summary = $this->context->cart->getSummaryDetails();
     $products = $this->context->cart->getProducts(true);
     foreach ($products as $product) {
         $image = $this->context->link->getImageLink($product['link_rewrite'], $product['id_image'], 'home_default');
         $link = 'http://' . $_SERVER['HTTP_HOST'] . '/index.php?controller=product&id_product=' . $product['id_product'];
         $price = 0;
         $checkmeout->addItem($product['name'], $product['total'], $image, $link);
     }
     $shipping = $summary['total_shipping'];
     $tax = $summary['total_tax'];
     if ($summary['total_shipping_tax_exc'] > 0) {
         $shipping = $summary['total_shipping_tax_exc'];
     }
     if ($shipping > 0) {
         $checkmeout->addItem('Shipping Cost', $shipping, null, null);
     }
     if ($tax > 0) {
         $checkmeout->addItem('Tax', $tax, null, null);
     }
     // if user is logged in
     if ($this->context->customer->isLogged(true)) {
         // get the customer details
         $customer = $this->context->customer;
         // get the fullname
         $fullname = $customer->firstname . ' ' . $customer->lastname;
         // set customer information
         $checkmeout->setCustomer($fullname, $customer->email, null);
     }
     $currency = $this->context->currency->iso_code;
     // get all addresses
     $addresses = $this->context->customer->getAddresses($this->context->language->id);
     foreach ($addresses as $address) {
         // get the customer details
         $customer = $this->context->customer;
         // get the fullname
         $fullname = $customer->firstname . ' ' . $customer->lastname;
         // set customer information
         $checkmeout->setCustomer($fullname, $customer->email, $address['phone_mobile']);
         // get country iso code
         $country = $this->context->country->iso_code;
         $city = 'N/A';
         if (isset($address['city']) && $address['city'] != '') {
             $city = $address['city'];
         }
         $state = 'N/A';
         // if state is set
         if ($address['id_state'] != 0) {
             // get the state by id
             $state = new State(intval($addres['id_state']));
             // get the state
             $state = $state->name;
         }
         $checkmeout->addAddress($address['alias'], $address['address1'], $city, $state, $country, $address['postcode']);
     }
     $redirect = $checkmeout->setRedirect('http://' . $_SERVER['HTTP_HOST'] . '/module/checkmeout/confirmation')->setTitle($configuration['CHECKMEOUT_TITLE'])->setWebsite($configuration['CHECKMEOUT_WEBSITE'])->setLogo($configuration['CHECKMEOUT_LOGO'])->setFavicon($configuration['CHECKMEOUT_FAVICON'])->setStyles($configuration['CHECKMEOUT_STYLES'])->setTerms($configuration['CHECKMEOUT_TERMS'])->setGateway($configuration['CHECKMEOUT_GATEWAYS'])->setCurrency($currency)->setShipping(1)->setCallbackUrl('http://' . $_SERVER['HTTP_HOST'] . '/order')->setSession($configuration['CHECKMEOUT_REFERENCE'])->getCheckoutUrl();
     Tools::redirect($redirect);
 }