Exemplo n.º 1
0
 /**
  * Sends a test notification.
  * 
  * @param int $id Notification id stored in table shop_notification
  * @param array $data Order data array
  * @param string|null $to Recipient address/number. If not specified, recipient address/number from notification parameters is used.
  */
 public static function sendOne($id, $data, $to = null)
 {
     $n = self::getModel()->getOne($id);
     if ($n) {
         $params_model = new shopOrderParamsModel();
         $data['order']['params'] = $params_model->get($data['order']['id']);
         $items_model = new shopOrderItemsModel();
         $data['order']['items'] = $items_model->getItems($data['order']['id']);
         foreach ($data['order']['items'] as &$i) {
             if (!empty($i['file_name'])) {
                 $i['download_link'] = wa()->getRouteUrl('/frontend/myOrderDownload', array('id' => $data['order']['id'], 'code' => $data['order']['params']['auth_code'], 'item' => $i['id']), true);
             }
         }
         unset($i);
         if (!empty($data['order']['params']['shipping_id'])) {
             try {
                 $data['shipping_plugin'] = shopShipping::getPlugin($data['order']['params']['shipping_plugin'], $data['order']['params']['shipping_id']);
             } catch (waException $e) {
             }
         }
         $method = 'send' . ucfirst($n['transport']);
         if (method_exists('shopNotifications', $method)) {
             if ($to !== null) {
                 $n['to'] = $to;
             }
             self::$method($n, $data);
         }
     }
 }
 protected function isAuth($order, &$code)
 {
     $result = false;
     if (!$result) {
         $result = $order['id'] == wa()->getStorage()->get('shop/order_id');
     }
     if (!$result) {
         // Check that order exists and belongs to this user
         $result = $order['contact_id'] == wa()->getUser()->getId() && $order['state_id'] != 'deleted';
     }
     if (!$result && $code) {
         // Check auth code
         $opm = new shopOrderParamsModel();
         $params = $opm->get($order['id']);
         if (!empty($params['auth_pin']) && ifset($params['auth_code']) === $code) {
             $pin = wa()->getStorage()->get('shop/pin/' . $order['id']);
             if ($pin && $pin == $params['auth_pin']) {
                 $result = true;
             }
         } else {
             $code = false;
         }
     }
     return $result;
 }
 public function execute()
 {
     $order_id = waRequest::request('order_id', 0, 'int');
     $id = waRequest::request('id', 0, 'int');
     $to = waRequest::request('to');
     $nm = new shopNotificationModel();
     $n = $nm->getById($id);
     if (!$n) {
         $this->errors = sprintf_wp('%s entry not found', _w('Notification'));
         return;
     }
     $om = new shopOrderModel();
     $o = $om->getById($order_id);
     if (!$o) {
         $this->errors = _w('Order not found');
         return;
     }
     shopHelper::workupOrders($o, true);
     $opm = new shopOrderParamsModel();
     $o['params'] = $opm->get($order_id);
     try {
         $contact = $o['contact_id'] ? new shopCustomer($o['contact_id']) : wa()->getUser();
         $contact->getName();
     } catch (Exception $e) {
         $contact = new shopCustomer(wa()->getUser()->getId());
     }
     $cm = new shopCustomerModel();
     $customer = $cm->getById($contact->getId());
     if (!$customer) {
         $customer = $cm->getEmptyRow();
     }
     $workflow = new shopWorkflow();
     // send notifications
     shopNotifications::sendOne($id, array('order' => $o, 'customer' => $contact, 'status' => $workflow->getStateById($o['state_id'])->getName()), $to);
 }
 public function execute()
 {
     $code = waRequest::param('code');
     $encoded_order_id = waRequest::param('id');
     $order_id = shopHelper::decodeOrderId($encoded_order_id);
     if (!$order_id) {
         // fall back to non-encoded id
         $order_id = $encoded_order_id;
         $encoded_order_id = shopHelper::encodeOrderId($order_id);
     }
     if (!$order_id || $order_id != substr($code, 16, -16)) {
         throw new waException(_w('Order not found'), 404);
     }
     // When user is authorized, check if order belongs to him.
     // When it does, redirect to plain order page.
     if (wa()->getUser()->isAuth()) {
         $om = new shopOrderModel();
         $order = $om->getOrder($order_id);
         if (!$order) {
             throw new waException(_w('Order not found'), 404);
         }
         if ($order['contact_id'] == wa()->getUser()->getId()) {
             $this->redirect(wa()->getRouteUrl('/frontend/myOrder', array('id' => $order_id)));
         }
     }
     // Check auth code
     $opm = new shopOrderParamsModel();
     $params = $opm->get($order_id);
     if (ifset($params['auth_code']) !== $code || empty($params['auth_pin'])) {
         throw new waException(_w('Order not found'), 404);
     }
     // Check auth pin and show order page if pin is correct
     $pin = waRequest::request('pin', wa()->getStorage()->get('shop/pin/' . $order_id));
     if ($pin && $pin == $params['auth_pin']) {
         wa()->getStorage()->set('shop/pin/' . $order_id, $pin);
         parent::execute();
         if (!waRequest::isXMLHttpRequest()) {
             $this->layout->assign('breadcrumbs', self::getBreadcrumbs());
         }
         return;
     }
     //
     // No pin or pin is incorrect: show form to enter pin
     //
     $this->view->assign('wrong_pin', !!$pin);
     $this->view->assign('pin_required', true);
     $this->view->assign('encoded_order_id', $encoded_order_id);
     $this->view->assign('my_nav_selected', 'orders');
     // Set up layout and template from theme
     $this->setThemeTemplate('my.order.html');
     if (!waRequest::isXMLHttpRequest()) {
         $this->setLayout(new shopFrontendLayout());
         $this->getResponse()->setTitle(_w('Order') . ' ' . $encoded_order_id);
         $this->view->assign('breadcrumbs', self::getBreadcrumbs());
         $this->layout->assign('nofollow', true);
     }
 }
 public function execute()
 {
     $order_id = waRequest::request('order_id', 0, 'int');
     $followup_id = waRequest::request('followup_id', 0, 'int');
     $email = waRequest::request('email');
     $fm = new shopFollowupModel();
     $f = $fm->getById($followup_id);
     if (!$f) {
         $this->errors = sprintf_wp('%s entry not found', _w('Follow-up'));
         return;
     }
     $om = new shopOrderModel();
     $o = $om->getById($order_id);
     if (!$o) {
         $this->errors = _w('Order not found');
         return;
     }
     shopHelper::workupOrders($o, true);
     $opm = new shopOrderParamsModel();
     $o['params'] = $opm->get($order_id);
     try {
         $contact = $o['contact_id'] ? new shopCustomer($o['contact_id']) : wa()->getUser();
         $contact->getName();
     } catch (Exception $e) {
         $contact = new shopCustomer(wa()->getUser()->getId());
     }
     $cm = new shopCustomerModel();
     $customer = $cm->getById($contact->getId());
     if (!$customer) {
         $customer = $cm->getEmptyRow();
     }
     $to = array($email => $contact->getName());
     if (!shopFollowupCli::sendOne($f, $o, $customer, $contact, $to)) {
         $this->errors = "Unable to send follow-up #{$f['id']} for order #{$o['id']}: waMessage->send() returned FALSE.";
         return;
     }
     $this->response = 'ok';
 }
 public function execute()
 {
     $encoded_order_id = waRequest::param('id');
     $order_id = shopHelper::decodeOrderId($encoded_order_id);
     if (!$order_id) {
         // fall back to non-encoded id
         $order_id = $encoded_order_id;
         $encoded_order_id = shopHelper::encodeOrderId($order_id);
     }
     $om = new shopOrderModel();
     $order = $om->getOrder($order_id);
     if (!$order) {
         throw new waException(_w('Order not found'), 404);
     }
     if (!$this->isAuth($order)) {
         throw new waException(_w('The file will be available for download after the order is paid and processed.'), 404);
     }
     // Check auth code
     $opm = new shopOrderParamsModel();
     $params = $opm->get($order_id);
     $code = waRequest::param('code');
     if (ifset($params['auth_code']) !== $code) {
         throw new waException(_w('Order not found'), 404);
     }
     if ($item = ifempty($order['items'][waRequest::param('item')])) {
         $skus_model = new shopProductSkusModel();
         $sku = $skus_model->getById(ifempty($item['sku_id']));
         if ($sku['file_name'] && $sku['file_size']) {
             $file_path = shopProductSkusModel::getPath($sku);
             waFiles::readFile($file_path, $sku['file_name']);
         } else {
             throw new waException(_w('File not found'), 404);
         }
     } else {
         throw new waException(_w('Order item not found'), 404);
     }
 }
 public function postExecute($order_id = null, $result = null)
 {
     $order_id = $result['order_id'];
     $data = is_array($result) ? $result : array();
     $data['order_id'] = $order_id;
     $data['action_id'] = $this->getId();
     $data['before_state_id'] = '';
     $data['after_state_id'] = 'new';
     $order_log_model = new shopOrderLogModel();
     $order_log_model->add($data);
     /**
      * @event order_action.create
      */
     wa('shop')->event('order_action.create', $data);
     $order_model = new shopOrderModel();
     $order = $order_model->getById($order_id);
     $params_model = new shopOrderParamsModel();
     $order['params'] = $params_model->get($order_id);
     // send notifications
     shopNotifications::send('order.' . $this->getId(), array('order' => $order, 'customer' => new waContact($order['contact_id']), 'status' => $this->getWorkflow()->getStateById($data['after_state_id'])->getName(), 'action_data' => $data));
     // Update stock count, but take into account 'update_stock_count_on_create_order'-setting
     $app_settings_model = new waAppSettingsModel();
     if ($app_settings_model->get('shop', 'update_stock_count_on_create_order')) {
         // for logging changes in stocks
         shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was placed', array('order_id' => $order_id));
         $order_model->reduceProductsFromStocks($order_id);
         shopProductStocksLogModel::clearContext();
     }
     return $order_id;
 }
 public function execute()
 {
     $id = waRequest::request('id', 0, 'int');
     if (!$id || !wa()->getUser()->getRights('shop', 'orders')) {
         $this->redirect(wa()->getAppUrl());
     }
     // Order
     $om = new shopOrderModel();
     $order = $om->getOrder($id);
     shopHelper::workupOrders($order, true);
     $order['tax'] = (double) $order['tax'];
     $order['discount'] = (double) $order['discount'];
     // Order params
     $opm = new shopOrderParamsModel();
     $order['params'] = $opm->get($order['id']);
     // Order subtotal
     $order_subtotal = 0;
     foreach ($order['items'] as $i) {
         $order_subtotal += $i['price'] * $i['quantity'];
     }
     // Format addresses
     $settings = wa('shop')->getConfig()->getCheckoutSettings();
     $form_fields = ifset($settings['contactinfo']['fields'], array());
     $formatter = new waContactAddressSeveralLinesFormatter();
     $shipping_address = shopHelper::getOrderAddress($order['params'], 'shipping');
     $shipping_address = $formatter->format(array('data' => $shipping_address));
     $shipping_address = $shipping_address['value'];
     if (isset($form_fields['address.billing'])) {
         $billing_address = shopHelper::getOrderAddress($order['params'], 'billing');
         $billing_address = $formatter->format(array('data' => $billing_address));
         $billing_address = $billing_address['value'];
         if ($billing_address === $shipping_address) {
             $billing_address = null;
         }
     } else {
         $billing_address = null;
     }
     // Order history
     $log_model = new shopOrderLogModel();
     $log = $log_model->getLog($order['id']);
     // Customer
     $contact = $customer = self::getCustomer($order);
     $top = array();
     foreach (array('email', 'phone') as $f) {
         if ($v = $contact->get($f, 'top,html')) {
             $top[] = array('id' => $f, 'name' => waContactFields::get($f)->getName(), 'value' => is_array($v) ? implode(', ', $v) : $v);
         }
     }
     // Workflow stuff: actions and state
     $workflow = new shopWorkflow();
     $workflow_state = $workflow->getStateById($order['state_id']);
     $workflow_buttons = array();
     foreach ($workflow_state->getActions() as $a_id => $action) {
         if ($a_id === 'edit' || $a_id === 'delete') {
             continue;
         }
         $workflow_buttons[] = $action->getButton();
     }
     $this->view->assign('top', $top);
     $this->view->assign('log', $log);
     $this->view->assign('order', $order);
     $this->view->assign('uniqid', uniqid('f'));
     $this->view->assign('customer', $customer);
     $this->view->assign('workflow_state', $workflow_state);
     $this->view->assign('workflow_buttons', $workflow_buttons);
     $this->view->assign('shipping_address', $shipping_address);
     $this->view->assign('billing_address', $billing_address);
     $this->view->assign('order_subtotal', $order_subtotal);
     $this->view->assign('currency', ifempty($order['currency'], wa()->getConfig()->getCurrency()));
     wa()->getResponse()->setTitle(_w('Order') . ' ' . $order['id_str']);
     parent::execute();
 }
