示例#1
0
 /**
  * Get custom params of order
  * @param array|int $ids order ID
  * @return array params in key=>value format
  */
 public function get($ids, $full = false)
 {
     if (!$ids) {
         return array();
     }
     $params = array();
     $payment = $shipping = array();
     $plugin_model = new shopPluginModel();
     foreach ($this->getByField('order_id', $ids, true) as $p) {
         $params[$p['order_id']][$p['name']] = $p['value'];
         if ($full) {
             if ($p['name'] == 'shipping_id' && $p['value']) {
                 if (!isset($shipping[$p['value']])) {
                     $shipping[$p['value']] = $plugin_model->getById($p['value']);
                 }
                 if (!empty($shipping[$p['value']])) {
                     $params[$p['order_id']]['shipping_description'] = $shipping[$p['value']]['description'];
                 } else {
                     $params[$p['order_id']]['shipping_description'] = '';
                 }
             }
             if ($p['name'] == 'payment_id' && $p['value']) {
                 if (!isset($payment[$p['value']])) {
                     $payment[$p['value']] = $plugin_model->getById($p['value']);
                 }
                 if (!empty($payment[$p['value']])) {
                     $params[$p['order_id']]['payment_description'] = $payment[$p['value']]['description'];
                 } else {
                     $params[$p['order_id']]['payment_description'] = '';
                 }
             }
         }
     }
     if (is_numeric($ids)) {
         $params = isset($params[$ids]) ? $params[$ids] : array();
     } else {
         foreach ($ids as $id) {
             if (!isset($params[$id])) {
                 $params[$id] = array();
             }
         }
     }
     return $params;
 }
 public function display()
 {
     $plugin_model = new shopPluginModel();
     if (waRequest::param('payment_id') && is_array(waRequest::param('payment_id'))) {
         $methods = $plugin_model->getById(waRequest::param('payment_id'));
     } else {
         $methods = $plugin_model->listPlugins('payment');
     }
     $shipping = $this->getSessionData('shipping');
     if ($shipping) {
         $disabled = shopHelper::getDisabledMethods('payment', $shipping['id']);
     } else {
         $disabled = array();
     }
     $currencies = wa('shop')->getConfig()->getCurrencies();
     $selected = null;
     foreach ($methods as $key => $m) {
         $method_id = $m['id'];
         if (in_array($method_id, $disabled)) {
             unset($methods[$key]);
             continue;
         }
         $plugin = shopPayment::getPlugin($m['plugin'], $m['id']);
         $plugin_info = $plugin->info($m['plugin']);
         $methods[$key]['icon'] = $plugin_info['icon'];
         $custom_fields = $this->getCustomFields($method_id, $plugin);
         $custom_html = '';
         foreach ($custom_fields as $c) {
             $custom_html .= '<div class="wa-field">' . $c . '</div>';
         }
         $methods[$key]['custom_html'] = $custom_html;
         $allowed_currencies = $plugin->allowedCurrency();
         if ($allowed_currencies !== true) {
             $allowed_currencies = (array) $allowed_currencies;
             if (!array_intersect($allowed_currencies, array_keys($currencies))) {
                 $methods[$key]['error'] = sprintf(_w('Payment procedure cannot be processed because required currency %s is not defined in your store settings.'), implode(', ', $allowed_currencies));
             }
         }
         if (!$selected && empty($methods[$key]['error'])) {
             $selected = $method_id;
         }
     }
     $view = wa()->getView();
     $view->assign('checkout_payment_methods', $methods);
     $view->assign('payment_id', $this->getSessionData('payment', $selected));
     $checkout_flow = new shopCheckoutFlowModel();
     $step_number = shopCheckout::getStepNumber('payment');
     // IF no errors
     $checkout_flow->add(array('step' => $step_number));
     // ELSE
     //        $checkout_flow->add(array(
     //            'step' => $step_number,
     //            'description' => ERROR MESSAGE HERE
     //        ));
 }
