需求背景:微信提现到用户的零钱
官方文档:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_1
<?php
/**
* 功能说明:微信退款(企业付款到零钱)
*/
class WxRefund
{
//应用appid
public $pay_appid = '';
//商户id
public $pay_mchid = '';
//商户密钥
public $pay_mchkey = '';
public function __construct()
{
$this->pay_appid = config('WEIXINPAY.APPID');
$this->pay_mchid = config('WEIXINPAY.PAYMCHID');
$this->pay_mchkey = config('WEIXINPAY.PAYMCHKEY');
}
/**
* 作用:生成签名
*/
private function getSign($Parameters)
{
//签名步骤一:按字典序排序参数
ksort($Parameters);
$buff = "";
foreach ($Parameters as $k => $v) {
$buff .= $k . "=" . $v . "&";
}
$String = '';
if (strlen($buff) > 0) {
$String = substr($buff, 0, strlen($buff) - 1);
}
//签名步骤二:在string后加入KEY
$String = $String . "&key=" . $this->pay_mchkey;
//签名步骤三:MD5加密
$String = md5($String);
//签名步骤四:所有字符转为大写
$result_ = strtoupper($String);
return $result_;
}
/**
* 作用:array转xml
*/
private function arrayToXml($arr)
{
$xml = "<xml>";
foreach ($arr as $key => $val) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
}
$xml .= "</xml>";
return $xml;
}
/**
* xml转换为数组
*/
public function xmlToArray($xml)
{
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring), true);
return $val;
}
/**
* 作用:以post方式提交xml到对应的接口url
*/
private function postXmlCurl($xml, $url)
{
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url); //抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); // 增加 HTTP Header(头)里的字段
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 终止从服务端进行验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//(3)API证书安全
// 1.证书文件不能放在web服务器虚拟目录,应放在有访问权限控制的目录中,防止被他人下载;
// 2.建议将证书文件名改为复杂且不容易猜测的文件名;
// 3.商户服务器要做好病毒和木马防护工作,不被非法侵入者窃取证书文件。
$zs1 = "证书文件夹/weixin/********.pem"; // 证书地址
$zs2 = "证书文件夹/weixin/******.pem"; // 证书地址
curl_setopt($ch, CURLOPT_SSLCERT, $zs1);
curl_setopt($ch, CURLOPT_SSLKEY, $zs2);
$data = curl_exec($ch); //运行curl
if (curl_errno($ch)) {
echo 'Errno:' . curl_error($ch);
}
curl_close($ch);
return $data;
}
/**
* 产生随机字符串,不长于32位
*
* @param int $length
* @return 产生的随机字符串
*/
public static function getNonceStr($length = 32)
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
/**
* 企业付款到零钱
* @param string $partner_trade_no 商户订单号
* @param float $amount 发送的金额(分)
* @param string $re_openid 收款人的 openid
* @param string $desc 企业付款描述信息 (必填)
* @param string $check_name 收款用户姓名 (选填)
* @return [type] [description] 这里没有使用姓名强校验
*/
public function sendMoney($partner_trade_no, $amount, $re_openid, $desc = '测试', $check_name = '')
{
$data = array(
'mch_appid' => $this->pay_appid,// 商户账号appid
'mchid' => $this->pay_mchid,// 商户号
'nonce_str' => $this->getNonceStr(),// 随机字符串
'partner_trade_no' => $partner_trade_no,// 商户订单号
'openid' => $re_openid,// 用户openid
'check_name' => 'NO_CHECK',// 校验用户姓名选项, NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名
// 're_user_name' => $check_name,// 收款用户姓名
'amount' => $amount * 100,// 金额
'desc' => $desc,// 企业付款描述信息
// 'spbill_create_ip'=> IP,// Ip地址
);
//生成签名算法
$data['sign'] = $this->getSign($data);
$xml = $this->arrayToXml($data);
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; // 调用接口
$res = $this->postXmlCurl($xml, $url);
$return = $this->xmlToArray($res);
if($return['return_code'] == 'SUCCESS' && $return['result_code'] == 'SUCCESS' && $return['err_code'] == 'SUCCESS'){
return array('status'=>1,'payment_no'=>$return['payment_no']);
}else{
return array('status'=>0,'message'=>$return['err_code_des']);
}
}
}