Ejemplo n.º 1
0
 function payAction()
 {
     $request = $this->getRequest();
     if ('POST' == $request->getMethod()) {
         $ssotoken = $this->post()->get("ssotoken");
         $payType = $this->post()->get("payType");
         $resource = $this->post()->get("resource");
         $amt = $this->post()->get("amt");
         $coinId = $this->post()->get("coinId");
         $platform = $this->post()->get("channel");
         $schoolId = $this->post()->get("schoolId");
     } else {
         $ssotoken = $this->get("ssotoken");
         $payType = $this->get("payType");
         $resource = $this->get("resource");
         $amt = $this->get("amt");
         $coinId = $this->get("coinId");
         $platform = $this->get("channel");
         $schoolId = $this->get("schoolId");
     }
     //此处为测试数据
     //$resource = [];
     $resource = json_decode($resource, true);
     //接收参数判断
     if (!$ssotoken || !$payType || !$amt) {
         $this->displayJsonUdo(Common_Error::ERROR_PARAM, "", "缺少必选参数");
     }
     $courseCount = 0;
     $schoolModel = new SchoolModel();
     //余额支付时,需对resource参数进行判断
     if ($payType == Common_Config::UDO_PAYTYPE_COIN || $payType == Common_Config::UDO_PAYTYPE_CREDIT) {
         if (!is_array($resource) || !$schoolId) {
             $this->displayJsonUdo(Common_Error::ERROR_PARAM, "", "缺少resource或schoolId参数");
         }
         //从客户端传过来的resource是所有选中的资源
         //此处根据resource生成交易的信息
         //$resourceType = 0;
         $resourceInfo = "";
         $courseName = "";
         foreach ($resource as $k => $value) {
             if ($value['resourceType'] == Common_Config::UDO_RESOURCE_COURSE) {
                 $courseCount++;
                 //$resourceType = Common_Config::UDO_RESOURCE_SCHOOL;
                 $resourceInfo = "频道";
                 $courseName = $courseName ? $courseName : $schoolModel->getSingleCourse($value['resourceId']);
             }
         }
         $resourceInfo = $courseName['name'] . "'等" . $courseCount . "个课程";
     } elseif ($payType == Common_Config::UDO_PAYTYPE_RECHARGE) {
         if (!$coinId || !$platform) {
             $this->displayJsonUdo(Common_Error::ERROR_PARAM, "", "缺少channel或coinId参数");
         }
     }
     $accountModel = new AccountModel();
     $tradeModel = new TradeModel();
     $userModel = new UserModel();
     //在校验过参数完整性后,生成订单
     $uid = $userModel->getUserId($ssotoken);
     if (is_array($uid)) {
         $this->displayJsonUdo(Common_Error::INVALID_TOKEN, "", $uid['msg']);
     }
     $order = $accountModel->newOrder($ssotoken, $uid, $schoolId, $courseCount, $payType, $resource, $coinId, $amt, $platform);
     //对生成订单的结果进行判断
     if ($order < 0) {
         $this->displayJsonUdo(Common_Error::ERROR_SUCCESS, array("result" => $order));
     }
     //生成订单后,将支付参数传向公共云
     //根据支付类型不同,在调用公共云的支付服务时,传相应的值处理
     /*
      * @param payType
      * 1:U币支付
      * 2:U币充值
      * 3.学分支付
      */
     switch ($payType) {
         case Common_Config::UDO_PAYTYPE_COIN:
             $type = Common_Config::PUBLIC_PAYTYPE_AMOUNT;
             $subject = "U币购买'" . $resourceInfo;
             $score = 0;
             $balanceAmt = $amt;
             $channel = 0;
             $chargeAmt = 0;
             break;
         case Common_Config::UDO_PAYTYPE_RECHARGE:
             //客户端提交的amt均为
             $type = $type = Common_Config::PUBLIC_PAYTYPE_CHANNEL;
             $coinMoney = $tradeModel->getCoinMoney($coinId);
             //实际
             $chargeAmt = $amt;
             $amt = $coinMoney['price'];
             $subject = "U币充值";
             $score = 0;
             $balanceAmt = 0;
             $channel = $platform;
             break;
         case Common_Config::UDO_PAYTYPE_CREDIT:
             $type = Common_Config::PUBLIC_PAYTYPE_AMOUNT;
             $subject = "学分购买'" . $resourceInfo;
             $score = $amt;
             $amt = 0;
             $balanceAmt = 0;
             $channel = 0;
             $chargeAmt = 0;
             break;
     }
     //notifyUrl:需腰写在配置文件里
     $notifyUrl = Common_Config::PAY_NOTIFY_URL;
     $remark = "支付";
     //公共云传回预下单信息
     $result = $accountModel->pay($ssotoken, $type, $subject, $amt, $chargeAmt, $score, $balanceAmt, $channel, $notifyUrl, $remark);
     switch ($payType) {
         case Common_Config::UDO_PAYTYPE_COIN:
         case Common_Config::UDO_PAYTYPE_CREDIT:
             if ($result) {
                 //支付成功,写入购买关系表,U币和学分变动表
                 $insertBought = $accountModel->insertBought($uid, $schoolId, $resource, $order, $result['transNo']);
                 $accountModel->insertTransLog($uid, 0 - ($payType == Common_Config::UDO_PAYTYPE_CREDIT ? $score : $amt), $subject, $payType == Common_Config::UDO_PAYTYPE_CREDIT ? Common_Config::CREDIT_LOG : Common_Config::COIN_LOG, $ssotoken, $schoolId);
                 if ($insertBought < 0) {
                     $this->displayJsonUdo(Common_Error::ERROR_SUCCESS, array("result" => $insertBought));
                 }
             }
             //print_r($result);
             $this->displayJsonUdo(Common_Error::ERROR_SUCCESS, array_merge(array("result" => Common_Error::ERROR_ORDER_SUCCESS), $result));
             break;
         case 2:
             if (array_key_exists("invoke", $result) && array_key_exists("transNo", $result) && $result['transNo'] && $result['invoke']) {
                 $accountModel->updateOrder($order, $result['transNo'], Common_Config::ORDER_NOT_PAY);
                 $this->displayJsonUdo(Common_Error::ERROR_SUCCESS, array_merge(array("result" => Common_Error::ERROR_ORDER_SUCCESS), array("transNo" => $result['transNo'], "invoke" => $result['invoke'])));
             }
             break;
     }
     $this->displayJsonUdo(Common_Error::ERROR_FAIL);
 }