/** * * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填 * appid、mchid、spbill_create_ip、nonce_str不需要填入 * @param base\WxPayUnifiedOrder $inputObj * @param int $timeOut * @throws WxPayException * @return 成功时返回,其他抛异常 */ public static 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(WxPayConfig::NOTIFY_URL); //异步通知url } $inputObj->SetAppid(WxPayConfig::APPID); //公众账号ID $inputObj->SetMch_id(WxPayConfig::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 = WxPayResults::Init($response); self::reportCostTime($url, $startTimeStamp, $result); //上报请求花费时间 return $result; }
public function unifiedorder($openId, $product_id) { //统一下单 $input = new WxPayUnifiedOrder(); $input->SetBody("test"); $input->SetAttach("test"); $input->SetOut_trade_no(WxPayConfig::MCHID . date("YmdHis")); $input->SetTotal_fee("1"); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetGoods_tag("test"); $input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php"); $input->SetTrade_type("NATIVE"); $input->SetOpenid($openId); $input->SetProduct_id($product_id); $result = WxPayApi::unifiedOrder($input); Log::DEBUG("unifiedorder:" . json_encode($result)); return $result; }
} } //①、获取用户openid $tools = new JsApiPay(); $openId = $tools->GetOpenid(); //②、统一下单 $input = new WxPayUnifiedOrder(); $input->SetBody("test"); $input->SetAttach("test"); $input->SetOut_trade_no(WxPayConfig::MCHID . date("YmdHis")); $input->SetTotal_fee("1"); //金额 $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetGoods_tag("test"); $input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php"); $input->SetTrade_type("JSAPI"); $input->SetOpenid($openId); $order = WxPayApi::unifiedOrder($input); echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>'; printf_info($order); //$order为数组 $jsApiParameters = $tools->GetJsApiParameters($order); //$jsApiParameters为json格式 //获取共享收货地址js函数参数 $editAddress = $tools->GetEditAddressParameters(); //③、在支持成功回调通知中处理成功之后的事宜,见 notify.php /** * 注意: * 1、当你的回调地址不可访问的时候,回调通知会失败,可以通过查询订单来确认支付是否成功 * 2、jsapi支付时需要填入用户openid,WxPay.JsApiPay.php中有获取openid流程 (文档可以参考微信公众平台“网页授权接口”,