WxPay.Api.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. <?php
  2. require_once "WxPay.Exception.php";
  3. require_once "WxPay.Data.php";
  4. /**
  5. *
  6. * 接口访问类,包含所有微信支付API列表的封装,类中方法为static方法,
  7. * 每个接口有默认超时时间(除提交被扫支付为10s,上报超时时间为1s外,其他均为6s)
  8. * @author widyhu
  9. *
  10. */
  11. class WxPayApi
  12. {
  13. /**
  14. *
  15. * 统一下单,WxPayUnifiedOrder中out_trade_no、body、total_fee、trade_type必填
  16. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  17. * @param WxPayConfig $config 配置对象
  18. * @param WxPayUnifiedOrder $inputObj
  19. * @param int $timeOut
  20. * @throws WxPayException
  21. * @return 成功时返回,其他抛异常
  22. */
  23. public static function unifiedOrder($config, $inputObj, $timeOut = 6)
  24. {
  25. $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  26. //检测必填参数
  27. if(!$inputObj->IsOut_trade_noSet()) {
  28. throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
  29. }else if(!$inputObj->IsBodySet()){
  30. throw new WxPayException("缺少统一支付接口必填参数body!");
  31. }else if(!$inputObj->IsTotal_feeSet()) {
  32. throw new WxPayException("缺少统一支付接口必填参数total_fee!");
  33. }else if(!$inputObj->IsTrade_typeSet()) {
  34. throw new WxPayException("缺少统一支付接口必填参数trade_type!");
  35. }
  36. //关联参数
  37. if($inputObj->GetTrade_type() == "JSAPI" && !$inputObj->IsOpenidSet()){
  38. throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
  39. }
  40. if($inputObj->GetTrade_type() == "NATIVE" && !$inputObj->IsProduct_idSet()){
  41. throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
  42. }
  43. //异步通知url未设置,则使用配置文件中的url
  44. if(!$inputObj->IsNotify_urlSet() && $config->GetNotifyUrl() != ""){
  45. $inputObj->SetNotify_url($config->GetNotifyUrl());//异步通知url
  46. }
  47. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  48. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  49. $inputObj->SetSpbill_create_ip($config->GetIp());//终端ip
  50. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  51. //签名
  52. $inputObj->SetSign($config);
  53. $xml = $inputObj->ToXml();
  54. $startTimeStamp = self::getMillisecond();//请求开始时间
  55. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  56. $result = WxPayResults::Init($config, $response);
  57. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  58. return $result;
  59. }
  60. /**
  61. *
  62. * 查询订单,WxPayOrderQuery中out_trade_no、transaction_id至少填一个
  63. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  64. * @param WxPayConfig $config 配置对象
  65. * @param WxPayOrderQuery $inputObj
  66. * @param int $timeOut
  67. * @throws WxPayException
  68. * @return 成功时返回,其他抛异常
  69. */
  70. public static function orderQuery($config, $inputObj, $timeOut = 6)
  71. {
  72. $url = "https://api.mch.weixin.qq.com/pay/orderquery";
  73. //检测必填参数
  74. if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
  75. throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!");
  76. }
  77. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  78. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  79. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  80. $inputObj->SetSign($config);//签名
  81. $xml = $inputObj->ToXml();
  82. $startTimeStamp = self::getMillisecond();//请求开始时间
  83. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  84. $result = WxPayResults::Init($config, $response);
  85. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  86. return $result;
  87. }
  88. /**
  89. *
  90. * 关闭订单,WxPayCloseOrder中out_trade_no必填
  91. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  92. * @param WxPayConfig $config 配置对象
  93. * @param WxPayCloseOrder $inputObj
  94. * @param int $timeOut
  95. * @throws WxPayException
  96. * @return 成功时返回,其他抛异常
  97. */
  98. public static function closeOrder($config, $inputObj, $timeOut = 6)
  99. {
  100. $url = "https://api.mch.weixin.qq.com/pay/closeorder";
  101. //检测必填参数
  102. if(!$inputObj->IsOut_trade_noSet()) {
  103. throw new WxPayException("订单查询接口中,out_trade_no必填!");
  104. }
  105. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  106. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  107. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  108. $inputObj->SetSign($config);//签名
  109. $xml = $inputObj->ToXml();
  110. $startTimeStamp = self::getMillisecond();//请求开始时间
  111. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  112. $result = WxPayResults::Init($config, $response);
  113. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  114. return $result;
  115. }
  116. /**
  117. *
  118. * 申请退款,WxPayRefund中out_trade_no、transaction_id至少填一个且
  119. * out_refund_no、total_fee、refund_fee、op_user_id为必填参数
  120. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  121. * @param WxPayConfig $config 配置对象
  122. * @param WxPayRefund $inputObj
  123. * @param int $timeOut
  124. * @throws WxPayException
  125. * @return 成功时返回,其他抛异常
  126. */
  127. public static function refund($config, $inputObj, $timeOut = 6)
  128. {
  129. $url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
  130. //检测必填参数
  131. if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
  132. throw new WxPayException("退款申请接口中,out_trade_no、transaction_id至少填一个!");
  133. }else if(!$inputObj->IsOut_refund_noSet()){
  134. throw new WxPayException("退款申请接口中,缺少必填参数out_refund_no!");
  135. }else if(!$inputObj->IsTotal_feeSet()){
  136. throw new WxPayException("退款申请接口中,缺少必填参数total_fee!");
  137. }else if(!$inputObj->IsRefund_feeSet()){
  138. throw new WxPayException("退款申请接口中,缺少必填参数refund_fee!");
  139. }else if(!$inputObj->IsOp_user_idSet()){
  140. throw new WxPayException("退款申请接口中,缺少必填参数op_user_id!");
  141. }
  142. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  143. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  144. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  145. $inputObj->SetSign($config);//签名
  146. $xml = $inputObj->ToXml();
  147. $startTimeStamp = self::getMillisecond();//请求开始时间
  148. $response = self::postXmlCurl($config, $xml, $url, true, $timeOut);
  149. $result = WxPayResults::Init($config, $response);
  150. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  151. return $result;
  152. }
  153. /**
  154. *
  155. * 查询退款
  156. * 提交退款申请后,通过调用该接口查询退款状态。退款有一定延时,
  157. * 用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。
  158. * WxPayRefundQuery中out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个
  159. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  160. * @param WxPayConfig $config 配置对象
  161. * @param WxPayRefundQuery $inputObj
  162. * @param int $timeOut
  163. * @throws WxPayException
  164. * @return 成功时返回,其他抛异常
  165. */
  166. public static function refundQuery($config, $inputObj, $timeOut = 6)
  167. {
  168. $url = "https://api.mch.weixin.qq.com/pay/refundquery";
  169. //检测必填参数
  170. if(!$inputObj->IsOut_refund_noSet() &&
  171. !$inputObj->IsOut_trade_noSet() &&
  172. !$inputObj->IsTransaction_idSet() &&
  173. !$inputObj->IsRefund_idSet()) {
  174. throw new WxPayException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!");
  175. }
  176. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  177. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  178. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  179. $inputObj->SetSign($config);//签名
  180. $xml = $inputObj->ToXml();
  181. $startTimeStamp = self::getMillisecond();//请求开始时间
  182. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  183. $result = WxPayResults::Init($config, $response);
  184. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  185. return $result;
  186. }
  187. /**
  188. * 下载对账单,WxPayDownloadBill中bill_date为必填参数
  189. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  190. * @param WxPayConfig $config 配置对象
  191. * @param WxPayDownloadBill $inputObj
  192. * @param int $timeOut
  193. * @throws WxPayException
  194. * @return 成功时返回,其他抛异常
  195. */
  196. public static function downloadBill($config, $inputObj, $timeOut = 6)
  197. {
  198. $url = "https://api.mch.weixin.qq.com/pay/downloadbill";
  199. //检测必填参数
  200. if(!$inputObj->IsBill_dateSet()) {
  201. throw new WxPayException("对账单接口中,缺少必填参数bill_date!");
  202. }
  203. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  204. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  205. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  206. $inputObj->SetSign($config);//签名
  207. $xml = $inputObj->ToXml();
  208. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  209. if(substr($response, 0 , 5) == "<xml>"){
  210. return "";
  211. }
  212. return $response;
  213. }
  214. /**
  215. * 提交被扫支付API
  216. * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,
  217. * 由商户收银台或者商户后台调用该接口发起支付。
  218. * WxPayWxPayMicroPay中body、out_trade_no、total_fee、auth_code参数必填
  219. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  220. * @param WxPayConfig $config 配置对象
  221. * @param WxPayWxPayMicroPay $inputObj
  222. * @param int $timeOut
  223. */
  224. public static function micropay($config, $inputObj, $timeOut = 10)
  225. {
  226. $url = "https://api.mch.weixin.qq.com/pay/micropay";
  227. //检测必填参数
  228. if(!$inputObj->IsBodySet()) {
  229. throw new WxPayException("提交被扫支付API接口中,缺少必填参数body!");
  230. } else if(!$inputObj->IsOut_trade_noSet()) {
  231. throw new WxPayException("提交被扫支付API接口中,缺少必填参数out_trade_no!");
  232. } else if(!$inputObj->IsTotal_feeSet()) {
  233. throw new WxPayException("提交被扫支付API接口中,缺少必填参数total_fee!");
  234. } else if(!$inputObj->IsAuth_codeSet()) {
  235. throw new WxPayException("提交被扫支付API接口中,缺少必填参数auth_code!");
  236. }
  237. $inputObj->SetSpbill_create_ip($config->GetIp());//终端ip
  238. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  239. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  240. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  241. $inputObj->SetSign($config);//签名
  242. $xml = $inputObj->ToXml();
  243. $startTimeStamp = self::getMillisecond();//请求开始时间
  244. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  245. $result = WxPayResults::Init($config, $response);
  246. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  247. return $result;
  248. }
  249. /**
  250. *
  251. * 撤销订单API接口,WxPayReverse中参数out_trade_no和transaction_id必须填写一个
  252. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  253. * @param WxPayConfig $config 配置对象
  254. * @param WxPayReverse $inputObj
  255. * @param int $timeOut
  256. * @throws WxPayException
  257. */
  258. public static function reverse($config, $inputObj, $timeOut = 6)
  259. {
  260. $url = "https://api.mch.weixin.qq.com/secapi/pay/reverse";
  261. //检测必填参数
  262. if(!$inputObj->IsOut_trade_noSet() && !$inputObj->IsTransaction_idSet()) {
  263. throw new WxPayException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!");
  264. }
  265. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  266. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  267. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  268. $inputObj->SetSign($config);//签名
  269. $xml = $inputObj->ToXml();
  270. $startTimeStamp = self::getMillisecond();//请求开始时间
  271. $response = self::postXmlCurl($config, $xml, $url, true, $timeOut);
  272. $result = WxPayResults::Init($config, $response);
  273. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  274. return $result;
  275. }
  276. /**
  277. *
  278. * 测速上报,该方法内部封装在report中,使用时请注意异常流程
  279. * WxPayReport中interface_url、return_code、result_code、user_ip、execute_time_必填
  280. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  281. * @param WxPayConfig $config 配置对象
  282. * @param WxPayReport $inputObj
  283. * @param int $timeOut
  284. * @throws WxPayException
  285. * @return 成功时返回,其他抛异常
  286. */
  287. public static function report($config, $inputObj, $timeOut = 1)
  288. {
  289. $url = "https://api.mch.weixin.qq.com/payitil/report";
  290. //检测必填参数
  291. if(!$inputObj->IsInterface_urlSet()) {
  292. throw new WxPayException("接口URL,缺少必填参数interface_url!");
  293. } if(!$inputObj->IsReturn_codeSet()) {
  294. throw new WxPayException("返回状态码,缺少必填参数return_code!");
  295. } if(!$inputObj->IsResult_codeSet()) {
  296. throw new WxPayException("业务结果,缺少必填参数result_code!");
  297. } if(!$inputObj->IsUser_ipSet()) {
  298. throw new WxPayException("访问接口IP,缺少必填参数user_ip!");
  299. } if(!$inputObj->IsExecute_time_Set()) {
  300. throw new WxPayException("接口耗时,缺少必填参数execute_time_!");
  301. }
  302. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  303. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  304. $inputObj->SetUser_ip($_SERVER['REMOTE_ADDR']);//终端ip
  305. $inputObj->SetTime(date("YmdHis"));//商户上报时间
  306. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  307. $inputObj->SetSign($config);//签名
  308. $xml = $inputObj->ToXml();
  309. $startTimeStamp = self::getMillisecond();//请求开始时间
  310. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  311. return $response;
  312. }
  313. /**
  314. *
  315. * 生成二维码规则,模式一生成支付二维码
  316. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  317. * @param WxPayConfig $config 配置对象
  318. * @param WxPayBizPayUrl $inputObj
  319. * @param int $timeOut
  320. * @throws WxPayException
  321. * @return 成功时返回,其他抛异常
  322. */
  323. public static function bizpayurl($config, $inputObj, $timeOut = 6)
  324. {
  325. if(!$inputObj->IsProduct_idSet()){
  326. throw new WxPayException("生成二维码,缺少必填参数product_id!");
  327. }
  328. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  329. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  330. $inputObj->SetTime_stamp(time());//时间戳
  331. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  332. $inputObj->SetSign($config);//签名
  333. return $inputObj->GetValues();
  334. }
  335. /**
  336. *
  337. * 转换短链接
  338. * 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX),
  339. * 减小二维码数据量,提升扫描速度和精确度。
  340. * appid、mchid、spbill_create_ip、nonce_str不需要填入
  341. * @param WxPayConfig $config 配置对象
  342. * @param WxPayShortUrl $inputObj
  343. * @param int $timeOut
  344. * @throws WxPayException
  345. * @return 成功时返回,其他抛异常
  346. */
  347. public static function shorturl($config, $inputObj, $timeOut = 6)
  348. {
  349. $url = "https://api.mch.weixin.qq.com/tools/shorturl";
  350. //检测必填参数
  351. if(!$inputObj->IsLong_urlSet()) {
  352. throw new WxPayException("需要转换的URL,签名用原串,传输需URL encode!");
  353. }
  354. $inputObj->SetAppid($config->GetAppId());//公众账号ID
  355. $inputObj->SetMch_id($config->GetMerchantId());//商户号
  356. $inputObj->SetNonce_str(self::getNonceStr());//随机字符串
  357. $inputObj->SetSign($config);//签名
  358. $xml = $inputObj->ToXml();
  359. $startTimeStamp = self::getMillisecond();//请求开始时间
  360. $response = self::postXmlCurl($config, $xml, $url, false, $timeOut);
  361. $result = WxPayResults::Init($config, $response);
  362. self::reportCostTime($config, $url, $startTimeStamp, $result);//上报请求花费时间
  363. return $result;
  364. }
  365. /**
  366. *
  367. * 支付结果通用通知
  368. * @param function $callback
  369. * 直接回调函数使用方法: notify(you_function);
  370. * 回调类成员函数方法:notify(array($this, you_function));
  371. * $callback 原型为:function function_name($data){}
  372. */
  373. public static function notify($config, $callback, &$msg)
  374. {
  375. $xml = file_get_contents("php://input");
  376. if (!$xml) {
  377. # 如果没有数据,直接返回失败
  378. return false;
  379. }
  380. //如果返回成功则验证签名
  381. try {
  382. //获取通知的数据
  383. $result = WxPayNotifyResults::Init($config, $xml);
  384. } catch (WxPayException $e){
  385. $msg = $e->errorMessage();
  386. return false;
  387. }
  388. return call_user_func($callback, $result);
  389. }
  390. /**
  391. *
  392. * 产生随机字符串,不长于32位
  393. * @param int $length
  394. * @return 产生的随机字符串
  395. */
  396. public static function getNonceStr($length = 32)
  397. {
  398. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  399. $str ="";
  400. for ( $i = 0; $i < $length; $i++ ) {
  401. $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  402. }
  403. return $str;
  404. }
  405. /**
  406. * 直接输出xml
  407. * @param string $xml
  408. */
  409. public static function replyNotify($xml)
  410. {
  411. echo $xml;
  412. }
  413. /**
  414. *
  415. * 上报数据, 上报的时候将屏蔽所有异常流程
  416. * @param WxPayConfig $config 配置对象
  417. * @param string $usrl
  418. * @param int $startTimeStamp
  419. * @param array $data
  420. */
  421. private static function reportCostTime($config, $url, $startTimeStamp, $data)
  422. {
  423. //如果不需要上报数据
  424. $reportLevenl = $config->GetReportLevenl();
  425. if($reportLevenl == 0){
  426. return;
  427. }
  428. //如果仅失败上报
  429. if($reportLevenl == 1 &&
  430. array_key_exists("return_code", $data) &&
  431. $data["return_code"] == "SUCCESS" &&
  432. array_key_exists("result_code", $data) &&
  433. $data["result_code"] == "SUCCESS")
  434. {
  435. return;
  436. }
  437. //上报逻辑
  438. $endTimeStamp = self::getMillisecond();
  439. $objInput = new WxPayReport();
  440. $objInput->SetInterface_url($url);
  441. $objInput->SetExecute_time_($endTimeStamp - $startTimeStamp);
  442. //返回状态码
  443. if(array_key_exists("return_code", $data)){
  444. $objInput->SetReturn_code($data["return_code"]);
  445. }
  446. //返回信息
  447. if(array_key_exists("return_msg", $data)){
  448. $objInput->SetReturn_msg($data["return_msg"]);
  449. }
  450. //业务结果
  451. if(array_key_exists("result_code", $data)){
  452. $objInput->SetResult_code($data["result_code"]);
  453. }
  454. //错误代码
  455. if(array_key_exists("err_code", $data)){
  456. $objInput->SetErr_code($data["err_code"]);
  457. }
  458. //错误代码描述
  459. if(array_key_exists("err_code_des", $data)){
  460. $objInput->SetErr_code_des($data["err_code_des"]);
  461. }
  462. //商户订单号
  463. if(array_key_exists("out_trade_no", $data)){
  464. $objInput->SetOut_trade_no($data["out_trade_no"]);
  465. }
  466. //设备号
  467. if(array_key_exists("device_info", $data)){
  468. $objInput->SetDevice_info($data["device_info"]);
  469. }
  470. try{
  471. self::report($config, $objInput);
  472. } catch (WxPayException $e){
  473. //不做任何处理
  474. }
  475. }
  476. /**
  477. * 以post方式提交xml到对应的接口url
  478. *
  479. * @param WxPayConfig $config 配置对象
  480. * @param string $xml 需要post的xml数据
  481. * @param string $url url
  482. * @param bool $useCert 是否需要证书,默认不需要
  483. * @param int $second url执行超时时间,默认30s
  484. * @throws WxPayException
  485. */
  486. private static function postXmlCurl($config, $xml, $url, $useCert = false, $second = 30)
  487. {
  488. $ch = curl_init();
  489. $curlVersion = curl_version();
  490. $ua = "WXPaySDK/3.0.9 (".PHP_OS.") PHP/".PHP_VERSION." CURL/".$curlVersion['version']." "
  491. .$config->GetMerchantId();
  492. //设置超时
  493. curl_setopt($ch, CURLOPT_TIMEOUT, $second);
  494. $proxyHost = "0.0.0.0";
  495. $proxyPort = 0;
  496. $config->GetProxy($proxyHost, $proxyPort);
  497. //如果有配置代理这里就设置代理
  498. if($proxyHost != "0.0.0.0" && $proxyPort != 0){
  499. curl_setopt($ch,CURLOPT_PROXY, $proxyHost);
  500. curl_setopt($ch,CURLOPT_PROXYPORT, $proxyPort);
  501. }
  502. curl_setopt($ch,CURLOPT_URL, $url);
  503. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
  504. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验
  505. curl_setopt($ch,CURLOPT_USERAGENT, $ua);
  506. //设置header
  507. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  508. //要求结果为字符串且输出到屏幕上
  509. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  510. if($useCert == true){
  511. //设置证书
  512. //使用证书:cert 与 key 分别属于两个.pem文件
  513. //证书文件请放入服务器的非web目录下
  514. $sslCertPath = "";
  515. $sslKeyPath = "";
  516. $config->GetSSLCertPath($sslCertPath, $sslKeyPath);
  517. curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
  518. curl_setopt($ch,CURLOPT_SSLCERT, $sslCertPath);
  519. curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
  520. curl_setopt($ch,CURLOPT_SSLKEY, $sslKeyPath);
  521. }
  522. //post提交方式
  523. curl_setopt($ch, CURLOPT_POST, TRUE);
  524. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  525. //运行curl
  526. $data = curl_exec($ch);
  527. //返回结果
  528. if($data){
  529. curl_close($ch);
  530. return $data;
  531. } else {
  532. $error = curl_errno($ch);
  533. curl_close($ch);
  534. throw new WxPayException("curl出错,错误码:$error");
  535. }
  536. }
  537. /**
  538. * 获取毫秒级别的时间戳
  539. */
  540. private static function getMillisecond()
  541. {
  542. //获取毫秒的时间戳
  543. $time = explode ( " ", microtime () );
  544. $time = $time[1] . ($time[0] * 1000);
  545. $time2 = explode( ".", $time );
  546. $time = $time2[0];
  547. return $time;
  548. }
  549. }