1. 首页
  2. 新闻资讯
  3.  
  4. 建站经验
  5.  

php实现微信企业转账功能

发布时间:2018-11-28 10:51:41 来源:脚本之家
本文实例为大家分享了php实现微信企业转账的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 配置账号信息
* 配置要和证书在一起!!!!
*/
 
class WxTransfersConfig
{
 //=======【基本信息设置】==============
 //
 /**
  * TODO: 修改这里配置为您自己申请的商户信息
  * 微信公众号信息配置
  *
  * APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
  *
  * MCHID:商户号(必须配置,开户邮件中可查看)
  *
  * KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
  *
  */
 const APPID = '';
 const MCHID = '';
 const KEY = '';
 //=======【证书路径设置】=====================================
 /**
  * TODO:设置商户证书路径
  * 证书路径,注意应该填写绝对路径,发送红包和查询需要,可登录商户平台下载
  * API证书下载地址:https://pay.weixin.qq.com/index.php/account/api_cert,下载之前需要安装商户操作证书)
  * @var path 跟这个文件同一目录下的cert文件夹放置证书!!!!
  */
 const SSLCRET12 = 'cert/apiclient_cert.p12';
 const SSLCERT_PATH = 'cert/apiclient_cert.pem';
 const SSLKEY_PATH = 'cert/apiclient_key.pem';
 const SSLROOTCA = 'cert/rootca.pem';
  
 //=======【证书路径设置】=====================================
 /**
  * 获取文件的路径,证书需要完整路径
  * @return string
  */
 public static function getRealPath(){
  return __DIR__.'/';
 }
}

微信企业转账工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
require_once "WxTransfers.Config.php";
 
/**
 * 微信企业转账工具类
 */
class WxTransfers
{
 // 企业转账请求地址
  
 //获取转账信息地址
  
 // 转账需要的配置 'wxappid','mch_id','key'
 private $_keys;
  
 // 转账需要的证书文件 'api_cert', 'api_key', 'rootca',请传入绝对路径!!!
 private $_cert;
  
 protected $log_file;
  
 public $error;
  
 // 相关配置必备参数
 protected $_parameters = array();
  
 // 最后一次生产的订单号
 protected $_lastPartnerTradeNo;
  
 // 记录最后一次发送请求的结果对象
 protected $_lastResult;
  
 // 最后一次随机数
 protected $_lastRandNum;
  
 public function __construct($config)
 {
  $keys = array(
   'wxappid',
   'mch_id',
   'key'
  );
  $files = array(
   'api_cert',
   'api_key',
   'rootca'
  );
  
  foreach ($keys as $key) {
   try {
    $this->_keys[$key] = $config[$key];
   } catch (Exception $e) {
    throw new Exception('参数缺失:' . $key);
   }
  }
  
  foreach ($files as $file) {
   try {
    $cret_file = $config[$file];
    if (is_file($cret_file)) {
     $this->_cert[$file] = $cret_file;
    }
   } catch (Exception $e) {
    throw new Exception('证书错误');
   }
  }
 }
 
 public function transfers($parameters){
 
  $this->log($parameters, 'SEND_PARAM');
   
  $this->setParameter('mchid', $this->_keys['mch_id']);
  $this->setParameter('mch_appid', $this->_keys['wxappid']);
   
  $must = array(
   'openid',
   'check_name',
   're_user_name',
   'amount',
   'desc',
   'spbill_create_ip',
  );
  foreach ($must as $key) {
   if (isset($parameters[$key]) && $parameters[$key]) {
    $this->setParameter($key, $parameters[$key]);
   } else
    if (! isset($this->_parameters[$key]) || ! $this->_parameters[$key]) {
     $this->error = '参数缺损:' . $key;
     return false;
    }
  }
  if (! isset($parameters['partner_trade_no'])) {
   $parameters['partner_trade_no'] = $this->getPartnerTradeNo();
  }
   
  $this->setParameter('partner_trade_no', $parameters['partner_trade_no']);
   
  $this->setParameter('nonce_str', $this->getRand(30, 3));
   
  $postXml = $this->_createXml();
   
  if (! $postXml) {
   return false;
  }
  $this->log($postXml, 'SEND_XML');
   
  $result = $this->curl_post_ssl(self::TRANSFERS_URL, $postXml);
   
  $this->log($result, 'RESULT_XML');
   
  if (! $result) {
   return false;
  }
  $resultObj = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
   
  $this->_lastResult = $resultObj;
   
  if ($resultObj->return_code == 'SUCCESS') { // 成功标识
     
   if ($resultObj->result_code == 'SUCCESS') {
   
    return $resultObj->send_listid;
   }
     
   if ($resultObj->return_msg) {
    $this->error = (string) $resultObj->return_msg;
    return false;
   }
     
   $this->error = (string) $resultObj->err_code_des;
   return false;
  }
   
  if ($resultObj->return_code != 'FAIL') {
   $this->error = '返回信息格式异常';
   return false;
  }
   
  $this->error = (string) $resultObj->return_msg;
  return false;
 }
  
