예제 #1
0
 public function actionPayment($id)
 {
     $cart = $this->getCart();
     $add = CheckoutAddress::model()->findByPk($id);
     $link;
     $onlinePaymentMethodName;
     $extrapObj = array();
     $orderArray = array();
     $classArray = array();
     if (!empty($add) && !empty($cart['items'])) {
         if (isset($_POST['Payment']['payment_method'])) {
             $pay_method = $_POST['Payment']['payment_method'];
             $cart['payment'] = $pay_method == 4 ? 2 : $pay_method;
             $paymentType = is_numeric($pay_method) ? 'others' : 'online';
             $order = new Order();
             $order->order_status_id = 1;
             $order->total = $this->getCartTotal();
             $order->ip = Yii::app()->request->userHostAddress;
             $order->user_agent = Yii::app()->request->userAgent;
             $order->firstname = $add->firstname;
             $order->lastname = $add->lastname;
             $order->email = $add->email;
             $order->telephone = $add->telephone;
             $order->payment_firstname = $add->firstname;
             $order->payment_lastname = $add->lastname;
             $order->payment_company = '';
             $order->payment_tax_id = 0;
             $order->payment_address_1 = $add->address_1;
             $order->payment_address_2 = $add->address_2;
             $order->payment_city = $add->city;
             $order->payment_postcode = $add->postal_code;
             $order->payment_country_id = $add->country_id;
             $order->payment_zone_id = $add->zone_id;
             $order->payment_method = $cart['payment'];
             $order->payment_code = uniqid() . rand(1, 9);
             $order->shipping_firstname = $add->firstname;
             $order->shipping_lastname = $add->lastname;
             $order->shipping_company = '';
             $order->shipping_address_1 = $add->address_1;
             $order->shipping_address_2 = $add->address_2;
             $order->shipping_city = $add->city;
             $order->shipping_postcode = $add->postal_code;
             $order->shipping_country_id = $add->country_id;
             $order->shipping_zone_id = $add->zone_id;
             $order->shipping_method = 2;
             if ($add->zone_id == 2412) {
                 $order->shipping_method = 1;
             }
             $storeid = UtilityHelper::yiiparam('storeID');
             $store = Store::model()->findByPk($storeid);
             $order->store_id = $storeid;
             $order->store_name = $store->name;
             $order->store_url = $store->url;
             $order->payment_country = Country::model()->findByPk($order->payment_country_id)->name;
             $order->payment_zone = Zone::model()->findByPk($order->payment_zone_id)->name;
             $order->shipping_country = $order->payment_country;
             $order->shipping_zone = $order->payment_zone;
             if ($order->save()) {
                 foreach ($cart['items'] as $product) {
                     $orderproduct = new OrderProduct();
                     $orderproduct->order_id = $order->id;
                     $orderproduct->product_id = $product['product_id'];
                     $orderproduct->name = $product['name'];
                     $orderproduct->model = $product['model'];
                     $orderproduct->quantity = $product['quantity'];
                     $orderproduct->price = $product['price'];
                     $orderproduct->total = $product['total'];
                     $orderproduct->tax = 0;
                     if ($orderproduct->save()) {
                         if (!empty($product['option'])) {
                             foreach ($product['option'] as $orderoption) {
                                 $orderoption->order_id = $order->id;
                                 $orderoption->order_product_id = $orderproduct->id;
                                 $orderoption->save();
                             }
                         }
                     } else {
                         Yii::log("CartPaymentOrderProductErrors: " . CVarDumper::dumpAsString($orderproduct->getErrors()), CLogger::LEVEL_ERROR, "cart.actions.payment");
                     }
                 }
                 //Yii::app()->user->setState('user_cart', NULL);
                 $extrap = array();
                 //
                 switch ($paymentType) {
                     case 'others':
                         // Will need to loop through the ids for non online payment to do this properly
                         if ($pay_method == 1) {
                             $link = '_cash';
                             $extrap = array('orderID' => $order->id, 'total' => $order->total);
                             UtilityHelper::changeOrderStatus($order->id);
                         } else {
                             if ($pay_method == 3) {
                                 $link = '_bank';
                                 $extrap = array('orderID' => $order->id, 'total' => $order->total);
                             }
                         }
                         break;
                     case 'online':
                         $subClasses = new PaymentGatewayHelper();
                         $subClassesArray = $subClasses->getSubClasses();
                         foreach ($subClassesArray as $class => $payOption) {
                             ${$class} = new $class();
                             $onlinePaymentMethodName = ${$class}->getPaymentName();
                             if ($pay_method == $onlinePaymentMethodName) {
                                 $link = ${$class}->getViewLink();
                                 ${$class}->setOrderObj($order);
                                 $extrapObj = ${$class}->getOrderObj();
                                 foreach ($extrapObj as $field => $value) {
                                     $orderArray[$field] = $value;
                                 }
                                 $link = ${$class}->getViewLink();
                                 $extrap = array('orderID' => $orderArray['id'], 'total' => $orderArray['total']);
                                 ${$class}->regPaymentTransaction($order);
                                 $classArray['pay_method'] = $pay_method;
                             } else {
                                 continue;
                             }
                         }
                         break;
                     default:
                         break;
                 }
                 if (Yii::app()->getRequest()->getIsAjaxRequest()) {
                     $this->renderPartial($link, array_merge(array('ref' => $order->payment_code), $extrap, $classArray), false, true);
                     Yii::app()->end();
                 }
                 $this->render($link, array_merge(array('ref' => $order->payment_code), $extrap, $classArray));
                 Yii::app()->end();
             } else {
                 Yii::log("CartPaymentErrors: " . CVarDumper::dumpAsString($order->getErrors()), CLogger::LEVEL_ERROR, "cart.actions.payment");
             }
         }
         if (Yii::app()->getRequest()->getIsAjaxRequest()) {
             $this->renderPartial('_payment', array('app' => $add), false, true);
             Yii::app()->end();
         }
         $this->render('payment', array('app' => $add));
         Yii::app()->end();
     }
     $this->redirect(array('cart'));
 }