Exemplo n.º 9
0
 public function getOrder($id, $extend = false, $escape = true)
 {
     $order = $this->getById($id);
     if (!$order) {
         return array();
     }
     $order_params_model = new shopOrderParamsModel();
     $order['params'] = $order_params_model->get($id);
     if ($order['contact_id']) {
         $contact = new waContact($order['contact_id']);
         $order['contact'] = array('id' => $order['contact_id'], 'name' => $contact->getName(), 'email' => $contact->get('email', 'default'), 'phone' => $contact->get('phone', 'default'));
         $config = wa('shop')->getConfig();
         $use_gravatar = $config->getGeneralSettings('use_gravatar');
         $gravatar_default = $config->getGeneralSettings('gravatar_default');
         if (!$contact->get('photo') && $use_gravatar) {
             $order['contact']['photo_50x50'] = shopHelper::getGravatar($order['contact']['email'], 50, $gravatar_default);
         } else {
             $order['contact']['photo_50x50'] = $contact->getPhoto(50);
         }
     } else {
         $order['contact'] = $this->extractConctactInfo($order['params']);
     }
     if (!empty($order['params']['coupon_id'])) {
         $coupon_model = new shopCouponModel();
         $coupon = $coupon_model->getById($order['params']['coupon_id']);
         $order['coupon'] = array();
         if ($coupon) {
             $order['coupon'] = $coupon;
         } else {
             if (!empty($order['params']['coupon_code'])) {
                 $order['coupon']['code'] = $order['params']['coupon_code'];
             }
         }
     }
     $order_items_model = new shopOrderItemsModel();
     $order['items'] = $order_items_model->getItems($id, $extend);
     if ($escape) {
         if (!empty($order['items'])) {
             foreach ($order['items'] as &$product) {
                 if (!empty($product['name'])) {
                     $product['name'] = htmlspecialchars($product['name']);
                 }
                 if (!empty($product['item']['name'])) {
                     $product['item']['name'] = htmlspecialchars($product['item']['name']);
                 }
                 if (!empty($product['skus'])) {
                     foreach ($product['skus'] as &$sku) {
                         if (!empty($sku['name'])) {
                             $sku['name'] = htmlspecialchars($sku['name']);
                         }
                         unset($sku);
                     }
                 }
                 if (!empty($product['services'])) {
                     foreach ($product['services'] as &$service) {
                         if (!empty($service['name'])) {
                             $service['name'] = htmlspecialchars($service['name']);
                         }
                         if (!empty($service['item']['name'])) {
                             $service['item']['name'] = htmlspecialchars($service['item']['name']);
                         }
                         if (!empty($service['variants'])) {
                             foreach ($service['variants'] as &$variant) {
                                 $variant['name'] = htmlspecialchars($variant['name']);
                                 unset($variant);
                             }
                         }
                         unset($service);
                     }
                 }
                 unset($product);
             }
         }
         $order['contact']['name'] = htmlspecialchars($order['contact']['name']);
     }
     return $order;
 }
