/** * @note 设置调起微信支付所需的参数 * @access public * @author zhangchong <*****@*****.**> * @date 2015/7/8 14:07 * @param $params['body'] 设置主题 * @param $params['attach'] 设置附加信息 * @param $params['out_trade_no'] 设置订单号 * @param $params['total_fee'] 设置总金额 单位:元 * @param $params['time_start'] 设置开始时间 格式:YmdHis 如:20150708141123 * @param $params['time_expire'] 设置失效时间 格式:YmdHis 如:20150708141123 * @param $params['goods_tag'] 设置商品标签(商品名称) * @param $params['notify_url'] 设置微信异步通知地址 * @param $params['trade_type'] 设置微信交易类型 默认:JSAPI(jssdk) 其他支付类型参考微信支付文档 * @param $params['open_id'] 设置Openid * @return json|boolean **/ public function setPayParams($params = array()) { if (empty($params)) { return false; } if (!isset($params['openid']) || $params['total_fee'] < 0) { return false; } $input = new Pay\WxPayUnifiedOrder($this->config); foreach ($params as $k => $v) { if ($v !== '') { $input->setParam($k, $v); } } /*$input->SetBody($params['body']); $input->SetAttach($params['attach']); $input->SetOut_trade_no($params['out_trade_no']); $input->SetTotal_fee($params['total_fee']); $input->SetTime_start($params['time_start']); $input->SetTime_expire($params['time_expire']); $input->SetGoods_tag($params['goods_tag']); $input->SetNotify_url($params['notify_url']); $input->SetTrade_type($params['trade_type']); $input->SetOpenid($params['openid']);*/ $api = new Pay\WxPayApi($this->config); $order = $api->unifiedOrder($input); $jsApiParameters = $this->GetJsApiParameters($order); return $jsApiParameters; }
/** * * 统一下单,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->IsOut_trade_noSet()) { throw new WxPayException("缺少统一支付接口必填参数out_trade_no!"); } else { if (!$inputObj->IsBodySet()) { throw new WxPayException("缺少统一支付接口必填参数body!"); } else { if (!$inputObj->IsTotal_feeSet()) { throw new WxPayException("缺少统一支付接口必填参数total_fee!"); } else { if (!$inputObj->IsTrade_typeSet()) { throw new WxPayException("缺少统一支付接口必填参数trade_type!"); } } } } //关联参数 if ($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()) { 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为必填参数!"); } //异步通知url未设置,则使用配置文件中的url if (!$inputObj->IsNotify_urlSet()) { $inputObj->SetNotify_url($this->wxpaycfg->notify_url); //异步通知url } $inputObj->SetAppid($this->wxpaycfg->appid); //公众账号ID $inputObj->SetMch_id($this->wxpaycfg->mchid); //商户号 $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']); //终端ip //$inputObj->SetSpbill_create_ip("1.1.1.1"); $inputObj->SetNonce_str(self::getNonceStr()); //随机字符串 //签名 $inputObj->SetSign(); $xml = $inputObj->ToXml(); $startTimeStamp = self::getMillisecond(); //请求开始时间 $response = self::postXmlCurl($xml, $url, false, $timeOut); $result = $this->wxpayresults->Init($response); self::reportCostTime($url, $startTimeStamp, $result); //上报请求花费时间 return $result; }