예제 #2
0
 public function validateAddress()
 {
     if (isset($_POST['CheckoutAddress'])) {
         $valid = true;
         foreach ($_POST['CheckoutAddress'] as $i => $item) {
             $model = new CheckoutAddress();
             if (isset($_POST['CheckoutAddress'][$i])) {
                 $model->attributes = $_POST['CheckoutAddress'][$i];
                 $model->user_id = 0;
                 $valid = $model->validate() && $valid;
                 $this->_address[$i] = $model;
             }
         }
         if (!$valid) {
             return false;
         }
     }
     return true;
 }
 public function actionCheckout()
 {
     $cart = Yii::app()->user->getState('special_order');
     if (!empty($cart)) {
         $user = Yii::app()->user;
         $address = array();
         //$address = Address::model()->findAll('id=:id', array(':id'=>41));
         if (!$user->isGuest) {
             $address = Address::model()->findAll('user_id=:id AND country_id=:c_id', array(':id' => $user->id, ':c_id' => 156));
         }
         if (!empty($_GET['golp']) && is_numeric($_GET['golp'])) {
             $id = 0;
             $id = $_GET['golp'];
             $naddress = Address::model()->findAll('user_id=:user_id AND id=:id', array(':user_id' => $user->id, ':id' => $id));
             $model = CheckoutAddress::model()->findByPk($id);
             if (empty($naddress)) {
                 Yii::app()->user->setFlash(TbHtml::ALERT_COLOR_ERROR, 'The address you selected is invalid');
                 if (Yii::app()->getRequest()->getIsAjaxRequest()) {
                     $this->renderPartial('/cart/_shipping', array('model' => $model, 'address' => $address), false, true);
                     Yii::app()->end();
                 }
                 $this->render('/cart/_shipping', array('model' => $model, 'address' => $address));
                 Yii::app()->end();
             }
             //Yii::app()->user->setState('user_cart', $cart);
             if (Yii::app()->getRequest()->getIsAjaxRequest()) {
                 $this->renderPartial('_view', array('model' => $order), false, true);
             } else {
                 $this->render('view', array('model' => $order));
             }
             Yii::app()->end();
         } else {
             $model = new CheckoutAddress();
         }
         if (isset($_POST['Address'])) {
             $_POST['CheckoutAddress'] = $_POST['Address'];
         }
         if (isset($_POST['CheckoutAddress'])) {
             $model->attributes = $_POST['CheckoutAddress'];
             $model->user_id = 0;
             if (!empty($user->id)) {
                 $model->user_id = $user->id;
             }
             $model->country_id = 156;
             if ($model->save()) {
                 $order = new SpecialOrder();
                 $order->product_name = $cart['product_name'];
                 $order->product_quantity = $cart['product_quantity'];
                 $order->product_colour = $cart['product_colour'];
                 $order->specification = $cart['specification'];
                 $order->comment = $cart['comment'];
                 $order->email = $model->email;
                 $order->telephone = $model->telephone;
                 $order->firstname = $model->firstname;
                 $order->lastname = $model->lastname;
                 $order->address_1 = $model->address_1;
                 $order->address_2 = $model->address_2;
                 $order->city = $model->city;
                 $order->postal_code = $model->postal_code;
                 $order->country_id = 156;
                 $order->zone_id = $model->zone_id;
                 $order->ip = Yii::app()->request->userHostAddress;
                 $order->user_agent = Yii::app()->request->userAgent;
                 $order->order_status_id = 1;
                 $order->payment_code = uniqid() . rand(1, 9);
                 $order->save();
                 //Yii::app()->user->setState('user_cart', $cart);
                 if (Yii::app()->getRequest()->getIsAjaxRequest()) {
                     $this->renderPartial('_view', array('model' => $order), false, true);
                 } else {
                     $this->render('view', array('model' => $order));
                 }
                 Yii::app()->end();
             }
         }
         if (Yii::app()->getRequest()->getIsAjaxRequest()) {
             $this->renderPartial('/cart/_shipping', array('model' => $model, 'address' => $address), false, true);
             Yii::app()->end();
         }
         $this->render('/cart/_shipping', array('model' => $model, 'address' => $address));
         Yii::app()->end();
     }
     $this->redirect(array('specialorder'));
 }