public function execute()
 {
     $cart = new shopCart();
     $total = $cart->total();
     $shipping = new shopCheckoutShipping();
     $items = $shipping->getItems();
     if (waRequest::method() == 'post') {
         wa()->getStorage()->close();
         $shipping_id = waRequest::post('shipping_id');
         $customer = waRequest::post('customer_' . $shipping_id);
         if (isset($customer['address.shipping'])) {
             $address = $customer['address.shipping'];
         } else {
             $address = array();
         }
         if ($shipping_id) {
             $this->response = $this->getRates($shipping_id, $items, $address, $total);
         } else {
             $this->errors = _w('Shipping is required');
         }
     } elseif ($shipping_ids = waRequest::get('shipping_id', array(), waRequest::TYPE_ARRAY_INT)) {
         $address = $shipping->getAddress();
         wa()->getStorage()->close();
         $empty = true;
         foreach ($address as $v) {
             if ($v) {
                 $empty = false;
                 break;
             }
         }
         if ($empty) {
             $address = array();
         }
         if (!$address) {
             $settings = wa('shop')->getConfig()->getCheckoutSettings();
             if ($settings['contactinfo']['fields']['address']) {
                 foreach ($settings['contactinfo']['fields']['address']['fields'] as $k => $f) {
                     if (!empty($f['value'])) {
                         $address[$k] = $f['value'];
                     }
                 }
             }
         }
         foreach ($shipping_ids as $shipping_id) {
             $this->response[$shipping_id] = $this->getRates($shipping_id, $items, $address, $total);
         }
     }
 }
 /**
  * Execute step
  *
  * @return bool
  */
 public function execute()
 {
     $contact = $this->getContact();
     if (!$contact) {
         $contact = new waContact();
     }
     $this->form = shopHelper::getCustomerForm();
     if (!$this->form->isValid($contact)) {
         return false;
     }
     $data = waRequest::post('customer');
     if ($data && is_array($data)) {
         foreach ($data as $field => $value) {
             $contact->set($field, $value);
         }
     }
     if ($shipping = $this->getSessionData('shipping') && !waRequest::post('ignore_shipping_error')) {
         $shipping_step = new shopCheckoutShipping();
         $rate = $shipping_step->getRate($shipping['id'], isset($shipping['rate_id']) ? $shipping['rate_id'] : null, $contact);
         if (!$rate || is_string($rate)) {
             // remove selected shipping method
             $this->setSessionData('shipping', null);
             /*
             $errors = array();
             $errors['all'] = sprintf(_w('We cannot ship to the specified address via %s.'), $shipping['name']);
             if ($rate) {
                 $errors['all'] .= '<br> <strong>'.$rate.'</strong><br>';
             }
             $errors['all'] .= '<br> '._w('Please double-check the address above, or return to the shipping step and select another shipping option.');
             $errors['all'] .= '<input type="hidden" name="ignore_shipping_error" value="1">';
             wa()->getView()->assign('errors', $errors);
             return false;
             */
         }
     }
     if (wa()->getUser()->isAuth()) {
         $contact->save();
     } else {
         $errors = array();
         if (waRequest::post('create_user')) {
             $login = waRequest::post('login');
             if (!$login) {
                 $errors['email'][] = _ws('Required');
             }
             if (!waRequest::post('password')) {
                 $errors['password'] = _ws('Required');
             }
             $email_validator = new waEmailValidator();
             if (!$email_validator->isValid($login)) {
                 $errors['email'] = $email_validator->getErrors();
             }
             if (!$errors) {
                 $contact_model = new waContactModel();
                 if ($contact_model->getByEmail($login, true)) {
                     $errors['email'][] = _w('Email already registered');
                 }
             }
             if (!$errors) {
                 $contact->set('email', $login);
                 $contact->set('password', waRequest::post('password'));
             } else {
                 if (isset($errors['email'])) {
                     $errors['email'] = implode(', ', $errors['email']);
                 }
                 wa()->getView()->assign('errors', $errors);
                 return false;
             }
         }
         $this->setSessionData('contact', $contact);
     }
     if ($comment = waRequest::post('comment')) {
         $this->setSessionData('comment', $comment);
     }
     return true;
 }
 protected function createOrder()
 {
     $checkout_data = $this->getStorage()->get('shop/checkout');
     $contact = $this->getUser()->isAuth() ? $this->getUser() : $checkout_data['contact'];
     $cart = new shopCart();
     $items = $cart->items(false);
     // remove id from item
     foreach ($items as &$item) {
         unset($item['id']);
         unset($item['parent_id']);
     }
     unset($item);
     $order = array('contact' => $contact, 'items' => $items, 'total' => $cart->total(false), 'params' => isset($checkout_data['params']) ? $checkout_data['params'] : array());
     $order['discount'] = shopDiscounts::apply($order);
     if (isset($checkout_data['shipping'])) {
         $order['params']['shipping_id'] = $checkout_data['shipping']['id'];
         $order['params']['shipping_rate_id'] = $checkout_data['shipping']['rate_id'];
         $shipping_step = new shopCheckoutShipping();
         $rate = $shipping_step->getRate($order['params']['shipping_id'], $order['params']['shipping_rate_id']);
         $order['params']['shipping_plugin'] = $rate['plugin'];
         $order['params']['shipping_name'] = $rate['name'];
         if (isset($rate['est_delivery'])) {
             $order['params']['shipping_est_delivery'] = $rate['est_delivery'];
         }
         if (!isset($order['shipping'])) {
             $order['shipping'] = $rate['rate'];
         }
         if (!empty($order['params']['shipping'])) {
             foreach ($order['params']['shipping'] as $k => $v) {
                 $order['params']['shipping_params_' . $k] = $v;
             }
             unset($order['params']['shipping']);
         }
     } else {
         $order['shipping'] = 0;
     }
     if (isset($checkout_data['payment'])) {
         $order['params']['payment_id'] = $checkout_data['payment'];
         $plugin_model = new shopPluginModel();
         $plugin_info = $plugin_model->getById($checkout_data['payment']);
         $order['params']['payment_name'] = $plugin_info['name'];
         $order['params']['payment_plugin'] = $plugin_info['plugin'];
         if (!empty($order['params']['payment'])) {
             foreach ($order['params']['payment'] as $k => $v) {
                 $order['params']['payment_params_' . $k] = $v;
             }
             unset($order['params']['payment']);
         }
     }
     if ($skock_id = waRequest::post('stock_id')) {
         $order['params']['stock_id'] = $skock_id;
     }
     $routing_url = wa()->getRouting()->getRootUrl();
     $order['params']['storefront'] = wa()->getConfig()->getDomain() . ($routing_url ? '/' . $routing_url : '');
     if (($ref = wa()->getStorage()->get('shop/referer')) || ($ref = waRequest::cookie('referer'))) {
         $order['params']['referer'] = $ref;
         $ref_parts = parse_url($ref);
         $order['params']['referer_host'] = $ref_parts['host'];
         // try get search keywords
         if (!empty($ref_parts['query'])) {
             $search_engines = array('text' => 'yandex\\.|rambler\\.', 'q' => 'bing\\.com|mail\\.|google\\.', 's' => 'nigma\\.ru', 'p' => 'yahoo\\.com');
             $q_var = false;
             foreach ($search_engines as $q => $pattern) {
                 if (preg_match('/(' . $pattern . ')/si', $ref_parts['host'])) {
                     $q_var = $q;
                     break;
                 }
             }
             // default query var name
             if (!$q_var) {
                 $q_var = 'q';
             }
             parse_str($ref_parts['query'], $query);
             if (!empty($query[$q_var])) {
                 $order['params']['keyword'] = $query[$q_var];
             }
         }
     }
     if ($utm = waRequest::cookie('utm')) {
         $utm = json_decode($utm, true);
         if ($utm && is_array($utm)) {
             foreach ($utm as $k => $v) {
                 $order['params']['utm_' . $k] = $v;
             }
         }
     }
     $order['params']['ip'] = waRequest::getIp();
     $order['params']['user_agent'] = waRequest::getUserAgent();
     foreach (array('shipping', 'billing') as $ext) {
         $address = $contact->getFirst('address.' . $ext);
         if ($address) {
             foreach ($address['data'] as $k => $v) {
                 $order['params'][$ext . '_address.' . $k] = $v;
             }
         }
     }
     if (isset($checkout_data['comment'])) {
         $order['comment'] = $checkout_data['comment'];
     }
     $workflow = new shopWorkflow();
     if ($order_id = $workflow->getActionById('create')->run($order)) {
         $step_number = shopCheckout::getStepNumber();
         $checkout_flow = new shopCheckoutFlowModel();
         $checkout_flow->add(array('step' => $step_number));
         $cart->clear();
         wa()->getStorage()->remove('shop/checkout');
         wa()->getStorage()->set('shop/order_id', $order_id);
         return true;
     }
 }
 public function display()
 {
     $settings = wa('shop')->getConfig()->getCheckoutSettings();
     if (!empty($settings['confirmation']['terms']) && waRequest::get('terms')) {
         echo $settings['confirmation']['terms'];
         exit;
     }
     $cart = new shopCart();
     $items = $cart->items(false);
     $subtotal = $cart->total(false);
     $order = array('contact' => $this->getContact(), 'total' => $subtotal, 'items' => $items);
     $order['discount'] = shopDiscounts::calculate($order);
     $contact = $this->getContact();
     $view = wa()->getView();
     if (!$contact) {
         $view->assign('error', _w('Not enough data in the contact information to place the order.'));
         return;
     }
     $shipping_address = $contact->getFirst('address.shipping');
     if (!$shipping_address) {
         $shipping_address = array('data' => array(), 'value' => '');
     }
     $billing_address = $contact->getFirst('address.billing');
     if (!$billing_address) {
         $billing_address = array('data' => array(), 'value' => '');
     }
     $discount_rate = (double) $subtotal ? $order['discount'] / $subtotal : 0;
     $taxes = shopTaxes::apply($items, array('shipping' => $shipping_address['data'], 'billing' => $billing_address['data'], 'discount_rate' => $discount_rate));
     $tax = 0;
     $tax_included = 0;
     foreach ($taxes as $t) {
         if (isset($t['sum'])) {
             $tax += $t['sum'];
         }
         if (isset($t['sum_included'])) {
             $tax_included += $t['sum_included'];
         }
     }
     if (!isset($order['shipping'])) {
         $shipping_step = new shopCheckoutShipping();
         $rate = $shipping_step->getRate();
         if ($rate) {
             $order['shipping'] = $rate['rate'];
         } else {
             $order['shipping'] = 0;
         }
     }
     $plugin_model = new shopPluginModel();
     $params = array();
     if ($shipping = $this->getSessionData('shipping')) {
         $params['shipping_id'] = $shipping['id'];
         if ($shipping['id']) {
             $plugin_info = $plugin_model->getById($shipping['id']);
             $params['shipping_rate_id'] = $shipping['rate_id'];
             $params['shipping_name'] = $shipping['name'];
             $params['shipping_description'] = $plugin_info['description'];
         }
     }
     if ($payment_id = $this->getSessionData('payment')) {
         $params['payment_id'] = $payment_id;
         $plugin_info = $plugin_model->getById($payment_id);
         $params['payment_name'] = $plugin_info['name'];
         $params['payment_plugin'] = $plugin_info['plugin'];
         $params['payment_description'] = $plugin_info['description'];
     }
     $view->assign(array('params' => $params, 'contact' => $contact, 'items' => $items, 'shipping' => $order['shipping'], 'discount' => $order['discount'], 'total' => $subtotal - $order['discount'] + $order['shipping'] + $tax, 'tax' => $tax_included + $tax, 'subtotal' => $subtotal, 'shipping_address' => $shipping_address, 'billing_address' => !empty($settings['contactinfo']['fields']['address.billing']) ? $billing_address : false, 'terms' => !empty($settings['confirmation']['terms']) ? $settings['confirmation']['terms'] : false));
     $checkout_flow = new shopCheckoutFlowModel();
     $step_number = shopCheckout::getStepNumber('confirmation');
     // IF no errors
     $checkout_flow->add(array('step' => $step_number));
     // ELSE
     //        $checkout_flow->add(array(
     //            'step' => $step_number,
     //            'description' => ERROR MESSAGE HERE
     //        ));
 }