/** @inheritdoc} */
 public function submit($data = array())
 {
     $response = $this->ms2->invokeEvent('msOnSubmitOrder', array('data' => $data, 'order' => $this));
     if (!$response['success']) {
         return $this->error($response['message']);
     }
     if (!empty($response['data']['data'])) {
         $this->set($response['data']['data']);
     }
     $response = $this->getDeliveryRequiresFields();
     if ($this->ms2->config['json_response']) {
         $response = $this->modx->fromJSON($response);
     }
     $requires = $response['data']['requires'];
     $errors = array();
     foreach ($requires as $v) {
         if (!empty($v) && empty($this->order[$v])) {
             $errors[] = $v;
         }
     }
     if (!empty($errors)) {
         return $this->error('ms2_order_err_requires', $errors);
     }
     $user_id = $this->ms2->getCustomerId();
     $cart_status = $this->ms2->cart->status();
     $delivery_cost = $this->getCost(false, true);
     $createdon = date('Y-m-d H:i:s');
     /* @var msOrder $order */
     $order = $this->modx->newObject('msOrder');
     $order->fromArray(array('user_id' => $user_id, 'createdon' => $createdon, 'num' => $this->getnum(), 'delivery' => $this->order['delivery'], 'payment' => $this->order['payment'], 'cart_cost' => $cart_status['total_cost'], 'weight' => $cart_status['total_weight'], 'delivery_cost' => $delivery_cost, 'cost' => $cart_status['total_cost'] + $delivery_cost, 'status' => 0, 'context' => $this->ms2->config['ctx']));
     // Adding address
     /* @var msOrderAddress $address */
     $address = $this->modx->newObject('msOrderAddress');
     $address->fromArray(array_merge($this->order, array('user_id' => $user_id, 'createdon' => $createdon)));
     $order->addOne($address);
     // Adding products
     $cart = $this->ms2->cart->get();
     $products = array();
     foreach ($cart as $v) {
         /* @var msOrderProduct $product */
         $product = $this->modx->newObject('msOrderProduct');
         $product->fromArray(array_merge($v, array('product_id' => $v['id'], 'cost' => $v['price'] * $v['count'])));
         $products[] = $product;
     }
     $order->addMany($products);
     $response = $this->ms2->invokeEvent('msOnBeforeCreateOrder', array('msOrder' => $order, 'order' => $this));
     if (!$response['success']) {
         return $this->error($response['message']);
     }
     if ($order->save()) {
         $response = $this->ms2->invokeEvent('msOnCreateOrder', array('msOrder' => $order, 'order' => $this));
         if (!$response['success']) {
             return $this->error($response['message']);
         }
         $this->ms2->cart->clean();
         $this->clean();
         if (empty($_SESSION['minishop2']['orders'])) {
             $_SESSION['minishop2']['orders'] = array();
         }
         $_SESSION['minishop2']['orders'][] = $order->get('id');
         // Trying to set status "new"
         $response = $this->ms2->changeOrderStatus($order->get('id'), 1);
         if ($response !== true) {
             return $this->error($response, array('msorder' => $order->get('id')));
         } elseif ($payment = $this->modx->getObject('msPayment', array('id' => $order->get('payment'), 'active' => 1))) {
             $response = $payment->send($order);
             if ($this->config['json_response']) {
                 @session_write_close();
                 exit(is_array($response) ? $this->modx->toJSON($response) : $response);
             } else {
                 if (!empty($response['data']['redirect'])) {
                     $this->modx->sendRedirect($response['data']['redirect']);
                     exit;
                 } elseif (!empty($response['data']['msorder'])) {
                     $this->modx->sendRedirect($this->modx->makeUrl($this->modx->resource->id), array('msorder' => $response['data']['msorder']));
                     exit;
                 } else {
                     $this->modx->sendRedirect($this->modx->makeUrl($this->modx->resource->id));
                     exit;
                 }
             }
         } else {
             if ($this->ms2->config['json_response']) {
                 return $this->success('', array('msorder' => $order->get('id')));
             } else {
                 $this->modx->sendRedirect($this->modx->makeUrl($this->modx->resource->id), array('msorder' => $response['data']['msorder']));
                 exit;
             }
         }
     }
     return $this->error();
 }