WeixinPay.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\common\library;
  3. /**
  4. * 微信支付类
  5. */
  6. class WeixinPay {
  7. var $url; //统一下单接口链接
  8. var $order_query_url; //查询订单链接
  9. var $curl_timeout; //curl超时时间
  10. public function __construct(){
  11. $this->url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  12. $this->order_query_url = "https://api.mch.weixin.qq.com/pay/orderquery";
  13. }
  14. /**
  15. * 生成签名
  16. * @param $params array 签名用参数
  17. * @param $key string 微信支付的key
  18. *
  19. * @return string 签名
  20. */
  21. public function MakeSign($params,$key)
  22. {
  23. //签名步骤一:按字典序排序数组参数
  24. ksort($params);
  25. $string = $this->ToUrlParams($params);
  26. //签名步骤二:在string后加入KEY
  27. $string = $string . "&key=" . $key;
  28. //签名步骤三:MD5加密
  29. $string = md5($string);
  30. //签名步骤四:所有字符转为大写
  31. $result = strtoupper($string);
  32. return $result;
  33. }
  34. /**
  35. * 将参数拼接为url: key=value&key=value
  36. * @param $params
  37. * @return string
  38. */
  39. public function ToUrlParams($params)
  40. {
  41. $string = '';
  42. if (!empty($params)) {
  43. $array = array();
  44. foreach ($params as $key => $value) {
  45. $array[] = $key . '=' . $value;
  46. }
  47. $string = implode("&", $array);
  48. }
  49. return $string;
  50. }
  51. /**
  52. * 作用:以post方式提交xml到对应的接口url
  53. *
  54. * @param $xml string xml的报文内容
  55. * @param $second int 请求的超时时间
  56. *
  57. */
  58. public function postXmlCurl($xml,$url='',$second=30){
  59. if($url==''){
  60. $url = $this->url;
  61. }
  62. //初始化curl
  63. $ch = curl_init();
  64. //超时时间
  65. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$second);
  66. curl_setopt($ch,CURLOPT_TIMEOUT,$second);
  67. //这里设置代理,如果有的话
  68. //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
  69. //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
  70. curl_setopt($ch,CURLOPT_URL, $url);
  71. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
  72. //curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,1);
  73. //curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2); //php5.2以上版本
  74. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); //php5.5以上版本
  75. //设置header
  76. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  77. //设定传输xml格式
  78. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: text/xml" ));
  79. //要求结果为字符串且输出到屏幕上
  80. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  81. //post提交方式
  82. curl_setopt($ch, CURLOPT_POST, TRUE);
  83. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  84. //运行curl
  85. $data = curl_exec($ch);
  86. //返回结果
  87. if($data){
  88. curl_close($ch);
  89. return $data;
  90. }else{
  91. $error = curl_errno($ch);
  92. curl_close($ch);
  93. throw new \Exception("curl出错,错误码:$error");
  94. return false;
  95. }
  96. }
  97. /**
  98. * post方式提交xml微信订单查询接口
  99. *
  100. * @param $xml string xml的报文内容
  101. *
  102. */
  103. public function queryPostXmlCurl($xml)
  104. {
  105. return $this->postXmlCurl($xml,$this->order_query_url);
  106. }
  107. /**
  108. * 将xml转为array
  109. * @param string $xml
  110. *
  111. * @return array
  112. */
  113. public function xmlToArray($xml)
  114. {
  115. if (!$xml) {
  116. return false;
  117. }
  118. $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  119. return $data;
  120. }
  121. }