Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

WxPayApi.php 22KB

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