经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
php实现微信公众号企业转账功能
来源:jb51  时间:2018/10/8 8:45:32  对本文有异议

企业付款提供由商户直接付钱至用户微信零钱的能力,支持平台操作及接口调用两种方式,资金到账速度快,使用及查询方便。主要用来解决合理的商户对用户付款需求,比如:保险理赔、彩票兑换等等。

特点

  • 发起方式灵活,可通过页面或接口发起
  • 微信消息触达,用户及时获知入账详情
  • 支持实名校验,判断收款人真实身份
  • 通过openid即可实现付款,无需用户敏感隐私信息
  • 到账速度快,在发起后,用户可在几分钟内收到付款

企业转账需要到微信商户平台=》产品中心=》企业付款到零钱,开启此功能

下面是程序截图:

第一步:设置配置参数

  1. $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
  2. $pars = array();
  3. $pars['mch_appid'] =$this->module['config']['appid'];
  4. $pars['mchid']=$this->module['config']['mchid'];
  5. $pars['nonce_str'] =random(32);
  6. $pars['partner_trade_no'] =time().random(3,1);
  7. $pars['openid'] =$openid;
  8. $pars['check_name'] ='NO_CHECK' ;
  9. //$pars['re_user_name'] ='' ;
  10. $monet_finall = $price * 100;
  11. $pars['amount'] =$monet_finall; //这里是折算成1%的所以要*100
  12. $pars['desc'] ='您已成功提现 '.$price.' 现金';
  13. $pars['spbill_create_ip'] =$this->module['config']['ip'];
  14.  
  15. ksort($pars, SORT_STRING);
  16. $string1 = '';
  17. foreach ($pars as $k => $v) {
  18. $string1 .= "{$k}={$v}&";
  19. }
  20.  
  21. $string1 .= "key=".$this->module['config']['password'];
  22. $pars['sign'] = strtoupper(md5($string1));
  23. $xml = array2xml($pars);
  24. $extras = array();
  25. $extras['CURLOPT_CAINFO'] = ATTACHMENT_ROOT . '/withdraw/cert/rootca.pem.' . $_W['uniacid'];
  26. $extras['CURLOPT_SSLCERT'] = ATTACHMENT_ROOT . '/withdraw/cert/apiclient_cert.pem.' . $_W['uniacid'];
  27. $extras['CURLOPT_SSLKEY'] = ATTACHMENT_ROOT . '/withdraw/cert/apiclient_key.pem.' . $_W['uniacid'];
  28. $procResult = null;

第二步:CURL请求微信服务器

  1. load()->func('communication');
  2. $resp = ihttp_request($url, $xml, $extras);