Exemplo n.º 10
0
 public function execute()
 {
     $fm = new shopFollowupModel();
     $opm = new shopOrderParamsModel();
     $asm = new waAppSettingsModel();
     $olm = new shopOrderLogModel();
     $cm = new shopCustomerModel();
     $om = new shopOrderModel();
     $asm->set('shop', 'last_followup_cli', time());
     $view = wa()->getView();
     $empty_customer = $cm->getEmptyRow();
     $general = wa('shop')->getConfig()->getGeneralSettings();
     foreach ($fm->getAllEnabled() as $f) {
         $between_from = date('Y-m-d', strtotime($f['last_cron_time']) - 24 * 3600);
         $between_to = date('Y-m-d 23:59:59', time() - $f['delay'] - 10 * 3600);
         $orders = $om->where('paid_date >= ? AND paid_date < ?', $between_from, $between_to)->fetchAll('id');
         if ($orders) {
             $f_param_key = 'followup_' . $f['id'];
             // Params for all orders with one query
             $params = $opm->get(array_keys($orders));
             // Customer data for all orders with one query
             $cids = array();
             foreach ($orders as $o) {
                 $cids[] = $o['contact_id'];
             }
             $customers = $cm->getById($cids);
             $sent_count = 0;
             foreach ($orders as $o) {
                 try {
                     // Is there a recipient in the first place?
                     if (empty($o['contact_id'])) {
                         if (waSystemConfig::isDebug()) {
                             waLog::log("Unable to send follow-up #{$f['id']} for order #{$o['id']}: no contact_id");
                         }
                         continue;
                     }
                     // Check that this is the first order of this customer
                     if ($f['first_order_only']) {
                         $first_order_id = $om->select('MIN(id)')->where('contact_id=? AND paid_date IS NOT NULL', $o['contact_id'])->fetchField();
                         if ($first_order_id != $o['id']) {
                             if (waSystemConfig::isDebug()) {
                                 waLog::log("Skipping follow-up #{$f['id']} for order #{$o['id']}: not the first order of a customer.");
                             }
                             continue;
                         }
                     }
                     $o['params'] = ifset($params[$o['id']], array());
                     $source = 'backend';
                     if (!empty($o['params']['storefront'])) {
                         $source = rtrim($o['params']['storefront'], '/') . '/*';
                     }
                     if ($f['source'] && $f['source'] != $source) {
                         continue;
                     }
                     // Make sure we have not send follow-up for this order yet
                     if (isset($o['params'][$f_param_key])) {
                         if (waSystemConfig::isDebug()) {
                             waLog::log("Skipping follow-up #{$f['id']} for order #{$o['id']}: already sent before.");
                         }
                         continue;
                     }
                     shopHelper::workupOrders($o, true);
                     // Recipient info
                     $customer = ifset($customers[$o['contact_id']], $empty_customer);
                     $contact = new shopCustomer($o['contact_id']);
                     $email = $contact->get('email', 'default');
                     // this with throw exception if contact does not exist; that's ok
                     if (!$email) {
                         if (waSystemConfig::isDebug()) {
                             waLog::log("Unable to send follow-up #{$f['id']} for order #{$o['id']}: contact has no email");
                         }
                         continue;
                     }
                     $to = array($email => $contact->getName());
                     if (self::sendOne($f, $o, $customer, $contact, $to, $view, $general)) {
                         $sent_count++;
                         // Write to order log
                         $olm->add(array('order_id' => $o['id'], 'contact_id' => null, 'action_id' => '', 'text' => sprintf_wp("Follow-up <strong>%s</strong> (%s) sent to customer.", htmlspecialchars($f['name']), $f['id']), 'before_state_id' => $o['state_id'], 'after_state_id' => $o['state_id']));
                         // Write to order params
                         $opm->insert(array('order_id' => $o['id'], 'name' => $f_param_key, 'value' => date('Y-m-d H:i:s')));
                     } else {
                         waLog::log("Unable to send follow-up #{$f['id']} for order #{$o['id']}: waMessage->send() returned FALSE.");
                     }
                 } catch (Exception $e) {
                     waLog::log("Unable to send follow-up #{$f['id']} for order #{$o['id']}:\n" . $e);
                 }
             }
             /**
              * Notify plugins about sending followup
              * @event followup_send
              * @param array[string]int $params['sent_count'] number of emails successfully sent
              * @param array[string]int $params['id'] followup_id
              * @return void
              */
             $event_params = $f;
             $event_params['sent_count'] = $sent_count;
             wa()->event('followup_send', $event_params);
         }
         $fm->updateById($f['id'], array('last_cron_time' => $between_to));
     }
 }