 /**
  * 获取转账信息
  * @param unknown $partner_trade_no
  * @return boolean|SimpleXMLElement
  */
 public function getInfo($partner_trade_no){
  $param = array(
   'nonce_str' => $this->getRand(30, 3),
   'partner_trade_no'=> $partner_trade_no ,
   'mch_id' => $this->_keys['mch_id'],
   'appid'  => $this->_keys['wxappid'],
  );
   
  ksort($param);
  $unSignParaString = $this->_formatQueryParaMap($param, false);
  $param['sign'] = $this->_sign($unSignParaString, $this->_keys['key']);
   
  $xml = $this->arrayToXml($param);
   
  $this->log($xml, 'GETINFO_XML');
   
  $result = $this->curl_post_ssl(self::GETINFO_URL, $xml);
   
  if(!$result){
   return false ;
  }
   
  $this->log($result, 'RESULT_XML');
   
  $resultObj = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
  $this->_lastResult = $resultObj ;
  if($resultObj->return_code == 'SUCCESS'){//成功标识
   
   if($resultObj->result_code == 'SUCCESS'){
    return $resultObj ;
   }
   
   if($resultObj->return_msg){
    $this->error = $resultObj->return_msg ;
    return false ;
   }
   
   $this->error = $resultObj->err_code_des ;
   return false ;
  }
   
  if($resultObj->return_code != 'FAIL'){
   $this->error = '返回信息格式异常';
   return false ;
  }
   
  $this->error = $resultObj->return_msg ;
  return false ;
 }
 /**
  * 设置所需要的参数
  * @param $parameter 键值数组/键
  * @param $value 值
  * @return WxBonusApi
  */
 public function setParameter($parameter, $value = null)
 {
  if (! is_array($parameter)) {
   return $this->setParameter(array(
    $parameter => $value
   ));
  }
  
  foreach ($parameter as $key => $value) {
   $key = trim($key);
   $value = trim($value);
   $this->_parameters[$key] = $value;
  }
  return $this;
 }
  
 /**
  * 获取参数值
  * @param $parameter 键名
  * @return multitype:
  */
 public function getParameter($parameter)
 {
  return $this->_parameters[$parameter];
 }
  
 /**
  * 获取随机数
  * @param number $len 随机数的位数
  * @param number $type 取值范围 1表示数字 2小写字母 4大写字母
  * @return string
  */
 public function getRand($len = 30, $type = 0)
 {
  $str = '';
  $max = - 1;
  
  if (! $type) {
   $type = 3;
  }
  
  if ($type & 1) {
   $str .= '1234567890';
   $max += 10;
  }
  
  if ($type & 2) {
   $str .= 'abcdefghijklmnopqrstuvwxyz';
   $max += 26;
  }
  
  if ($type & 4) {
   $str .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
   $max += 26;
  }
  
  $rand = '';
  for ($i = 0; $i < $len; $i ++) {
   $rand .= $str[rand(0, $max)];
  }
  
  return $rand;
 }
  
 /**
  * 生成商户的订单号
  * @return string
  */
 public function getPartnerTradeNo()
 {
  $this->_lastPartnerTradeNo = $this->_parameters['mch_id'] . date('YmdHis') . $this->getRand(4, 1); // $this->getRandNum();
  return $this->_lastPartnerTradeNo;
 }
  
  
 /**
  * 获取最后一次创建生成的订单号
  * @return string
  */
 public function getLastPartnerTradeNo()
 {
  return $this->_lastPartnerTradeNo;
 }
  
  
  
