微信提现
// 微信提现接口开始
$info['amount'] = $info['price']; // 金额
$rand = rand(11111111, 99999999);
$info['orderno'] = '2023' . $rand; // 订单号
$info['openid'] = $userInfo['openid']; // openid
$resArr = $this->tixian($info, $userInfo);
// 提现 企业付款到零钱
public function tixian($info = [], $userInfo)
{
if (empty($info)) {
return false;
}
$wechat_withdrawal_set = WechatWithdrawalSet::where('status', 1)->find();
$mchid = $wechat_withdrawal_set['txmchid']; // https://pay.weixin.qq.com 产品中心-开发配置-商户号
$appid = $wechat_withdrawal_set['txappid']; // 微信支付申请对应的公众号的APPID
$url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
$batch_name = $wechat_withdrawal_set['notes']; // 付款备注
$pars = [];
$pars['appid'] = $appid; // 直连商户的appid
$pars['out_batch_no'] = 'sjzz' . date('Ymd') . mt_rand(1000, 9999); // 商户系统内部的商家批次单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一
$pars['batch_name'] = $batch_name; // 该笔批量转账的名称
$pars['batch_remark'] = $batch_name; // 转账说明,UTF8编码,最多允许32个字符
$pars['total_amount'] = intval(strval($info['amount'] * 100)); // 转账总金额 单位为“分”
$pars['total_num'] = 1; // 转账总笔数
$pars['transfer_detail_list'][0] = [
'out_detail_no' => $info['orderno'],
'transfer_amount' => intval(strval($info['amount'] * 100)),
'transfer_remark' => $batch_name,
'openid' => $info['openid']
]; // 转账明细列表
$token = $this->getToken($pars, $mchid, $userInfo); // 获取token
$res = $this->https_request($url, json_encode($pars), $token); // 发送请求
$resArr = json_decode($res, true);
return $resArr;
}
public function getToken($pars, $merchant_id, $userInfo)
{
$wechat_withdrawal_set = WechatWithdrawalSet::where('id', 1)->find();
$SSLCERT_PATH = 'public/' . $wechat_withdrawal_set['apiclient_cert']; // api证书路径
$SSLKEY_PATH = 'public/' . $wechat_withdrawal_set['apiclient_key']; // 密钥证书路径
// $SSLCERT_PATH = 'public/cert/apiclient_cert.pem';// api证书路径
// $SSLKEY_PATH = 'public/cert/apiclient_key.pem';// 密钥证书路径
$url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
$http_method = 'POST'; // 请求方法(GET,POST,PUT)
$timestamp = time(); // 请求时间戳
$url_parts = parse_url($url); // 获取请求的绝对URL
$nonce = $timestamp . rand('10000', '99999'); // 请求随机串
$body = json_encode((object) $pars); // 请求报文主体
$stream_opts = [
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false
]
];
// 证书
$apiclient_cert_path = Env::get('root_path') . $SSLCERT_PATH;
$apiclient_key_path = Env::get('root_path') . $SSLKEY_PATH;
// dump($apiclient_cert_path);die;
$apiclient_cert_arr = openssl_x509_parse(file_get_contents($apiclient_cert_path, false, stream_context_create($stream_opts)));
$serial_no = $apiclient_cert_arr['serialNumberHex']; // 证书序列号
$mch_private_key = file_get_contents($apiclient_key_path, false, stream_context_create($stream_opts)); // 密钥
// $merchant_id = '';//商户id
$canonical_url = ($url_parts['path'] . (! empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
$message = $http_method . "\n" . $canonical_url . "\n" . $timestamp . "\n" . $nonce . "\n" . $body . "\n";
openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
$sign = base64_encode($raw_sign); // 签名
$schema = 'WECHATPAY2-SHA256-RSA2048';
$token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $merchant_id, $nonce, $timestamp, $serial_no, $sign); // 微信返回token
return $token;
}