コード例 #1
0
ファイル: WxPay.Api.php プロジェクト: beauty-x/wx-pay-sdk
 /**
  *
  * 统一下单,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 static function unifiedOrder($inputObj, $timeOut = 30)
 {
     $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
     // 检测必填参数
     if (!$inputObj->IsAppidSet()) {
         throw new WxPayException('缺少公众号APPID!');
     } elseif (!$inputObj->IsMch_idSet()) {
         throw new WxPayException('缺少商户号!');
     } elseif (!$inputObj->IsOut_trade_noSet()) {
         throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
     } elseif (!$inputObj->IsBodySet()) {
         throw new WxPayException("缺少统一支付接口必填参数body!");
     } elseif (!$inputObj->IsTotal_feeSet()) {
         throw new WxPayException("缺少统一支付接口必填参数total_fee!");
     } elseif (!$inputObj->IsTrade_typeSet()) {
         throw new WxPayException("缺少统一支付接口必填参数trade_type!");
     }
     // 关联参数
     if ($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet() && !$inputObj->IsSub_openidSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
     }
     if ($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()) {
         throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
     }
     // 签名
     $inputObj->SetSign();
     $xml = $inputObj->ToXml();
     $startTimeStamp = self::getMillisecond();
     // 请求开始时间
     $response = self::postXmlCurl($xml, $url, false, $timeOut);
     $result = WxPayResults::Init($response, $inputObj->wx_config);
     self::reportCostTime($url, $startTimeStamp, $result);
     // 上报请求花费时间
     return $result;
 }