php实现微信企业转账功能
程序员文章站
2022-11-10 08:42:37
本文实例为大家分享了php实现微信企业转账的具体代码,供大家参考,具体内容如下
本文实例为大家分享了php实现微信企业转账的具体代码,供大家参考,具体内容如下
<?php /** * 配置账号信息 * 配置要和证书在一起!!!! */ class wxtransfersconfig { //=======【基本信息设置】============== // /** * todo: 修改这里配置为您自己申请的商户信息 * 微信公众号信息配置 * * appid:绑定支付的appid(必须配置,开户邮件中可查看) * * mchid:商户号(必须配置,开户邮件中可查看) * * key:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置) * 设置地址:https://pay.weixin.qq.com/index.php/account/api_cert * */ 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__.'/'; } }
微信企业转账工具类:
<?php require_once "wxtransfers.config.php"; /** * 微信企业转账工具类 */ class wxtransfers { // 企业转账请求地址 const transfers_url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //获取转账信息地址 const getinfo_url='https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo'; // 转账需要的配置 '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 = "<xml>"; foreach ($arr as $key => $val) { if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . "</" . $key . ">"; } else { $xml .= "<" . $key . "><![cdata[" . $val . "]]></" . $key . ">"; } } $xml .= "</xml>"; return $xml; } /** * 获得签名结果 * @return boolean|ambigous <string, boolean> */ 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); } } }
<?php 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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 前两天下雨没事就休息了
下一篇: 朋友家的插座长这样,不舍得怎么下手啊!