其中ihttp_request函数内容是:

  1. function ihttp_request($url, $post = '', $extra = array(), $timeout = 60) {
  2. $urlset = parse_url($url);
  3. if (empty($urlset['path'])) {
  4. $urlset['path'] = '/';
  5. }
  6. if (!empty($urlset['query'])) {
  7. $urlset['query'] = "?{$urlset['query']}";
  8. }
  9. if (empty($urlset['port'])) {
  10. }
  11. if (strexists($url, 'https://') && !extension_loaded('openssl')) {
  12. if (!extension_loaded("openssl")) {
  13. message('请开启您PHP环境的openssl');
  14. }
  15. }
  16. if (function_exists('curl_init') && function_exists('curl_exec')) {
  17. $ch = curl_init();
  18. if (!empty($extra['ip'])) {
  19. $extra['Host'] = $urlset['host'];
  20. $urlset['host'] = $extra['ip'];
  21. unset($extra['ip']);
  22. }
  23. curl_setopt($ch, CURLOPT_URL, $urlset['scheme'] . '://' . $urlset['host'] . ($urlset['port'] == '80' || empty($urlset['port']) ? '' : ':' . $urlset['port']) . $urlset['path'] . $urlset['query']);
  24. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  25. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  26. curl_setopt($ch, CURLOPT_HEADER, 1);
  27. @curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  28. if ($post) {
  29. if (is_array($post)) {
  30. $filepost = false;
  31. foreach ($post as $name => &$value) {
  32. if (version_compare(phpversion(), '5.6') >= 0 && substr($value, 0, 1) == '@') {
  33. $value = new CURLFile(ltrim($value, '@'));
  34. }
  35. if ((is_string($value) && substr($value, 0, 1) == '@') || (class_exists('CURLFile') && $value instanceof CURLFile)) {
  36. $filepost = true;
  37. }
  38. }
  39. if (!$filepost) {
  40. $post = http_build_query($post);
  41. }
  42. }
  43. curl_setopt($ch, CURLOPT_POST, 1);
  44. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  45. }
  46. if (!empty($GLOBALS['_W']['config']['setting']['proxy'])) {
  47. $urls = parse_url($GLOBALS['_W']['config']['setting']['proxy']['host']);
  48. if (!empty($urls['host'])) {
  49. curl_setopt($ch, CURLOPT_PROXY, "{$urls['host']}:{$urls['port']}");
  50. $proxytype = 'CURLPROXY_' . strtoupper($urls['scheme']);
  51. if (!empty($urls['scheme']) && defined($proxytype)) {
  52. curl_setopt($ch, CURLOPT_PROXYTYPE, constant($proxytype));
  53. } else {
  54. curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  55. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
  56. }
  57. if (!empty($GLOBALS['_W']['config']['setting']['proxy']['auth'])) {
  58. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $GLOBALS['_W']['config']['setting']['proxy']['auth']);
  59. }
  60. }
  61. }
  62. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  63. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  64. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  65. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  66. curl_setopt($ch, CURLOPT_SSLVERSION, 1);
  67. if (defined('CURL_SSLVERSION_TLSv1')) {
  68. curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
  69. }
  70. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');
  71. if (!empty($extra) && is_array($extra)) {
  72. $headers = array();
  73. foreach ($extra as $opt => $value) {
  74. if (strexists($opt, 'CURLOPT_')) {
  75. curl_setopt($ch, constant($opt), $value);
  76. } elseif (is_numeric($opt)) {
  77. curl_setopt($ch, $opt, $value);
  78. } else {
  79. $headers[] = "{$opt}: {$value}";
  80. }
  81. }
  82. if (!empty($headers)) {
  83. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  84. }
  85. }
  86. $data = curl_exec($ch);
  87. $status = curl_getinfo($ch);
  88. $errno = curl_errno($ch);
  89. $error = curl_error($ch);
  90. curl_close($ch);
  91. if ($errno || empty($data)) {
  92. return error(1, $error);
  93. } else {
  94. return ihttp_response_parse($data);
  95. }
  96. }
  97. $method = empty($post) ? 'GET' : 'POST';
  98. $fdata = "{$method} {$urlset['path']}{$urlset['query']} HTTP/1.1\r\n";
  99. $fdata .= "Host: {$urlset['host']}\r\n";
  100. if (function_exists('gzdecode')) {
  101. $fdata .= "Accept-Encoding: gzip, deflate\r\n";
  102. }
  103. $fdata .= "Connection: close\r\n";
  104. if (!empty($extra) && is_array($extra)) {
  105. foreach ($extra as $opt => $value) {
  106. if (!strexists($opt, 'CURLOPT_')) {
  107. $fdata .= "{$opt}: {$value}\r\n";
  108. }
  109. }
  110. }
  111. $body = '';
  112. if ($post) {
  113. if (is_array($post)) {
  114. $body = http_build_query($post);
  115. } else {
  116. $body = urlencode($post);
  117. }
  118. $fdata .= 'Content-Length: ' . strlen($body) . "\r\n\r\n{$body}";
  119. } else {
  120. $fdata .= "\r\n";
  121. }
  122. if ($urlset['scheme'] == 'https') {
  123. $fp = fsockopen('ssl://' . $urlset['host'], $urlset['port'], $errno, $error);
  124. } else {
  125. $fp = fsockopen($urlset['host'], $urlset['port'], $errno, $error);
  126. }
  127. stream_set_blocking($fp, true);
  128. stream_set_timeout($fp, $timeout);
  129. if (!$fp) {
  130. return error(1, $error);
  131. } else {
  132. fwrite($fp, $fdata);
  133. $content = '';
  134. while (!feof($fp))
  135. $content .= fgets($fp, 512);
  136. fclose($fp);
  137. return ihttp_response_parse($content, true);
  138. }
  139. }

第三步:解析分析微信服务器返回值并返回。

  1. if (is_error($resp)) {
  2. $procResult = $resp;
  3. } else {
  4. $arr=json_decode(json_encode((array) simplexml_load_string($resp['content'])), true);
  5. $xml = '<?xml version="1.0" encoding="utf-8"?>' . $resp['content'];
  6. $dom = new \DOMDocument();
  7. if ($dom->loadXML($xml)) {
  8. $xpath = new \DOMXPath($dom);
  9. $code = $xpath->evaluate('string(//xml/return_code)');
  10. $ret = $xpath->evaluate('string(//xml/result_code)');
  11. if (strtolower($code) == 'success' && strtolower($ret) == 'success') {
  12. $procResult = array('errno'=>0,'error'=>'success');;
  13. } else {
  14. $error = $xpath->evaluate('string(//xml/err_code_des)');
  15. $procResult = array('errno'=>-2,'error'=>$error);
  16. }
  17. } else {
  18. $procResult = array('errno'=>-1,'error'=>'未知错误');
  19. }
  20. }
  21.  
  22. return $procResult;
  23.  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号