示例#3
0
 public static function getPluginInfo($id)
 {
     if ($plugin_id = max(0, intval($id))) {
         $model = new shopPluginModel();
         $info = $model->getById($plugin_id);
         if (!$info) {
             throw new waException("Shipping plugin {$plugin_id} not found", 404);
         }
     } else {
         $info = array('plugin' => $id, 'status' => 1);
     }
     $default_info = waShipping::info($info['plugin']);
     return array_merge($default_info, $info);
 }
 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 shopOnestepCheckoutShipping();
         $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 = shopOnestepCheckout::getStepNumber('confirmation');
     // IF no errors
     $checkout_flow->add(array('step' => $step_number));
     // ELSE
     //        $checkout_flow->add(array(
     //            'step' => $step_number,
     //            'description' => ERROR MESSAGE HERE
     //        ));
 }
 protected function createOrder()
 {
     $checkout_data = wa()->getStorage()->get('shop/checkout');
     if (wa()->getUser()->isAuth()) {
         $contact = wa()->getUser();
     } else {
         if (!empty($checkout_data['contact']) && $checkout_data['contact'] instanceof waContact) {
             $contact = $checkout_data['contact'];
         } else {
             $contact = new waContact();
         }
     }
     $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_description'] = null;
     $order['discount'] = shopDiscounts::apply($order, $order['discount_description']);
     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 shopOnestepCheckoutShipping();
         $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 = 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;
             }
         }
     }
     if (($landing = waRequest::cookie('landing')) && ($landing = @parse_url($landing))) {
         if (!empty($landing['query'])) {
             @parse_str($landing['query'], $arr);
             if (!empty($arr['gclid']) && !empty($order['params']['referer_host']) && strpos($order['params']['referer_host'], 'google') !== false) {
                 $order['params']['referer_host'] .= ' (cpc)';
                 $order['params']['cpc'] = 1;
             } else {
                 if (!empty($arr['_openstat']) && !empty($order['params']['referer_host']) && strpos($order['params']['referer_host'], 'yandex') !== false) {
                     $order['params']['referer_host'] .= ' (cpc)';
                     $order['params']['openstat'] = $arr['_openstat'];
                     $order['params']['cpc'] = 1;
                 }
             }
         }
         $order['params']['landing'] = $landing['path'];
     }
     // A/B tests
     /*
             $abtest_variants_model = new shopAbtestVariantsModel();
             foreach (waRequest::cookie() as $k => $v) {
                 if (substr($k, 0, 5) == 'waabt') {
                     $variant_id = $v;
                     $abtest_id = substr($k, 5);
                     if (wa_is_int($abtest_id) && wa_is_int($variant_id)) {
                         $row = $abtest_variants_model->getById($variant_id);
                         if ($row && $row['abtest_id'] == $abtest_id) {
                             $order['params']['abt' . $abtest_id] = $variant_id;
                         }
                     }
                 }
             }
     */
     $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 = shopOnestepCheckout::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 $order_id;
     } else {
         return false;
     }
 }
 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;
     }
 }
 private function getParams(&$data, $id)
 {
     $model = new shopPluginModel();
     $shipping_address = array();
     if (!empty($data['contact'])) {
         $address = $data['contact']->getFirst('address.shipping');
         if (!$address) {
             $address = $data['contact']->getFirst('address');
         }
         if (!empty($address['data'])) {
             $shipping_address = $address['data'];
         }
     }
     $empty_address = false;
     // shipping
     if ($shipping_id = waRequest::post('shipping_id')) {
         $shipping_parts = explode('.', $shipping_id);
         $shipping_id = $shipping_parts[0];
         $rate_id = isset($shipping_parts[1]) ? $shipping_parts[1] : '';
         $data['params']['shipping_id'] = $shipping_id;
         $data['params']['shipping_rate_id'] = $rate_id;
         $plugin_info = $model->getById($shipping_id);
         $plugin = shopShipping::getPlugin($plugin_info['plugin'], $shipping_id);
         $rates = $plugin->getRates($this->getOrderItems($data['items'], $plugin->allowedWeightUnit()), $shipping_address);
         // save address
         if ($plugin->allowedAddress() === false) {
             $empty_address = true;
         }
         if (!$rates) {
             $this->errors['order']['common'] = _w('Unknown region for delivery');
             return;
         }
         if (!$rate_id) {
             $rate = reset($rates);
             $data['params']['shipping_rate_id'] = key($rates);
         } else {
             $rate = $rates[$rate_id];
         }
         $data['params']['shipping_plugin'] = $plugin->getId();
         $data['params']['shipping_name'] = $plugin_info['name'] . (!empty($rate['name']) ? ' (' . $rate['name'] . ')' : '');
         $data['params']['shipping_est_delivery'] = $rate['est_delivery'];
         if (waRequest::post('shipping' . $shipping_id)) {
             foreach (waRequest::post('shipping_' . $shipping_id) as $k => $v) {
                 $data['params']['shipping_params_' . $k] = $v;
             }
         }
     } else {
         foreach (array('id', 'rate_id', 'plugin', 'name', 'est_delivery') as $k) {
             $data['params']['shipping_' . $k] = null;
         }
     }
     // payment
     if ($payment_id = waRequest::post('payment_id')) {
         $data['params']['payment_id'] = $payment_id;
         $plugin_info = $model->getById($payment_id);
         $data['params']['payment_plugin'] = $plugin_info['plugin'];
         $data['params']['payment_name'] = $plugin_info['name'];
         if (waRequest::post('payment_' . $payment_id)) {
             foreach (waRequest::post('payment_' . $payment_id) as $k => $v) {
                 $data['params']['payment_params_' . $k] = $v;
             }
         }
     }
     // shipping and billing addreses
     if (!empty($data['contact'])) {
         // Make sure all old address data is removed
         if ($id) {
             $opm = new shopOrderParamsModel();
             foreach ($opm->get($id) as $k => $v) {
                 if (preg_match('~^(billing|shipping)_address\\.~', $k)) {
                     $data['params'][$k] = null;
                 }
             }
         }
         if (!$empty_address && $this->shipping_address) {
             foreach ($this->shipping_address as $k => $v) {
                 $data['params']['shipping_address.' . $k] = $v;
             }
         }
         if ($this->billing_address) {
             foreach ($this->billing_address as $k => $v) {
                 $data['params']['billing_address.' . $k] = $v;
             }
         }
     }
 }
 public function getRate($id = null, $rate_id = null, $contact = null)
 {
     if (!$id) {
         $shipping = $this->getSessionData('shipping');
         if (!$shipping) {
             return array();
         }
         $id = $shipping['id'];
         $rate_id = $shipping['rate_id'];
     }
     if (!$contact) {
         $contact = $this->getContact();
     }
     $plugin_model = new shopPluginModel();
     $plugin_info = $plugin_model->getById($id);
     $plugin = shopShipping::getPlugin($plugin_info['plugin'], $id);
     $cart = new shopCart();
     $total = $cart->total();
     $currency = $plugin->allowedCurrency();
     $currrent_currency = wa()->getConfig()->getCurrency(false);
     if ($currency != $currrent_currency) {
         $total = shop_currency($total, $currrent_currency, $currency, false);
     }
     $rates = $plugin->getRates($this->getItems($plugin->allowedWeightUnit()), $this->getAddress($contact), array('total_price' => $total));
     if (!$rates) {
         return false;
     }
     if (is_string($rates)) {
         return $rates;
     }
     if ($rate_id) {
         $result = $rates[$rate_id];
     } else {
         $result = array('rate' => 0);
     }
     if (is_array($result['rate'])) {
         $result['rate'] = max($result['rate']);
     }
     if ($currency != $currrent_currency) {
         $result['rate'] = shop_currency($result['rate'], $currency, $currrent_currency, false);
     }
     $result['plugin'] = $plugin->getId();
     $result['name'] = $plugin_info['name'] . (!empty($result['name']) ? ' (' . $result['name'] . ')' : '');
     return $result;
 }
示例#9
0
 /**
  * Verify that the plugin is suitable for payment of the order
  * @param int $order_id
  * @return bool|string
  */
 private function isSuitable($order_id)
 {
     $order_params_model = new shopOrderParamsModel();
     if (!$this->merchant_id) {
         return 'Invalid plugin id';
     } else {
         if ($this->merchant_id != $order_params_model->getOne($order_id, 'payment_id')) {
             return 'Plugin does not suitable to payment of the order';
         }
         $shop_plugin_model = new shopPluginModel();
         if ($plugin = $shop_plugin_model->getById($this->merchant_id)) {
             if (!$plugin['status']) {
                 return 'Plugin status is disabled';
             }
         } else {
             return 'Plugin deleted';
         }
     }
     return true;
 }