WxPayPubHelper.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. <?php
  2. /**
  3. * 微信支付帮助库
  4. * ====================================================
  5. * 接口分三种类型:
  6. * 【请求型接口】--Wxpay_client_
  7. * 统一支付接口类--UnifiedOrder
  8. * 订单查询接口--OrderQuery
  9. * 退款申请接口--Refund
  10. * 退款查询接口--RefundQuery
  11. * 对账单接口--DownloadBill
  12. * 短链接转换接口--ShortUrl
  13. * 【响应型接口】--Wxpay_server_
  14. * 通用通知接口--Notify
  15. * Native支付——请求商家获取商品信息接口--NativeCall
  16. * 【其他】
  17. * 静态链接二维码--NativeLink
  18. * JSAPI支付--JsApi
  19. * =====================================================
  20. * 【CommonUtil】常用工具:
  21. * trimString(),设置参数时需要用到的字符处理函数
  22. * createNoncestr(),产生随机字符串,不长于32位
  23. * formatBizQueryParaMap(),格式化参数,签名过程需要用到
  24. * getSign(),生成签名
  25. * arrayToXml(),array转xml
  26. * xmlToArray(),xml转 array
  27. * postXmlCurl(),以post方式提交xml到对应的接口url
  28. * postXmlSSLCurl(),使用证书,以post方式提交xml到对应的接口url
  29. */
  30. include_once("SDKRuntimeException.php");
  31. include_once("WxPay.pub.config.php");
  32. include_once("log_.php");
  33. /**
  34. * 所有接口的基类
  35. */
  36. class Common_util_pub
  37. {
  38. function __construct() {
  39. }
  40. function trimString($value)
  41. {
  42. $ret = null;
  43. if (null != $value)
  44. {
  45. $ret = $value;
  46. if (strlen($ret) == 0)
  47. {
  48. $ret = null;
  49. }
  50. }
  51. return $ret;
  52. }
  53. /**
  54. * 作用:产生随机字符串,不长于32位
  55. */
  56. public function createNoncestr( $length = 32 )
  57. {
  58. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  59. $str ="";
  60. for ( $i = 0; $i < $length; $i++ ) {
  61. $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  62. }
  63. return $str;
  64. }
  65. /**
  66. * 作用:格式化参数,签名过程需要使用
  67. */
  68. function formatBizQueryParaMap($paraMap, $urlencode)
  69. {
  70. $buff = "";
  71. ksort($paraMap);
  72. foreach ($paraMap as $k => $v)
  73. {
  74. if($urlencode)
  75. {
  76. $v = urlencode($v);
  77. }
  78. //$buff .= strtolower($k) . "=" . $v . "&";
  79. $buff .= $k . "=" . $v . "&";
  80. }
  81. $reqPar;
  82. if (strlen($buff) > 0)
  83. {
  84. $reqPar = substr($buff, 0, strlen($buff)-1);
  85. }
  86. return $reqPar;
  87. }
  88. /**
  89. * 作用:生成签名
  90. */
  91. public function getSign($Obj)
  92. {
  93. foreach ($Obj as $k => $v)
  94. {
  95. $Parameters[$k] = $v;
  96. }
  97. //签名步骤一:按字典序排序参数
  98. ksort($Parameters);
  99. $String = $this->formatBizQueryParaMap($Parameters, false);
  100. //echo '【string1】'.$String.'</br>';
  101. //签名步骤二:在string后加入KEY
  102. $String = $String."&key=".WxPayConf_pub::KEY;
  103. //echo "【string2】".$String."</br>";
  104. //签名步骤三:MD5加密
  105. $String = md5($String);
  106. //echo "【string3】 ".$String."</br>";
  107. //签名步骤四:所有字符转为大写
  108. $result_ = strtoupper($String);
  109. //echo "【result】 ".$result_."</br>";
  110. return $result_;
  111. }
  112. /**
  113. * 作用:array转xml
  114. */
  115. function arrayToXml($arr)
  116. {
  117. $xml = "<xml>";
  118. foreach ($arr as $key=>$val)
  119. {
  120. if (is_numeric($val))
  121. {
  122. $xml.="<".$key.">".$val."</".$key.">";
  123. }
  124. else
  125. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  126. }
  127. $xml.="</xml>";
  128. return $xml;
  129. }
  130. /**
  131. * 作用:将xml转为array
  132. */
  133. public function xmlToArray($xml)
  134. {
  135. //将XML转为array
  136. $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  137. return $array_data;
  138. }
  139. /**
  140. * 作用:以post方式提交xml到对应的接口url
  141. */
  142. public function postXmlCurl($xml,$url,$second=30)
  143. {
  144. //以log文件形式记录回调信息
  145. $log_ = new Log_();
  146. $log_name="./notify_url.log";//log文件路径
  147. $log_->log_result($log_name,"【postXmlCurl xml】:\n".$xml."\n");
  148. $log_->log_result($log_name,"【postXmlCurl url】:\n".$url."\n");
  149. //初始化curl
  150. $ch = curl_init();
  151. //设置超时
  152. curl_setopt($ch, CURLOPT_TIMEOUT, $second);
  153. //这里设置代理,如果有的话
  154. //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
  155. //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
  156. curl_setopt($ch,CURLOPT_URL, $url);
  157. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  158. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
  159. //设置header
  160. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  161. //要求结果为字符串且输出到屏幕上
  162. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  163. //post提交方式
  164. curl_setopt($ch, CURLOPT_POST, TRUE);
  165. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  166. //运行curl
  167. $data = curl_exec($ch);
  168. curl_close($ch);
  169. //返回结果
  170. if($data)
  171. {
  172. //curl_close($ch);
  173. return $data;
  174. }
  175. else
  176. {
  177. $error = curl_errno($ch);
  178. echo "curl出错,错误码:$error"."<br>";
  179. echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
  180. curl_close($ch);
  181. return false;
  182. }
  183. }
  184. /**
  185. * 作用:使用证书,以post方式提交xml到对应的接口url
  186. */
  187. function postXmlSSLCurl($xml,$url,$second=30)
  188. {
  189. $ch = curl_init();
  190. //超时时间
  191. curl_setopt($ch,CURLOPT_TIMEOUT,$second);
  192. //这里设置代理,如果有的话
  193. //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
  194. //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
  195. curl_setopt($ch,CURLOPT_URL, $url);
  196. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  197. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
  198. //设置header
  199. curl_setopt($ch,CURLOPT_HEADER,FALSE);
  200. //要求结果为字符串且输出到屏幕上
  201. curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
  202. //设置证书
  203. //使用证书:cert 与 key 分别属于两个.pem文件
  204. //默认格式为PEM,可以注释
  205. curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
  206. curl_setopt($ch,CURLOPT_SSLCERT, WxPayConf_pub::SSLCERT_PATH);
  207. //默认格式为PEM,可以注释
  208. curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
  209. curl_setopt($ch,CURLOPT_SSLKEY, WxPayConf_pub::SSLKEY_PATH);
  210. //post提交方式
  211. curl_setopt($ch,CURLOPT_POST, true);
  212. curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
  213. $data = curl_exec($ch);
  214. //返回结果
  215. if($data){
  216. curl_close($ch);
  217. return $data;
  218. }
  219. else {
  220. $error = curl_errno($ch);
  221. echo "curl出错,错误码:$error"."<br>";
  222. echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
  223. curl_close($ch);
  224. return false;
  225. }
  226. }
  227. /**
  228. * 作用:打印数组
  229. */
  230. function printErr($wording='',$err='')
  231. {
  232. print_r('<pre>');
  233. echo $wording."</br>";
  234. var_dump($err);
  235. print_r('</pre>');
  236. }
  237. }
  238. /**
  239. * 请求型接口的基类
  240. */
  241. class Wxpay_client_pub extends Common_util_pub
  242. {
  243. var $parameters;//请求参数,类型为关联数组
  244. public $response;//微信返回的响应
  245. public $result;//返回参数,类型为关联数组
  246. var $url;//接口链接
  247. var $curl_timeout;//curl超时时间
  248. /**
  249. * 作用:设置请求参数
  250. */
  251. function setParameter($parameter, $parameterValue)
  252. {
  253. $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
  254. }
  255. /**
  256. * 作用:设置标配的请求参数,生成签名,生成接口参数xml
  257. */
  258. function createXml()
  259. {
  260. $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  261. $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  262. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  263. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  264. return $this->arrayToXml($this->parameters);
  265. }
  266. /**
  267. * 作用:post请求xml
  268. */
  269. function postXml()
  270. {
  271. //以log文件形式记录回调信息
  272. $log_ = new Log_();
  273. $log_name="./notify_url.log";//log文件路径
  274. $log_->log_result($log_name,"【postXml 】:\n\n");
  275. $xml = $this->createXml();
  276. $log_->log_result($log_name,"【xml 】:\n".$xml."\n");
  277. $log_->log_result($log_name,"【url 】:\n".$this->url."\n");
  278. $this->response = $this->postXmlCurl($xml,$this->url,$this->curl_timeout);
  279. return $this->response;
  280. }
  281. /**
  282. * 作用:使用证书post请求xml
  283. */
  284. function postXmlSSL()
  285. {
  286. $xml = $this->createXml();
  287. $this->response = $this->postXmlSSLCurl($xml,$this->url,$this->curl_timeout);
  288. return $this->response;
  289. }
  290. /**
  291. * 作用:获取结果,默认不使用证书
  292. */
  293. function getResult()
  294. {
  295. $this->postXml();
  296. $this->result = $this->xmlToArray($this->response);
  297. return $this->result;
  298. }
  299. }
  300. /**
  301. * 统一支付接口类
  302. */
  303. class UnifiedOrder_pub extends Wxpay_client_pub
  304. {
  305. function __construct()
  306. {
  307. //设置接口链接
  308. $this->url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  309. //设置curl超时时间
  310. $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  311. }
  312. /**
  313. * 生成接口参数xml
  314. */
  315. function createXml()
  316. {
  317. try
  318. {
  319. //检测必填参数
  320. if($this->parameters["out_trade_no"] == null)
  321. {
  322. throw new SDKRuntimeException("缺少统一支付接口必填参数out_trade_no!"."<br>");
  323. }elseif($this->parameters["body"] == null){
  324. throw new SDKRuntimeException("缺少统一支付接口必填参数body!"."<br>");
  325. }elseif ($this->parameters["total_fee"] == null ) {
  326. throw new SDKRuntimeException("缺少统一支付接口必填参数total_fee!"."<br>");
  327. }elseif ($this->parameters["notify_url"] == null) {
  328. throw new SDKRuntimeException("缺少统一支付接口必填参数notify_url!"."<br>");
  329. }elseif ($this->parameters["trade_type"] == null) {
  330. throw new SDKRuntimeException("缺少统一支付接口必填参数trade_type!"."<br>");
  331. }elseif ($this->parameters["trade_type"] == "JSAPI" &&
  332. $this->parameters["openid"] == NULL){
  333. throw new SDKRuntimeException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"."<br>");
  334. }
  335. $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  336. $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  337. $this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//终端ip
  338. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  339. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  340. return $this->arrayToXml($this->parameters);
  341. }catch (SDKRuntimeException $e)
  342. {
  343. die($e->errorMessage());
  344. }
  345. }
  346. /**
  347. * 获取prepay_id
  348. */
  349. function getPrepayId()
  350. {
  351. //以log文件形式记录回调信息
  352. $log_ = new Log_();
  353. $log_name="./notify_url.log";//log文件路径
  354. $this->postXml();
  355. $this->result = $this->xmlToArray($this->response);
  356. $log_->log_result($log_name,"【接收到的notify通知】:\n".$this->response."\n");
  357. $prepay_id = $this->result["prepay_id"];
  358. $log_->log_result($log_name,"【接收到的notify通知】:\n".$prepay_id."\n");
  359. return $prepay_id;
  360. }
  361. }
  362. /**
  363. * 订单查询接口
  364. */
  365. class OrderQuery_pub extends Wxpay_client_pub
  366. {
  367. function __construct()
  368. {
  369. //设置接口链接
  370. $this->url = "https://api.mch.weixin.qq.com/pay/orderquery";
  371. //设置curl超时时间
  372. $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  373. }
  374. /**
  375. * 生成接口参数xml
  376. */
  377. function createXml()
  378. {
  379. try
  380. {
  381. //检测必填参数
  382. if($this->parameters["out_trade_no"] == null &&
  383. $this->parameters["transaction_id"] == null)
  384. {
  385. throw new SDKRuntimeException("订单查询接口中,out_trade_no、transaction_id至少填一个!"."<br>");
  386. }
  387. $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  388. $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  389. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  390. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  391. return $this->arrayToXml($this->parameters);
  392. }catch (SDKRuntimeException $e)
  393. {
  394. die($e->errorMessage());
  395. }
  396. }
  397. }
  398. /**
  399. * 退款申请接口
  400. */
  401. class Refund_pub extends Wxpay_client_pub
  402. {
  403. function __construct() {
  404. //设置接口链接
  405. $this->url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
  406. //设置curl超时时间
  407. $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  408. }
  409. /**
  410. * 生成接口参数xml
  411. */
  412. function createXml()
  413. {
  414. try
  415. {
  416. //检测必填参数
  417. if($this->parameters["out_trade_no"] == null && $this->parameters["transaction_id"] == null) {
  418. throw new SDKRuntimeException("退款申请接口中,out_trade_no、transaction_id至少填一个!"."<br>");
  419. }elseif($this->parameters["out_refund_no"] == null){
  420. throw new SDKRuntimeException("退款申请接口中,缺少必填参数out_refund_no!"."<br>");
  421. }elseif($this->parameters["total_fee"] == null){
  422. throw new SDKRuntimeException("退款申请接口中,缺少必填参数total_fee!"."<br>");
  423. }elseif($this->parameters["refund_fee"] == null){
  424. throw new SDKRuntimeException("退款申请接口中,缺少必填参数refund_fee!"."<br>");
  425. }elseif($this->parameters["op_user_id"] == null){
  426. throw new SDKRuntimeException("退款申请接口中,缺少必填参数op_user_id!"."<br>");
  427. }
  428. $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  429. $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  430. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  431. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  432. return $this->arrayToXml($this->parameters);
  433. }catch (SDKRuntimeException $e)
  434. {
  435. die($e->errorMessage());
  436. }
  437. }
  438. /**
  439. * 作用:获取结果,使用证书通信
  440. */
  441. function getResult()
  442. {
  443. $this->postXmlSSL();
  444. $this->result = $this->xmlToArray($this->response);
  445. return $this->result;
  446. }
  447. }
  448. /**
  449. * 退款查询接口
  450. */
  451. class RefundQuery_pub extends Wxpay_client_pub
  452. {
  453. function __construct() {
  454. //设置接口链接
  455. $this->url = "https://api.mch.weixin.qq.com/pay/refundquery";
  456. //设置curl超时时间
  457. $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  458. }
  459. /**
  460. * 生成接口参数xml
  461. */
  462. function createXml()
  463. {
  464. try
  465. {
  466. if($this->parameters["out_refund_no"] == null &&
  467. $this->parameters["out_trade_no"] == null &&
  468. $this->parameters["transaction_id"] == null &&
  469. $this->parameters["refund_id "] == null)
  470. {
  471. throw new SDKRuntimeException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!"."<br>");
  472. }
  473. $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  474. $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  475. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  476. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  477. return $this->arrayToXml($this->parameters);
  478. }catch (SDKRuntimeException $e)
  479. {
  480. die($e->errorMessage());
  481. }
  482. }
  483. /**
  484. * 作用:获取结果,使用证书通信
  485. */
  486. function getResult()
  487. {
  488. $this->postXmlSSL();
  489. $this->result = $this->xmlToArray($this->response);
  490. return $this->result;
  491. }
  492. }
  493. /**
  494. * 对账单接口
  495. */
  496. class DownloadBill_pub extends Wxpay_client_pub
  497. {
  498. function __construct()
  499. {
  500. //设置接口链接
  501. $this->url = "https://api.mch.weixin.qq.com/pay/downloadbill";
  502. //设置curl超时时间
  503. $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  504. }
  505. /**
  506. * 生成接口参数xml
  507. */
  508. function createXml()
  509. {
  510. try
  511. {
  512. if($this->parameters["bill_date"] == null )
  513. {
  514. throw new SDKRuntimeException("对账单接口中,缺少必填参数bill_date!"."<br>");
  515. }
  516. $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  517. $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  518. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  519. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  520. return $this->arrayToXml($this->parameters);
  521. }catch (SDKRuntimeException $e)
  522. {
  523. die($e->errorMessage());
  524. }
  525. }
  526. /**
  527. * 作用:获取结果,默认不使用证书
  528. */
  529. function getResult()
  530. {
  531. $this->postXml();
  532. $this->result = $this->xmlToArray($this->result_xml);
  533. return $this->result;
  534. }
  535. }
  536. /**
  537. * 短链接转换接口
  538. */
  539. class ShortUrl_pub extends Wxpay_client_pub
  540. {
  541. function __construct()
  542. {
  543. //设置接口链接
  544. $this->url = "https://api.mch.weixin.qq.com/tools/shorturl";
  545. //设置curl超时时间
  546. $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  547. }
  548. /**
  549. * 生成接口参数xml
  550. */
  551. function createXml()
  552. {
  553. try
  554. {
  555. if($this->parameters["long_url"] == null )
  556. {
  557. throw new SDKRuntimeException("短链接转换接口中,缺少必填参数long_url!"."<br>");
  558. }
  559. $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  560. $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  561. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  562. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  563. return $this->arrayToXml($this->parameters);
  564. }catch (SDKRuntimeException $e)
  565. {
  566. die($e->errorMessage());
  567. }
  568. }
  569. /**
  570. * 获取prepay_id
  571. */
  572. function getShortUrl()
  573. {
  574. $this->postXml();
  575. $prepay_id = $this->result["short_url"];
  576. return $prepay_id;
  577. }
  578. }
  579. /**
  580. * 响应型接口基类
  581. */
  582. class Wxpay_server_pub extends Common_util_pub
  583. {
  584. public $data;//接收到的数据,类型为关联数组
  585. var $returnParameters;//返回参数,类型为关联数组
  586. /**
  587. * 将微信的请求xml转换成关联数组,以方便数据处理
  588. */
  589. function saveData($xml)
  590. {
  591. $this->data = $this->xmlToArray($xml);
  592. }
  593. function checkSign()
  594. {
  595. $tmpData = $this->data;
  596. unset($tmpData['sign']);
  597. $sign = $this->getSign($tmpData);//本地签名
  598. if ($this->data['sign'] == $sign) {
  599. return TRUE;
  600. }
  601. return FALSE;
  602. }
  603. /**
  604. * 获取微信的请求数据
  605. */
  606. function getData()
  607. {
  608. return $this->data;
  609. }
  610. /**
  611. * 设置返回微信的xml数据
  612. */
  613. function setReturnParameter($parameter, $parameterValue)
  614. {
  615. $this->returnParameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
  616. }
  617. /**
  618. * 生成接口参数xml
  619. */
  620. function createXml()
  621. {
  622. return $this->arrayToXml($this->returnParameters);
  623. }
  624. /**
  625. * 将xml数据返回微信
  626. */
  627. function returnXml()
  628. {
  629. $returnXml = $this->createXml();
  630. return $returnXml;
  631. }
  632. }
  633. /**
  634. * 通用通知接口
  635. */
  636. class Notify_pub extends Wxpay_server_pub
  637. {
  638. }
  639. /**
  640. * 请求商家获取商品信息接口
  641. */
  642. class NativeCall_pub extends Wxpay_server_pub
  643. {
  644. /**
  645. * 生成接口参数xml
  646. */
  647. function createXml()
  648. {
  649. if($this->returnParameters["return_code"] == "SUCCESS"){
  650. $this->returnParameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  651. $this->returnParameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  652. $this->returnParameters["nonce_str"] = $this->createNoncestr();//随机字符串
  653. $this->returnParameters["sign"] = $this->getSign($this->returnParameters);//签名
  654. }
  655. return $this->arrayToXml($this->returnParameters);
  656. }
  657. /**
  658. * 获取product_id
  659. */
  660. function getProductId()
  661. {
  662. $product_id = $this->data["product_id"];
  663. return $product_id;
  664. }
  665. }
  666. /**
  667. * 静态链接二维码
  668. */
  669. class NativeLink_pub extends Common_util_pub
  670. {
  671. var $parameters;//静态链接参数
  672. var $url;//静态链接
  673. function __construct()
  674. {
  675. }
  676. /**
  677. * 设置参数
  678. */
  679. function setParameter($parameter, $parameterValue)
  680. {
  681. $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
  682. }
  683. /**
  684. * 生成Native支付链接二维码
  685. */
  686. function createLink()
  687. {
  688. try
  689. {
  690. if($this->parameters["product_id"] == null)
  691. {
  692. throw new SDKRuntimeException("缺少Native支付二维码链接必填参数product_id!"."<br>");
  693. }
  694. $this->parameters["appid"] = WxPayConf_pub::APPID;//公众账号ID
  695. $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商户号
  696. $time_stamp = time();
  697. $this->parameters["time_stamp"] = "$time_stamp";//时间戳
  698. $this->parameters["nonce_str"] = $this->createNoncestr();//随机字符串
  699. $this->parameters["sign"] = $this->getSign($this->parameters);//签名
  700. $bizString = $this->formatBizQueryParaMap($this->parameters, false);
  701. $this->url = "weixin://wxpay/bizpayurl?".$bizString;
  702. }catch (SDKRuntimeException $e)
  703. {
  704. die($e->errorMessage());
  705. }
  706. }
  707. /**
  708. * 返回链接
  709. */
  710. function getUrl()
  711. {
  712. $this->createLink();
  713. return $this->url;
  714. }
  715. }
  716. /**
  717. * JSAPI支付——H5网页端调起支付接口
  718. */
  719. class JsApi_pub extends Common_util_pub
  720. {
  721. var $code;//code码,用以获取openid
  722. var $openid;//用户的openid
  723. var $parameters;//jsapi参数,格式为json
  724. var $prepay_id;//使用统一支付接口得到的预支付id
  725. var $curl_timeout;//curl超时时间
  726. function __construct()
  727. {
  728. //设置curl超时时间
  729. $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
  730. }
  731. /**
  732. * 作用:生成可以获得code的url
  733. */
  734. function createOauthUrlForCode($redirectUrl)
  735. {
  736. $urlObj["appid"] = WxPayConf_pub::APPID;
  737. $urlObj["redirect_uri"] = "$redirectUrl";
  738. $urlObj["response_type"] = "code";
  739. $urlObj["scope"] = "snsapi_base";
  740. $urlObj["state"] = "STATE"."#wechat_redirect";
  741. $bizString = $this->formatBizQueryParaMap($urlObj, false);
  742. return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
  743. }
  744. /**
  745. * 作用:生成可以获得openid的url
  746. */
  747. function createOauthUrlForOpenid()
  748. {
  749. $urlObj["appid"] = WxPayConf_pub::APPID;
  750. $urlObj["secret"] = WxPayConf_pub::APPSECRET;
  751. $urlObj["code"] = $this->code;
  752. $urlObj["grant_type"] = "authorization_code";
  753. $bizString = $this->formatBizQueryParaMap($urlObj, false);
  754. return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
  755. }
  756. /**
  757. * 作用:通过curl向微信提交code,以获取openid
  758. */
  759. function getOpenid()
  760. {
  761. $url = $this->createOauthUrlForOpenid();
  762. //初始化curl
  763. $ch = curl_init();
  764. //设置超时
  765. curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
  766. curl_setopt($ch, CURLOPT_URL, $url);
  767. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  768. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
  769. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  770. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  771. //运行curl,结果以jason形式返回
  772. $res = curl_exec($ch);
  773. curl_close($ch);
  774. //取出openid
  775. $data = json_decode($res,true);
  776. $this->openid = $data['openid'];
  777. return $this->openid;
  778. }
  779. /**
  780. * 作用:设置prepay_id
  781. */
  782. function setPrepayId($prepayId)
  783. {
  784. $this->prepay_id = $prepayId;
  785. }
  786. /**
  787. * 作用:设置code
  788. */
  789. function setCode($code_)
  790. {
  791. $this->code = $code_;
  792. }
  793. /**
  794. * 作用:设置jsapi的参数
  795. */
  796. public function getParameters()
  797. {
  798. $jsApiObj["appId"] = WxPayConf_pub::APPID;
  799. $timeStamp = time();
  800. $jsApiObj["timeStamp"] = "$timeStamp";
  801. $jsApiObj["nonceStr"] = $this->createNoncestr();
  802. $jsApiObj["package"] = "prepay_id=$this->prepay_id";
  803. $jsApiObj["signType"] = "MD5";
  804. $jsApiObj["paySign"] = $this->getSign($jsApiObj);
  805. $this->parameters = json_encode($jsApiObj);
  806. return $this->parameters;
  807. }
  808. }
  809. ?>