WxPayPubHelper.php 30 KB

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