企业付款的应用场景: 公众号向已关注用户付款,比如处理退款、财务结算等
材料:微信公众平台开发文档
1.证书需要用自己的商户里面的证书(注意:证书路径必须是绝对路径,如果使用相对路径会报下面的错误。
unable to use client certificate (no key found or wrong pass phrase?)
2.appid,secret以及key填写自己的即可。
先说一下实现思路:
1.首先获取openid,具体方法见下面。
2.填写必填参数,生成签名等,具体方法见下面。
/**
* 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’ # 签名
*/
参数参考: 企业付款API的文档
//信息回调文件所在的服务器位置
$str="http://www.xxx.com/company_pay/getInfo.php";
$str_url=urlencode($str);
$appid = "xxxx3e5273505e";
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$str_url.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
header("Location:".$url);
?>
$appid = "wxxxxx3505e";//你的微信公众平台的appid
$secret = "fxxxxx71xxx4cda2a671";//你微信公众平台的secret
$code = $_GET["code"];
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$get_token_url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
$json_obj = json_decode($res,true);
//根据openid和access_token查询用户信息
$access_token = $json_obj['access_token'];
$openid = $json_obj['openid'];
$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$get_user_info_url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
//解析json
$user_obj = json_decode($res,true);
//var_dump($user_obj);
echo "
"."-----".$openid."*****";
$mch_appid=$appid;
$mchid='10000401';//商户号
$nonce_str='vhmake'.rand(100000, 999999);//随机数
$partner_trade_no='VH'.time().rand(10000, 99999);//商户订单号
$openid=$openid;//用户唯一标识
$check_name='NO_CHECK';//校验用户姓名选项,NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名(未实名认证的用户会校验失败,无法转账)OPTION_CHECK:针对已实名认证的用户才校验真实姓名(未实名认证用户不校验,可以转账成功)
$re_user_name='[北京微函工坊科技有限公司](http://www.vhmake.com)';//用户姓名
$amount=100;//金额(以分为单位,必须大于100)
$desc='[北京微函工坊科技有限公司](http://www.vhmake.com)';//描述
$spbill_create_ip=$_SERVER["REMOTE_ADDR"];//请求ip
//封装成数据
$dataArr=array();
$dataArr['amount']=$amount;
$dataArr['check_name']=$check_name;
$dataArr['desc']=$desc;
$dataArr['mch_appid']=$mch_appid;
$dataArr['mchid']=$mchid;
$dataArr['nonce_str']=$nonce_str;
$dataArr['openid']=$openid;
$dataArr['partner_trade_no']=$partner_trade_no;
$dataArr['re_user_name']=$re_user_name;
$dataArr['spbill_create_ip']=$spbill_create_ip;
require 'api.php';
$sign=getSign($dataArr);
echo "-----
签名:".$sign."
*****";//die;
$data="
" .$mch_appid."
" .$mchid."
" .$nonce_str."
" .$partner_trade_no."
" .$openid."
" .$check_name."
" .$re_user_name."
" .$amount."
" .$desc."
" .$spbill_create_ip."
" .$sign."
";
//var_dump($data);
$ch = curl_init ();
$MENU_URL="https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
curl_setopt ( $ch, CURLOPT_URL, $MENU_URL );
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
$zs1="/xxxx/xxx/xxxxxx/apiclient_cert.pem";//注意:填写的路径必须为绝对路径,不可以填写相对路径
$zs2="/xxxx/xxx/xxxxx/apiclient_key.pem";
curl_setopt($ch,CURLOPT_SSLCERT,$zs1);
curl_setopt($ch,CURLOPT_SSLKEY,$zs2);
// curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01;
// Windows NT 5.0)');
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
$info = curl_exec ( $ch );
$infos=simplexml_load_string($info);
if (curl_errno ( $ch )) {
echo 'Errno:::' . curl_error ( $ch );
}
curl_close ( $ch );
echo "-----
请求返回值:";
echo $infos->return_code;
echo "
*****";
?>
/**
* 作用:格式化参数,签名过程需要使用
*/
function formatBizQueryParaMap($paraMap, $urlencode)
{
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v)
{
if($urlencode)
{
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
if (strlen($buff) > 0)
{
$reqPar = substr($buff, 0, strlen($buff)-1);
}
return $reqPar;
}
/**
* 作用:生成签名
*/
function getSign($Obj)
{
foreach ($Obj as $k => $v)
{
$Parameters[$k] = $v;
}
//签名步骤一:按字典序排序参数
ksort($Parameters);
$String = formatBizQueryParaMap($Parameters, false);
//echo '【string1】'.$String.'';
//签名步骤二:在string后加入KEY
$String = $String."&key=vhmake666vhmake666vhmake666vhmak";
//echo "【string2】".$String."";
//签名步骤三:MD5加密
$String = md5($String);
//echo "【string3】 ".$String."";
//签名步骤四:所有字符转为大写
$result_ = strtoupper($String);
//echo "【result】 ".$result_."";
return $result_;
}
<xml>
<return_code>return_code>
<return_msg>return_msg>
<mch_appid>mch_appid>
<mchid>mchid>
<device_info>device_info>
<nonce_str>nonce_str>
<result_code>result_code>
<partner_trade_no>partner_trade_no>
<payment_no>payment_no>
<payment_time>payment_time>
xml>
phpstorm小技巧:shift+F6给文件重命名