微信官方又更新了接口。。。

V3版本的微信商家转账到零钱的接口---俗称提现接口

注意事项


一:开通条件:需满足入驻满90天,连续正常交易30天,保持正常健康交易。
二:分为页面发起和api接口操作,均需要手动开启才可。
三:需要设置IP白名单,否则不能发起接口调用。
四:商家转账的户口为商户平台的运营账户,非商户基本账户,注意保证运营账户的余额充足!
五:注意提现额度的变化,超过设置的免密额度,该笔提现会自动发送给超级管理员审核,输入密码确定此笔提现操作。

class Withdraw
{
public $mchid = '你的商户id';
public $appid = '小程序appid';
public $serial_no = '你的证书序列号'; // 证书序列号,不知道怎么获取的看上面的图 public function transfer($openid, $trade_no, $money, $desc='备注说明')
{
$money = (int)($money * 100); $post_data = [
"appid" => $this->appid,//appid
"out_batch_no" => $trade_no,//商家批次单号
"batch_name" => $desc,//批次名称
"batch_remark" => $desc,//批次备注
"total_amount" => $money,// 转账金额单位为“分”
"total_num" => 1, // 转账总笔数
//此处可以多笔提现 组合二维数组放到transfer_detail_list即可 我这里单笔操作,写死了
"transfer_detail_list" => [
[
'out_detail_no' => $trade_no,
'transfer_amount' => $money,
'transfer_remark' => $desc,
'openid' => $openid,
]
]
];
$url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
//JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE 防止中文被转义
$result = $this->wx_post($url, json_encode($post_data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)); $result = json_decode($result, true);
if (isset($result['prepay_id'])) {
return $result['prepay_id'];
}
return $result;
} /**post请求
* @param $url
* @param $param
* @return bool|string
*/
private function wx_post($url, $param)
{
$authorization = $this->getV3Sign($url, "POST", $param);
$curl = curl_init();
$headers = [
'Authorization:' . $authorization,
'Accept:application/json',
'Content-Type:application/json;charset=utf-8',
'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
curl_setopt($curl, CURLOPT_POST, true);
$res = curl_exec($curl);
curl_close($curl);
return $res;
} /**
* 微信提现V3签名
* @param $url
* @param $http_method
* @param $body
* @return string
*/
private function getV3Sign($url, $http_method, $body)
{
$nonce = strtoupper($this->createNonceStr(32));
$timestamp = time();
$url_parts = parse_url($url);
$canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
$cert_dir = ROOT_PATH.DS."config".DS."payment_cert".DS."wechatpay".DS;
$sslKeyPath = $cert_dir."apiclient_key.pem";
//拼接参数
$message = $http_method . "\n" .
$canonical_url . "\n" .
$timestamp . "\n" .
$nonce . "\n" .
$body . "\n";
$private_key = $this->getPrivateKey($sslKeyPath);
openssl_sign($message, $raw_sign, $private_key, 'sha256WithRSAEncryption');
$sign = base64_encode($raw_sign);
$token = sprintf('WECHATPAY2-SHA256-RSA2048 mchid="%s",nonce_str="%s",timestamp="%s",serial_no="%s",signature="%s"', $this->mchid, $nonce, $timestamp, $this->serial_no, $sign);
return $token;
} /**
* 生成随机32位字符串
* @param $length
* @return string
*/
public function createNonceStr($length = 16) { //生成随机16个字符的字符串
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
} /**
* 获取私钥
* @param $filepath
* @return false|resource
*/
private function getPrivateKey($filepath = '')
{
if (empty($filepath)) {
//私钥位置
$cert_dir = ROOT_PATH.DS."config".DS."payment_cert".DS."wechatpay".DS;
$filepath = $cert_dir."apiclient_key.pem";
}
return openssl_get_privatekey(file_get_contents($filepath));
}
}

文章摘自:https://blog.zmzhz.com/archives/119.html

最新文章

  1. curl的登录总结
  2. centos 6.5 yum安装 mysql 5.6
  3. MLlib决策树与集成树
  4. 前端学习笔记 - Css初级篇
  5. 打造smali代码库辅助分析
  6. SPSS数据分析—简单线性回归
  7. WebService wsdl没有types的问题
  8. MFC编程基础
  9. 【git】git常用命令简介
  10. Zend Studio 12 大集合
  11. JSON的详细介绍
  12. win10 + Debian9.1双系统安装笔记
  13. 【转】用systemJS+karma+Jasmine+babel环境去编写简单的ES6工程
  14. 华为交换机以 LACP 模式实现链路聚合
  15. [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
  16. php程序开发之实现网页跳转
  17. golang:slice切片
  18. python利用lxml读写xml格式文件
  19. PHP数组和字符串的处理函数汇总
  20. 2、以自定义struct或struct指针作为map的Key

热门文章

  1. guide, manual, tutorial之间的区别
  2. Spring Boot上传文件功能的开发
  3. 关于git基本操作备忘
  4. mysql 获取某个时间段内每天的数据
  5. react组件传参记录,防止以后忘记,欢迎指正讨论
  6. 每日一抄 Go语言等待组
  7. Software--EB--Project 身份验证
  8. SQL Server创建表,存储过程,function函数脚本规范
  9. c语言中位运算符及用法 异或
  10. vue3+ts获取dom元素高度