Exemplo n.º 11
0
 public function execute()
 {
     $steps = $this->getConfig()->getCheckoutSettings();
     $current_step = waRequest::param('step', waRequest::request('step'));
     if (!$current_step) {
         $current_step = key($steps);
     }
     $title = _w('Checkout');
     if ($current_step == 'success') {
         $order_id = waRequest::get('order_id');
         if (!$order_id) {
             $order_id = wa()->getStorage()->get('shop/order_id');
             $payment_success = false;
         } else {
             $payment_success = true;
             $this->view->assign('payment_success', true);
         }
         if (!$order_id) {
             wa()->getResponse()->redirect(wa()->getRouteUrl('shop/frontend'));
         }
         $order_model = new shopOrderModel();
         $order = $order_model->getById($order_id);
         if ($order) {
             $order['_id'] = $order['id'];
         }
         if (!$payment_success) {
             $order_params_model = new shopOrderParamsModel();
             $order['params'] = $order_params_model->get($order_id);
             $order_items_model = new shopOrderItemsModel();
             $order['items'] = $order_items_model->getByField('order_id', $order_id, true);
             $payment = '';
             if (!empty($order['params']['payment_id'])) {
                 try {
                     /**
                      * @var waPayment $plugin
                      */
                     $plugin = shopPayment::getPlugin(null, $order['params']['payment_id']);
                     $payment = $plugin->payment(waRequest::post(), shopPayment::getOrderData($order, $plugin), true);
                 } catch (waException $ex) {
                     $payment = $ex->getMessage();
                 }
             }
             $order['id'] = shopHelper::encodeOrderId($order_id);
             $this->getResponse()->addGoogleAnalytics($this->getGoogleAnalytics($order));
         } else {
             $order['id'] = shopHelper::encodeOrderId($order_id);
         }
         $this->view->assign('order', $order);
         if (isset($payment)) {
             $this->view->assign('payment', $payment);
         }
     } else {
         $cart = new shopCart();
         if (!$cart->count() && $current_step != 'error') {
             $current_step = 'error';
             $this->view->assign('error', _w('Your shopping cart is empty. Please add some products to cart, and then proceed to checkout.'));
         }
         if ($current_step != 'error') {
             if (waRequest::method() == 'post') {
                 if (waRequest::post('wa_auth_login')) {
                     $login_action = new shopLoginAction();
                     $login_action->run();
                 } else {
                     $redirect = false;
                     foreach ($steps as $step_id => $step) {
                         if ($step_id == $current_step) {
                             $step_instance = $this->getStep($step_id);
                             if ($step_instance->execute()) {
                                 $redirect = true;
                             }
                         } elseif ($redirect) {
                             $this->redirect(wa()->getRouteUrl('/frontend/checkout', array('step' => $step_id)));
                         }
                     }
                     // last step
                     if ($redirect) {
                         if ($this->createOrder()) {
                             $this->redirect(wa()->getRouteUrl('/frontend/checkout', array('step' => 'success')));
                         }
                     }
                 }
             } else {
                 $this->view->assign('error', '');
             }
             $title .= ' - ' . $steps[$current_step]['name'];
             $steps[$current_step]['content'] = $this->getStep($current_step)->display();
             $this->view->assign('checkout_steps', $steps);
         }
     }
     $this->getResponse()->setTitle($title);
     $this->view->assign('checkout_current_step', $current_step);
     /**
      * @event frontend_checkout
      * @return array[string]string $return[%plugin_id%] html output
      */
     $event_params = array('step' => $current_step);
     $this->view->assign('frontend_checkout', wa()->event('frontend_checkout', $event_params));
     if (waRequest::isXMLHttpRequest()) {
         $this->setThemeTemplate('checkout.' . $current_step . '.html');
     } else {
         $this->setLayout(new shopFrontendLayout());
         $this->setThemeTemplate('checkout.html');
     }
 }
