/** * * 统一下单,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 WechatPayException("缺少统一支付接口必填参数out_trade_no!"); } else { if (!$inputObj->IsBodySet()) { throw new WechatPayException("缺少统一支付接口必填参数body!"); } else { if (!$inputObj->IsTotal_feeSet()) { throw new WechatPayException("缺少统一支付接口必填参数total_fee!"); } else { if (!$inputObj->IsTrade_typeSet()) { throw new WechatPayException("缺少统一支付接口必填参数trade_type!"); } } } } //关联参数 if ($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()) { throw new WechatPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } if ($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()) { throw new WechatPayException("统一支付接口中,缺少必填参数product_id!trade_type为NATIVE时,product_id为必填参数!"); } //异步通知url未设置,则使用配置文件中的url if (!$inputObj->IsNotify_urlSet()) { $inputObj->SetNotify_url(WxPayConfig::getNotifyUrl()); //异步通知url } $inputObj->SetAppid(WxPayConfig::getAppId()); //公众账号ID if (!$inputObj->IsMch_idSet()) { $inputObj->SetMch_id(WxPayConfig::getMchID()); //商户号 } $inputObj->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']); //终端ip //$inputObj->SetSpbill_create_ip("1.1.1.1"); $inputObj->SetNonce_str(self::getNonceStr()); //随机字符串 //签名 $inputObj->SetSign(); $response = $this->getHttpClient()->executeXmlCurl($inputObj, $url); $startTimeStamp = self::getMillisecond(); //请求开始时间 $result = WxPayResults::Init($response); self::reportCostTime($url, $startTimeStamp, $result); //上报请求花费时间 return $result; }
/** * * 支付结果通用通知 * @param function $callback * 直接回调函数使用方法: notify(you_function); * 回调类成员函数方法:notify(array($this, you_function)); * $callback 原型为:function function_name($data){} */ public function notify($callback, &$msg) { //获取通知的数据 //$GLOBALS 有些配置可能有限制 //$xml = $GLOBALS['HTTP_RAW_POST_DATA']; $xml = file_get_contents("php://input"); //如果返回成功则验证签名 try { $result = WxPayResults::Init($xml); } catch (WechatPayException $e) { $msg = $e->errorMessage(); return false; } return call_user_func($callback, $result); }