Exemple #1
0
 public function actionIndex()
 {
     if (!Yii::app()->request->isAjaxRequest) {
         if (Yii::app()->user->isGuest) {
             $userId = Yii::app()->session['user']['id'];
         } else {
             $userId = Yii::app()->user->id;
         }
         try {
             $productType = Yii::app()->user->productType;
             $productName = Yii::app()->user->productName;
             $productNum = Yii::app()->user->productNum;
             $productPrice = Yii::app()->user->productPrice;
         } catch (CException $e) {
             echo '订单创建失败';
             die;
         }
         $jsapi = new WxJsPayHelper();
         $openid = $jsapi->GetOpenid();
         $user = User::model()->findByAttributes(array('id' => $userId));
         $date = strtotime($user->deadDate) < strtotime(date('Ymd')) ? date('U') : strtotime($user->deadDate);
         $wechat = Yii::app()->params['partner']['wechat'];
         $order = new COrder();
         $orderId = $order->create(array('productId' => $productType, 'userId' => $userId, 'type' => 1, 'price' => $productPrice, 'orderTime' => date('YmdHis')));
         $orderId = $orderId['data']['orderId'];
         $times = Yii::app()->db->createCommand()->setText('select times from product where status!=0 and id=' . $productType)->queryRow();
         for ($i = 0; $i < $productNum; $i++) {
             $rtuorder = $order->createProduct(array('orderId' => $orderId, 'totalTimes' => $times['times'], 'usedTimes' => 0, 'startDate' => date('Ymd', $date), 'endDate' => date('Ymd', $date + Assist::timestampToMonthTimestamp($date) - 86400)));
             $date = $date + Assist::timestampToMonthTimestamp($date);
         }
         $input = new WxPayUnifiedOrder();
         $input->SetBody($productName);
         $input->SetAttach(date('Ymd', $date));
         $input->SetOut_trade_no((string) $orderId);
         $input->SetTotal_fee($productNum * $productPrice / 10000);
         $input->SetTime_start(date('YmdHis'));
         $input->SetTime_expire(Assist::getOrderExpireTime(time()));
         $input->SetProduct_id($productType);
         $input->SetNotify_url('http://hubapp.livenaked.com' . $wechat['payment']['notify']);
         $input->SetTrade_type('JSAPI');
         $input->SetOpenid($openid);
         $bill = WxPayApi::unifiedOrder($input);
         $jsApiParameters = $jsapi->GetJsApiParameters($bill);
         $this->bodyCss = 'orderDetail';
         $this->render('index', array('type' => $productType, 'name' => $productName, 'num' => $productNum, 'price' => $productPrice, 'jsparams' => $jsApiParameters));
     }
 }