Exemplo n.º 12
0
 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;
             }
         }
     }
 }
Exemplo n.º 13
0
 public function execute()
 {
     $id = waRequest::get('order_id');
     if (!$id) {
         throw new waException("Unknown order", 404);
     }
     $order = $this->getOrder($id);
     if (!$order) {
         $id = shopHelper::decodeOrderId($id);
         $order = $this->getOrder($id);
         if (!$order) {
             throw new waException("Unkown order", 404);
         }
     }
     $product_ids = array();
     foreach ($order['items'] as $item) {
         if ($item['type'] == 'product') {
             $product_ids[] = $item['product_id'];
         }
     }
     $product_ids = array_unique($product_ids);
     $form_id = waRequest::get('form_id');
     if (strpos($form_id, '.')) {
         list($type, $form) = explode('.', $form_id, 2);
     } else {
         $form = null;
         $type = $form_id;
     }
     $order_params_model = new shopOrderParamsModel();
     $params = $order_params_model->get($order['id']);
     $plugin = self::getPlugin($type, ifempty($params[$type . '_id']));
     if ($type == 'shipping') {
         /* add weight info only for shipping modules */
         $feature_model = new shopFeatureModel();
         $f = $feature_model->getByCode('weight');
         if (!$f) {
             $weights = array();
         } else {
             $values_model = $feature_model->getValuesModel($f['type']);
             $weights = $values_model->getProductValues($product_ids, $f['id']);
         }
         if ($weights) {
             $dimension = shopDimension::getInstance()->getDimension('weight');
             $weight_unit = $plugin->allowedWeightUnit();
             $m = null;
             if ($weight_unit != $dimension['base_unit']) {
                 $m = $dimension['units'][$weight_unit]['multiplier'];
             }
             foreach ($order['items'] as &$item) {
                 if ($item['type'] == 'product') {
                     if (isset($weights['skus'][$item['sku_id']])) {
                         $w = $weights['skus'][$item['sku_id']];
                     } else {
                         $w = isset($weights[$item['product_id']]) ? $weights[$item['product_id']] : 0;
                     }
                     if ($m !== null) {
                         $w = $w / $m;
                     }
                     $item['weight'] = $w;
                 }
             }
             unset($item);
         }
     }
     if (!$plugin) {
         throw new waException(_w('Printform not found'), 404);
     }
     print $plugin->displayPrintForm(ifempty($form, $plugin->getId()), shopPayment::getOrderData($order, $plugin));
     exit;
 }
