Ejemplo n.º 1
0
 /**
  *
  * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
  * appid、mchid、spbill_create_ip、nonce_str不需要填入
  * @param WxPayUnifiedOrder $inputObj
  * @param int $timeOut
  * @throws WxPayException
  * @return 成功时返回,其他抛异常
  */
 public function unifiedOrder($inputObj, $timeOut = 6)
 {
     $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
     //检测必填参数
     if (!$inputObj->isOutTradeNoSet()) {
         throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
     } else {
         if (!$inputObj->isBodySet()) {
             throw new WxPayException("缺少统一支付接口必填参数body!");
         } else {
             if (!$inputObj->isTotalFeeSet()) {
                 throw new WxPayException("缺少统一支付接口必填参数total_fee!");
             } else {
                 if (!$inputObj->isTradeTypeSet()) {
                     throw new WxPayException("缺少统一支付接口必填参数trade_type!");
                 }
             }
         }
     }
     //关联参数
     if ($inputObj->getTradeType() == "JSAPI" && !$inputObj->isOpenidSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
     }
     if ($inputObj->getTradeType() == "NATIVE" && !$inputObj->isProductIdSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
     }
     //异步通知url未设置,则使用配置文件中的url
     if (!$inputObj->isNotifyUrlSet()) {
         $inputObj->setNotifyUrl($this->apiConfig->notify_url);
         //异步通知url
     }
     $inputObj->setAppid($this->apiConfig->appid);
     //公众账号ID
     $inputObj->setMchId($this->apiConfig->mchid);
     //商户号
     $inputObj->setMchKey($this->apiConfig->mchkey);
     //商户密钥
     $inputObj->setSpbillCreateIp($_SERVER['REMOTE_ADDR']);
     //终端ip
     //$inputObj->setSpbillCreateIp("1.1.1.1");
     $inputObj->setNonceStr($this->getNonceStr());
     //随机字符串
     //签名
     $inputObj->setSign();
     $xml = $inputObj->toXml();
     $startTimeStamp = $this->getMillisecond();
     //请求开始时间
     $response = $this->postXmlCurl($xml, $url, false, $timeOut);
     $result = WxPayResults::init($response, $this->apiConfig->mchkey);
     $this->reportCostTime($url, $startTimeStamp, $result);
     //上报请求花费时间
     return $result;
 }