42d85a959b210db9c6ba7c78bb82e1327e9464a2.svn-base 22 KB

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