Exemplo n.º 14
0
 /**
  *
  * formalize order data
  * @param string|array $order order ID or order data
  * @param waPayment $payment_plugin
  * @return waOrder
  */
 public static function getOrderData($order, $payment_plugin = null)
 {
     if (!is_array($order)) {
         $order_id = shopHelper::decodeOrderId($encoded_order_id = $order);
         if (!$order_id) {
             $order_id = $encoded_order_id;
             $encoded_order_id = shopHelper::encodeOrderId($order_id);
         }
         $om = new shopOrderModel();
         $order = $om->getOrder($order_id);
         if (!$order) {
             return null;
         }
         $order['id_str'] = $encoded_order_id;
     }
     if (!isset($order['id_str'])) {
         $order['id_str'] = shopHelper::encodeOrderId($order['id']);
     }
     if (!isset($order['params'])) {
         $order_params_model = new shopOrderParamsModel();
         $order['params'] = $order_params_model->get($order['id']);
     }
     $convert = false;
     if ($payment_plugin && method_exists($payment_plugin, 'allowedCurrency')) {
         $currency = $payment_plugin->allowedCurrency();
         $total = $order['total'];
         $currency_id = $order['currency'];
         if ($currency !== true) {
             $currency = (array) $currency;
             if (!in_array($order['currency'], $currency)) {
                 $convert = true;
                 $total = shop_currency($total, $order['currency'], $currency_id = reset($currency), false);
             }
         }
     } else {
         $currency_id = $order['currency'];
         $total = $order['total'];
     }
     $items = array();
     if (!empty($order['items'])) {
         foreach ($order['items'] as $item) {
             ifempty($item['price'], 0.0);
             if ($convert) {
                 $item['price'] = shop_currency($item['price'], $order['currency'], $currency_id, false);
             }
             $items[] = array('id' => ifset($item['id']), 'name' => ifset($item['name']), 'sku' => ifset($item['sku_code']), 'description' => '', 'price' => $item['price'], 'quantity' => ifset($item['quantity'], 0), 'total' => $item['price'] * $item['quantity'], 'type' => ifset($item['type'], 'product'), 'product_id' => ifset($item['product_id']));
             if (isset($item['weight'])) {
                 $items[count($items) - 1]['weight'] = $item['weight'];
             }
         }
     }
     $empty_address = array('firstname' => '', 'lastname' => '', 'country' => '', 'region' => '', 'city' => '', 'street' => '', 'zip' => '');
     $shipping_address = array_merge($empty_address, shopHelper::getOrderAddress($order['params'], 'shipping'));
     $billing_address = array_merge($empty_address, shopHelper::getOrderAddress($order['params'], 'billing'));
     if (!count(array_filter($billing_address, 'strlen'))) {
         $billing_address = $shipping_address;
     }
     ifset($order['shipping'], 0.0);
     ifset($order['discount'], 0.0);
     ifset($order['tax'], 0.0);
     if ($convert) {
         $order['tax'] = shop_currency($order['tax'], $order['currency'], $currency_id, false);
         $order['shipping'] = shop_currency($order['shipping'], $order['currency'], $currency_id, false);
         $order['discount'] = shop_currency($order['discount'], $order['currency'], $currency_id, false);
     }
     $order_data = array('id_str' => ifempty($order['id_str'], $order['id']), 'id' => $order['id'], 'contact_id' => $order['contact_id'], 'datetime' => ifempty($order['create_datetime']), 'description' => sprintf(_w('Payment for order %s'), ifempty($order['id_str'], $order['id'])), 'update_datetime' => ifempty($order['update_datetime']), 'paid_datetime' => empty($order['paid_date']) ? null : $order['paid_date'] . ' 00:00:00', 'total' => ifempty($total, $order['total']), 'currency' => ifempty($currency_id, $order['currency']), 'discount' => $order['discount'], 'tax' => $order['tax'], 'payment_name' => ifset($order['params']['payment_name'], ''), 'billing_address' => $billing_address, 'shipping' => $order['shipping'], 'shipping_name' => ifset($order['params']['shipping_name'], ''), 'shipping_address' => $shipping_address, 'items' => $items, 'comment' => ifempty($order['comment'], ''), 'params' => $order['params']);
     return waOrder::factory($order_data);
 }