 /**
  * 创建XML的方法
  * @param number $retcode
  * @param string $reterrmsg
  * @return boolean|string
  */
 private function _createXml()
 {
  try {
   $sign = $this->_getSign();
   if (! $sign) {
    return false;
   }
   $this->setParameter('sign', $sign);
     
   return $this->arrayToXml($this->_parameters);
  } catch (Exception $e) {
   $this->error = $e->getMessage();
   return false;
  }
 }
  
  
 /**
  * 参数转换成XML
  * @param array $arr 参数数组
  * @return string
  */
 public function arrayToXml($arr)
 {
  $xml = "";
  foreach ($arr as $key => $val) {
   if (is_numeric($val)) {
    $xml .= "<" . $key . ">" . $val . ". $key . ">";
   } else {
    $xml .= "<" . $key . ">. $val . "]]>. $key . ">";
   }
  }
  $xml .= "";
  return $xml;
 }
  
 /**
  * 获得签名结果
  * @return boolean|Ambigous
  */
 protected function _getSign()
 {
  try {
     
   if ($this->_checkSign() == false) { // 检查生成签名参数
    $this->error = '生成签名参数缺失!';
    $this->log(json_encode($this->_parameters, JSON_UNESCAPED_UNICODE), 'ERROR_Sign_XML');
    return false;
   }
     
   ksort($this->_parameters);
   $unSignParaString = $this->_formatQueryParaMap($this->_parameters, false);
     
   return $this->_sign($unSignParaString, $this->_keys['key']);
  } catch (Exception $e) {
   $this->error = $e->getMessage();
   return false;
  }
 }
  
 /**
  * 检查签名所需参数是否齐全
  * @return boolean
  */
 private function _checkSign()
 {
  // return true; 
  if ($this->_parameters["mch_appid"] == null ||
   $this->_parameters["mchid"] == null ||
   //$this->_parameters["device_info"] == null || 设备id
   $this->_parameters["nonce_str"] == null ||
   $this->_parameters["partner_trade_no"] == null ||
   $this->_parameters["openid"] == null ||
   $this->_parameters["check_name"] == null ||
   $this->_parameters["re_user_name"] == null ||
   $this->_parameters["desc"] == null ||
   $this->_parameters["spbill_create_ip"] == null) {
    return false;
   }
   return true;
 }
  
 /**
  *
  * @param $paraMap
  * @param $urlencode
  * @return string
  */
 private function _formatQueryParaMap($paraMap,$urlencode)
 {
  $buff = "";
  ksort($paraMap);
  foreach ($paraMap as $k => $v) {
   if (null != $v && "null" != $v && "sign" != $k) {
    if ($urlencode) {
     $v = urlencode($v);
    }
    $buff .= $k . "=" . $v . "&";
   }
  }
  $reqPar;
  if (strlen($buff) > 0) {
   $reqPar = substr($buff, 0, strlen($buff) - 1);
  }
  return $reqPar;
 }
  
  
 /**
  * 签名
  * @param $content 签名的字符串
  * @param $key 密钥
  * @throws Exception
  * @return string|boolean
  */
 private function _sign($content, $key)
 {
  try {
   if (null == $key) {
    $this->error = '签名key不能为空!';
    return false;
   }
   if (null == $content) {
    $this->error = '签名内容不能为空';
    return false;
   }
   $signStr = $content . "&key=" . $key;
  
   return strtoupper(md5($signStr));
     
  } catch (Exception $e) {
   $this->error = $e->getMessage();
   return false;
  }
 }
  
 /**
  * cURL抓取
  *
  * @param $url 链接地址
  * @param $vars 参数
  * @param
  *   $second
  * @param
  *   $aHeader
  * @return mixed|boolean
  */
 function curl_post_ssl($url, $data, $second = 30, $aHeader = array())
 {
  $ch = curl_init();
  // 超时时间
  curl_setopt($ch, CURLOPT_TIMEOUT, $second);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  // 这里设置代理,如果有的话
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  // cert 与 key 分别属于两个.pem文件
  curl_setopt($ch, CURLOPT_SSLCERT, $this->_cert['api_cert']);
  curl_setopt($ch, CURLOPT_SSLKEY, $this->_cert['api_key']);
  curl_setopt($ch, CURLOPT_CAINFO, $this->_cert['rootca']);
  if (count($aHeader) >= 1) {
   curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
  }
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  $data = curl_exec($ch);
  if ($data) {
   curl_close($ch);
   return $data;
  } else {
   $this->log(json_encode($this->_cert));
   $this->error = 'aa:'.curl_errno($ch);
   curl_close($ch);
   return false;
  }
 }
  
 /**
  * 获取服务器ip
  *
  * @return string
  */
 public function getServerIp()
 {
  $server_ip = '127.0.0.1';
  if (isset($_SERVER)) {
   if (isset($_SERVER['SERVER_ADDR']) && $_SERVER['SERVER_ADDR']) {
    $server_ip = $_SERVER['SERVER_ADDR'];
   } elseif (isset($_SERVER['LOCAL_ADDR']) && $_SERVER['LOCAL_ADDR']) {
    $server_ip = $_SERVER['LOCAL_ADDR'];
   }
  } else {
   $server_ip = getenv('SERVER_ADDR');
  }
  return $server_ip;
 }
  
 /**
  * 设置日志目录文件
  *
  * @param unknown $file
  */
 public function setLogFile($file)
 {
  $this->log_file = $file;
 }
  
 /**
  * 写日志
  *
  * @param $msg 写入的信息
  * @param $type 日志类型作为查询标示
  */
 public function log($msg, $type)
 {
  if ($this->log_file) {
   $log = str_replace(array(
    "\r\n",
    "\r",
    "\n"
   ), array(
    "",
    "",
    ""
   ), $msg);
   error_log($type . ' ' . date('Y-m-d H:i:s') . ' ' . json_encode($log,JSON_UNESCAPED_UNICODE) . "\r\n", 3, $this->log_file);
  }
 }
  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
