public function actionPlaceOrder() { $shopCart = $this->getShoppingCart(); $user = $this->getLoggedUser(); if (!$shopCart->hasItems()) { return $this->render('index'); } $destination_id = Yii::$app->request->post('destination_id'); if (empty($destination_id)) { return $this->render('checkout', ['error' => Yii::t('app', 'Select a destination')]); } /** @var UserShippingAddress $destination */ $destination = UserShippingAddress::findOne($destination_id); if (is_null($destination) || $destination->user_id != $user->id) { return $this->render('checkout', ['error' => Yii::t('app', 'Select a destination')]); } $order = null; if ($shopCart->getTotal() > 0) { $payment_id = Yii::$app->request->post('payment_id'); if (empty($payment_id)) { return $this->render('checkout', ['error' => Yii::t('app', 'Select a payment method')]); } /** @var UserAuthorizeNet $uan */ $uan = UserAuthorizeNet::findOne($payment_id); if (is_null($uan) || $uan->user_id != $user->id) { return $this->render('checkout', ['error' => Yii::t('app', 'Select a payment method')]); } $order = Order::generateFromCart($shopCart, $user, $destination); $order->message = isset($_REQUEST['message']) ? $_REQUEST['message'] : ''; $order->status = Order::STATUS_PENDING; $order->save(); $uan->postPayment($order); } else { $order = Order::generateFromCart($shopCart, $user, $destination); $order->status = Order::STATUS_APPROVED; $order->message = isset($_REQUEST['message']) ? $_REQUEST['message'] : ''; $order->save(); } if (is_null($order)) { return $this->redirect(['index']); } else { //generate emails $order->generateEmailToBuyer(); $order->generateEmailToAdmin(); if ($order->status == Order::STATUS_APPROVED) { $order->generateEmailToDestination(); $order->generateEmailToProviders(); } StoreUtils::sendPendingEmails(); $shopCart->items = []; $user->resetUserShopCart(); return $this->render('order_result', ['model' => $order]); } }