Exemplo n.º 15
0
 public function execute()
 {
     $encoded_order_id = waRequest::param('id');
     $order_id = shopHelper::decodeOrderId($encoded_order_id);
     if (!$order_id) {
         // fall back to non-encoded id
         $order_id = $encoded_order_id;
         $encoded_order_id = shopHelper::encodeOrderId($order_id);
     }
     // Check that order exists and belongs to this user
     $om = new shopOrderModel();
     $order = $om->getOrder($order_id);
     if (!$order || !$this->isAuth($order)) {
         throw new waException(_w('Order not found'), 404);
     }
     if ($order['paid_date']) {
         foreach ($order['items'] as &$i) {
             if (!empty($i['file_name'])) {
                 $i['download_link'] = wa()->getRouteUrl('/frontend/myOrderDownload', array('id' => $order['id'], 'code' => $order['params']['auth_code'], 'item' => $i['id']), true);
             }
         }
         unset($i);
     }
     $workflow = new shopWorkflow();
     $order_params_model = new shopOrderParamsModel();
     $order['params'] = $order_params_model->get($order['id']);
     $order['id_str'] = $encoded_order_id;
     $order['state'] = $workflow->getStateById($order['state_id']);
     // Order subtotal
     $subtotal = 0;
     foreach ($order['items'] as $item) {
         $subtotal += $item['price'] * $item['quantity'];
     }
     // Order comment
     $lm = new shopOrderLogModel();
     $l = $lm->getByField(array('action_id' => 'create', 'order_id' => $order['id']));
     $order['comment'] = ifempty($l['text']);
     $order['payment_name'] = ifset($order['params']['payment_name'], '');
     $order['shipping_name'] = ifset($order['params']['shipping_name'], '');
     // Shipping and billing addresses
     $settings = wa('shop')->getConfig()->getCheckoutSettings();
     $form_fields = ifset($settings['contactinfo']['fields'], array());
     $formatter = new waContactAddressSeveralLinesFormatter();
     $shipping_address = shopHelper::getOrderAddress($order['params'], 'shipping');
     if ($shipping_address) {
         $shipping_address = $formatter->format(array('data' => $shipping_address));
         $shipping_address = $shipping_address['value'];
     }
     if (isset($form_fields['address.billing'])) {
         $billing_address = shopHelper::getOrderAddress($order['params'], 'billing');
         $billing_address = $formatter->format(array('data' => $billing_address));
         $billing_address = $billing_address['value'];
     } else {
         $billing_address = null;
     }
     if (wa()->getUser()->getId() == $order['contact_id']) {
         $contact = wa()->getUser();
     } else {
         $contact = new waContact($order['contact_id']);
     }
     $payment = '';
     if (!empty($order['params']['payment_id']) && !$order['paid_date']) {
         try {
             $plugin = shopPayment::getPlugin(null, $order['params']['payment_id']);
             $payment = $plugin->payment(waRequest::post(), shopPayment::getOrderData($order, $plugin), false);
         } catch (waException $ex) {
             $payment = $ex->getMessage();
         }
     }
     $this->view->assign('payment', $payment);
     $tracking = '';
     if (!empty($order['params']['shipping_id']) && !empty($order['params']['tracking_number'])) {
         try {
             $plugin = shopShipping::getPlugin(null, $order['params']['shipping_id']);
             $tracking = $plugin->tracking($order['params']['tracking_number']);
         } catch (waException $ex) {
             $tracking = $ex->getMessage();
         }
     }
     $this->view->assign('tracking', $tracking);
     $this->view->assign('order', $order);
     $this->view->assign('contact', $contact);
     $this->view->assign('shipping_address', $shipping_address);
     $this->view->assign('billing_address', $billing_address);
     $this->view->assign('subtotal', $subtotal);
     // Set up layout and template from theme
     $this->setThemeTemplate('my.order.html');
     $this->view->assign('my_nav_selected', 'orders');
     if (!waRequest::isXMLHttpRequest()) {
         $this->setLayout(new shopFrontendLayout());
         $this->getResponse()->setTitle(_w('Order') . ' ' . $encoded_order_id);
         $this->view->assign('breadcrumbs', self::getBreadcrumbs());
         $this->layout->assign('nofollow', true);
     }
 }