include 'WxTransfers.Api.php';
class WxTransfers{
 
/**
 *调用方法即可测试
 */
 public function index(){
   
  $path = WxTransfersConfig::getRealPath(); // 证书文件路径
  $config['wxappid'] = WxTransfersConfig::APPID;
  $config['mch_id'] = WxTransfersConfig::MCHID;
  $config['key'] = WxTransfersConfig::KEY;
  $config['PARTNERKEY'] = WxTransfersConfig::KEY;
  $config['api_cert'] = $path . WxTransfersConfig::SSLCERT_PATH;
  $config['api_key'] = $path . WxTransfersConfig::SSLKEY_PATH;
  $config['rootca'] = $path . WxTransfersConfig::SSLROOTCA;
   
  $wxtran=new WxTransfers($config);
   
  $wxtran->setLogFile('D:\\transfers.log');//日志地址
   
  //转账
  $data=array(
   'openid'=>'',//openid
   'check_name'=>'NO_CHECK',//是否验证真实姓名参数
   're_user_name'=>'11',//姓名
   'amount'=>100,//最小1元 也就是100
   'desc'=>'企业转账测试',//描述
   'spbill_create_ip'=>$wxtran->getServerIp(),//服务器IP地址
  );
  var_dump(json_encode($wxtran->transfers($data),JSON_UNESCAPED_UNICODE));
  var_dump($wxtran->error);
 
  //获取转账信息
  var_dump($wxtran->getInfo('11111111'));
  var_dump($wxtran->error);
 }
  
}

以上就是本文的全部内容,希望对大家的学习有所帮助

新闻资讯

 

服务项目

 

联系我们

  • 咨询电话:13316437003
  • 地址:广东省深圳市福田区八卦四路中浩大厦2楼201
  • 业务咨询:点击这里给我发消息
  • 技术服务:点击这里给我发消息
  • 售后邮箱:service@dcnop.com
  • 技术邮箱:zcl@dcnop.com

 

微信公众号

  • 客服:点击这里给客服发消息

    技术:点击这里给技术发消息

    大诚网络--深圳网站制作|深圳网站优化|深圳网站运营

    客户服务热线

    0755-82400810

    在线客服