WechatTransfers.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * 微信付款到零钱
  4. */
  5. namespace payment\wechat;
  6. class WechatTransfers
  7. {
  8. const TRANSFERS_URL = '/mmpaymkttransfers/promotion/transfers';
  9. private $app_id = '';
  10. private $mchid = '';
  11. private $secret_key = '';
  12. private $ip = '';
  13. public function __construct($app_id, $mchid, $secret_key, $ip)
  14. {
  15. $this->app_id = $app_id;
  16. $this->mchid = $mchid;
  17. $this->secret_key = $secret_key;
  18. $this->ip = $ip;
  19. }
  20. public function xmltoarray($xml)
  21. {
  22. //禁止引用外部xml实体
  23. libxml_disable_entity_loader(true);
  24. $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  25. $val = json_decode(json_encode($xmlstring), true);
  26. return $val;
  27. }
  28. public function arraytoxml($data)
  29. {
  30. $str = '<xml>';
  31. foreach ($data as $k => $v) {
  32. $str .= '<' . $k . '>' . $v . '</' . $k . '>';
  33. }
  34. $str .= '</xml>';
  35. return $str;
  36. }
  37. public function createNoncestr($length = 32)
  38. {
  39. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz0123456789";
  40. $str = "";
  41. for ($i = 0; $i < $length; $i++) {
  42. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  43. }
  44. return $str;
  45. }
  46. public function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = [])
  47. {
  48. $isdir = $_SERVER['DOCUMENT_ROOT'] . "/cert/";//证书位置;绝对路径
  49. $ch = curl_init();//初始化curl
  50. curl_setopt($ch, CURLOPT_TIMEOUT, $second);//设置执行最长秒数
  51. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  52. curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
  53. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 终止从服务端进行验证
  54. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
  55. curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//证书类型
  56. curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//证书位置
  57. curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中规定的私钥的加密类型
  58. curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//证书位置
  59. curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
  60. curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');
  61. if (($aHeader) >= 1) {
  62. curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//设置头部
  63. }
  64. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  65. curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);//全部数据使用HTTP协议中的"POST"操作来发送
  66. $data = curl_exec($ch);//执行回话
  67. if ($data) {
  68. curl_close($ch);
  69. return $data;
  70. } else {
  71. $error = curl_errno($ch);
  72. echo "call faild, errorCode:$error";
  73. curl_close($ch);
  74. return false;
  75. }
  76. }
  77. function sendMoney($amount, $re_openid, $desc = '测试', $check_name = '')
  78. {
  79. $total_amount = (100) * $amount;
  80. $data = [
  81. 'mch_appid' => $this->app_id,//商户账号appid
  82. 'mchid' => $this->mchid,//商户号
  83. 'nonce_str' => $this->createNoncestr(),//随机字符串
  84. 'partner_trade_no' => date('YmdHis') . rand(1000, 9999),//商户订单号
  85. 'openid' => $re_openid,//用户openid
  86. 'check_name' => 'NO_CHECK',//校验用户姓名选项,
  87. 're_user_name' => $check_name,//收款用户姓名
  88. 'amount' => $total_amount,//金额
  89. 'desc' => $desc,//企业付款描述信息
  90. 'spbill_create_ip' => $this->ip,//Ip地址
  91. ];
  92. //生成签名算法
  93. $secrect_key = $this->secret_key;///这个就是个API密码。MD5 32位。
  94. $data = array_filter($data);
  95. ksort($data);
  96. $str = '';
  97. foreach ($data as $k => $v) {
  98. $str .= $k . '=' . $v . '&';
  99. }
  100. $str .= 'key=' . $secrect_key;
  101. $data['sign'] = md5($str);
  102. //生成签名算法
  103. $xml = $this->arraytoxml($data);
  104. $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //调用接口
  105. $res = $this->curl_post_ssl($url, $xml);
  106. $return = $this->xmltoarray($res);
  107. print_r($return);
  108. //返回来的结果是xml,最后转换成数组
  109. /*
  110. array(9) {
  111. ["return_code"]=>
  112. string(7) "SUCCESS"
  113. ["return_msg"]=>
  114. array(0) {
  115. }
  116. ["mch_appid"]=>
  117. string(18) "wx57676786465544b2a5"
  118. ["mchid"]=>
  119. string(10) "143345612"
  120. ["nonce_str"]=>
  121. string(32) "iw6TtHdOySMAfS81qcnqXojwUMn8l8mY"
  122. ["result_code"]=>
  123. string(7) "SUCCESS"
  124. ["partner_trade_no"]=>
  125. string(18) "201807011410504098"
  126. ["payment_no"]=>
  127. string(28) "1000018301201807019357038738"
  128. ["payment_time"]=>
  129. string(19) "2018-07-01 14:56:35"
  130. }
  131. */
  132. $responseObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
  133. echo $res = $responseObj->return_code; //SUCCESS 如果返回来SUCCESS,则发生成功,处理自己的逻辑
  134. return $res;
  135. }
  136. }