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'));
 }