/** @inheritdoc} */ public function clean() { $response = $this->ms2->invokeEvent('msOnBeforeEmptyOrder', array('order' => $this)); if (!$response['success']) { return $this->error($response['message']); } $this->order = array(); $response = $this->ms2->invokeEvent('msOnEmptyOrder', array('order' => $this)); if (!$response['success']) { return $this->error($response['message']); } return $this->success('', array()); }
/** @inheritdoc} */ public function getCost($with_cart = true, $only_cost = false) { $response = $this->ms2->invokeEvent('msOnBeforeGetOrderCost', array('order' => $this, 'cart' => $this->ms2->cart, 'with_cart' => $with_cart, 'only_cost' => $only_cost)); if (!$response['success']) { return $this->error($response['message']); } $cart = $this->ms2->cart->status(); $cost = $with_cart ? $cart['total_cost'] : 0; /* @var msDelivery $delivery */ if (!empty($this->order['delivery']) && ($delivery = $this->modx->getObject('msDelivery', $this->order['delivery']))) { $cost = $delivery->getCost($this, $cost); } /* @var msPayment $payment */ if (!empty($this->order['payment']) && ($payment = $this->modx->getObject('msPayment', $this->order['payment']))) { $cost = $payment->getCost($this, $cost); } $response = $this->ms2->invokeEvent('msOnGetOrderCost', array('order' => $this, 'cart' => $this->ms2->cart, 'with_cart' => $with_cart, 'only_cost' => $only_cost, 'cost' => $cost)); if (!$response['success']) { return $this->error($response['message']); } $cost = $response['data']['cost']; return $only_cost ? $cost : $this->success('', array('cost' => $cost)); }
/** * @param array $data * * @return array|string */ public function createPayment(array $data) { if (!$this->modx->user->isAuthenticated($this->modx->context->key)) { return $this->ms2->error($this->modx->lexicon('ms2_profile_err_auth')); } // Call system event $response = $this->ms2->invokeEvent('msOnSubmitOrder', array('data' => $data, 'order' => $this->ms2->order)); if (!$response['success']) { return $this->ms2->error($response['message']); } if (!empty($response['data']['data'])) { $data = array_merge($data, $response['data']['data']); $this->ms2->order->set($data); } // Check required fields $errors = array(); if (empty($data['sum']) || $data['sum'] < $this->config['minSum']) { $errors['sum'] = $this->modx->lexicon('ms2_profile_err_min_sum', array('min_sum' => $this->config['minSum'])); } elseif (!empty($maxSum) && $data['sum'] > $this->config['maxSum']) { $errors['sum'] = $this->modx->lexicon('ms2_profile_err_max_sum', array('max_sum' => $this->config['maxSum'])); } if (empty($data['payment'])) { $errors['payment'] = $this->modx->lexicon('ms2_profile_err_payment', array('min_sum' => $this->config['minSum'])); } if (!empty($errors)) { return $this->ms2->error($this->modx->lexicon('ms2_profile_err_form'), $errors); } // Create new order /** @var msOrder $order */ $order = $this->modx->newObject('msOrder', array('user_id' => $this->modx->user->id, 'createdon' => date('Y-m-d H:i:s'), 'num' => $this->ms2->order->getnum(), 'delivery' => 0, 'payment' => $data['payment'], 'cart_cost' => $data['sum'], 'weight' => 0, 'delivery_cost' => 0, 'cost' => $data['sum'], 'status' => 0, 'context' => $this->ms2->config['ctx'], 'properties' => array('account_charge' => true))); $products = array($this->modx->newObject('msOrderProduct', array('product_id' => 0, 'name' => $this->modx->lexicon('ms2_profile_charge'), 'price' => $data['sum'], 'cost' => $data['sum']))); $order->addMany($products); $response = $this->ms2->invokeEvent('msOnBeforeCreateOrder', array('msOrder' => $order, 'order' => $this->ms2->order)); if (!$response['success']) { return $this->ms2->error($response['message']); } if ($order->save()) { $response = $this->ms2->invokeEvent('msOnCreateOrder', array('msOrder' => $order, 'order' => $this->ms2->order)); if (!$response['success']) { return $this->ms2->error($response['message']); } 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->ms2->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 (!empty($response['data']['redirect'])) { $this->modx->sendRedirect($response['data']['redirect']); } elseif (!empty($response['data']['msorder'])) { $this->modx->sendRedirect($this->modx->context->makeUrl($this->modx->resource->id, array('msorder' => $response['data']['msorder']))); } else { $this->modx->sendRedirect($this->modx->context->makeUrl($this->modx->resource->id)); } } else { $this->modx->sendRedirect($this->modx->context->makeUrl($this->modx->resource->id, array('msorder' => $response['data']['msorder']))); } } }