Exemplo n.º 16
0
 public function postExecute($order_id = null, $result = null)
 {
     if (!$result) {
         return;
     }
     $order_model = new shopOrderModel();
     if (is_array($order_id)) {
         $order = $order_id;
         $order_id = $order['id'];
     } else {
         $order = $order_model->getById($order_id);
     }
     $data = is_array($result) ? $result : array();
     $data['order_id'] = $order_id;
     $data['action_id'] = $this->getId();
     $data['before_state_id'] = $order['state_id'];
     if ($this->state_id) {
         $data['after_state_id'] = $this->state_id;
     } else {
         $data['after_state_id'] = $order['state_id'];
     }
     $order_log_model = new shopOrderLogModel();
     $data['id'] = $order_log_model->add($data);
     $update = isset($result['update']) ? $result['update'] : array();
     $update['update_datetime'] = date('Y-m-d H:i:s');
     $data['update'] = $update;
     if ($this->state_id) {
         $update['state_id'] = $this->state_id;
     }
     $order_model->updateById($order['id'], $update);
     $order_params_model = new shopOrderParamsModel();
     if (isset($update['params'])) {
         $order_params_model->set($order['id'], $update['params'], false);
     }
     $order['params'] = $order_params_model->get($order_id);
     // send notifications
     shopNotifications::send('order.' . $this->getId(), array('order' => $order, 'customer' => new waContact($order['contact_id']), 'status' => $this->getWorkflow()->getStateById($data['after_state_id'])->getName(), 'action_data' => $data));
     /**
      * @event order_action.callback
      * @event order_action.pay
      * @event order_action.ship
      * @event order_action.process
      * @event order_action.delete
      * @event order_action.restore
      * @event order_action.complete
      * @event order_action.comment
      *
      * @param array[string]mixed $data
      * @param array[string]int $data['order_id']
      * @param array[string]int $data['action_id']
      * @param array[string]int $data['before_state_id']
      * @param array[string]int $data['after_state_id']
      * @param array[string]int $data['id'] Order log record id
      */
     wa('shop')->event('order_action.' . $this->getId(), $data);
     return $data;
 }