PHP实现微信企业付款
程序员文章站
2022-07-02 21:05:58
一、封装微信企业付款类WeiXinPayToUser,如下图代码所示: 二、调用WeiXinPayToUser,如下图所示: ......
一、封装微信企业付款类weixinpaytouser,如下图代码所示:
class weixinpaytouser { /** * api 参数 * @var array * 'mch_appid' # 公众号appid * 'mchid' # 商户号 * 'device_info' # 设备号 * 'nonce_str' # 随机字符串 * 'partner_trade_no' # 商户订单号 * 'openid' # 收款用户openid * 'check_name' # 校验用户姓名选项 针对实名认证的用户 * 're_user_name' # 收款用户姓名 * 'amount' # 付款金额 * 'desc' # 企业付款描述信息 * 'spbill_create_ip' # ip地址 * 'sign' # 签名 */ public $parameters = []; public $sslrootca_path=''; public $sslcert_path=''; public $sslkey_path=''; public $appid=''; public $secret=''; public $mchid=''; public $key='';//商户密钥 public function __construct() { $this->url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; $this->curl_timeout = 10; $this->sslrootca_path=dirname(__file__).'/weixin/cert/rootca.pem'; $this->sslcert_path=dirname(__file__).'/weixin/cert/apiclient_cert.pem'; $this->sslkey_path=dirname(__file__).'/weixin/cert/apiclient_key.pem'; } public function setparameter($key,$value){ $this->parameters[$key]=$value; } function arraytoxml($arr,$dom=0,$item=0){ if (!$dom){ $dom = new domdocument("1.0"); } if(!$item){ $item = $dom->createelement("xml"); $dom->appendchild($item); } foreach ($arr as $key=>$val){ $itemx = $dom->createelement(is_string($key)?$key:"item"); $item->appendchild($itemx); if (!is_array($val)){ $text = $dom->createtextnode($val); $itemx->appendchild($text); }else { $this->arraytoxml($val,$dom,$itemx); } } $dom->encoding = 'utf-8'; // insert proper return $dom->savexml(); } public function getsign($paramarr){//print_r($paramarr); ksort($paramarr); $paramstr = http_build_query($paramarr); $paramstr=urldecode($paramstr); $param_temp=$paramstr.'&key='.$this->key;//echo $param_temp.'<br>'; $signvalue=strtoupper(md5($param_temp));//echo $signvalue.'<br>'; return $signvalue; } /** * 生成请求xml数据 * @return string */ public function createxml() { $this->parameters['mch_appid'] = $this->appid; $this->parameters['mchid'] = $this->mchid; // $this->parameters['nonce_str'] = md5(time()); $this->parameters['nonce_str'] = 'dddfff'; $this->parameters['sign'] = $this->getsign($this->parameters); $a= $this->arraytoxml($this->parameters); //echo $a; return $a; } public function pay(){ $xml=$this->createxml(); $url=$this->url; return $this->postxmlsslcurl($xml,$url,$second=30); } /** * 作用:使用证书,以post方式提交xml到对应的接口url */ function postxmlsslcurl($xml,$url,$second=30) { $ch = curl_init(); //超时时间 curl_setopt($ch,curlopt_timeout,$second); //这里设置代理,如果有的话 //curl_setopt($ch,curlopt_proxy, '8.8.8.8'); //curl_setopt($ch,curlopt_proxyport, 8080); curl_setopt($ch,curlopt_url, $url); curl_setopt($ch,curlopt_ssl_verifypeer,false); curl_setopt($ch,curlopt_ssl_verifyhost,false); //设置header curl_setopt($ch,curlopt_header,false); //要求结果为字符串且输出到屏幕上 curl_setopt($ch,curlopt_returntransfer,true); //设置证书 //curl_setopt($ch,curlopt_cainfo, $this->sslrootca_path); //使用证书:cert 与 key 分别属于两个.pem文件 //默认格式为pem,可以注释 curl_setopt($ch,curlopt_sslcerttype,'pem'); curl_setopt($ch,curlopt_sslcert, $this->sslcert_path); //默认格式为pem,可以注释 curl_setopt($ch,curlopt_sslkeytype,'pem'); curl_setopt($ch,curlopt_sslkey, $this->sslkey_path); //post提交方式 curl_setopt($ch,curlopt_post, true); curl_setopt($ch,curlopt_postfields,$xml); $data = curl_exec($ch); //返回结果 if($data){ curl_close($ch); return $data; } else { $error = curl_errno($ch); echo "curl出错,错误码:$error"."<br>"; echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>"; curl_close($ch); return false; } } } ?>
二、调用weixinpaytouser,如下图所示:
/** * 企业付款测试 */ public function paytouser() {
$mchpay = new weixinpaytouser(); // 用户openid $mchpay->setparameter('openid', 'oy2lbszskklaslekthrzqezikebzqu'); // 商户订单号 $mchpay->setparameter('partner_trade_no', 'test-'.time()); // 校验用户姓名选项 $mchpay->setparameter('check_name', 'no_check'); // 企业付款金额 单位为分 $mchpay->setparameter('amount', 100); // 企业付款描述信息 $mchpay->setparameter('desc', '开发测试'); // 调用接口的机器ip地址 自定义 $mchpay->setparameter('spbill_create_ip', '127.0.0.1'); # getclientip() // 收款用户姓名 // $mchpay->setparameter('re_user_name', 'max wen'); // 设备信息 // $mchpay->setparameter('device_info', 'dev_server'); $response = $mchpay->postxmlssl(); if( !empty($response) ) { $data = simplexml_load_string($response, null, libxml_nocdata); echo json_encode($data); }else{ echo json_encode( array('return_code' => 'fail', 'return_msg' => 'transfers_接口出错', 'return_ext' => array()) ); } } }