public function wxTransferAccounts($setting, $data)
{
$appid = $setting['appid'];
$mchid = $setting['mchid'];
$out_order_no = "订单号";
$batch_name = "提现";
$batch_remark = "提现";
$total_amount = (int)bcmul($data['total_amount'], 100, 0);
$total_num = 1;
$transfer_detail_list = [[
'out_detail_no' => "订单号",
'transfer_amount' => (int)bcmul($data['total_amount'], 100, 0),
'transfer_remark' => '提现',
'openid' => $data['open_id']
]];
$arr = [
'appid' => $appid,
'mchid' => $mchid,
'out_batch_no' => $out_order_no,
'transfer_detail_list' => $transfer_detail_list,
'batch_name' => $batch_name,
'batch_remark' => $batch_remark,
'total_amount' => $total_amount,
'total_num' => $total_num
];
$url = "https://api.mch.weixin.qq.com/v3/transfer/batches";
$arr = json_encode($arr);
$header = $this->getV3Sign('POST', '/v3/transfer/batches', $arr, $setting);
$res = $this->curl_post($url, $arr, $header);
$result = json_decode($res, true);
if (isset($result['batch_status']) && $result['batch_status'] == "ACCEPTED") {
return ['result_code' => 'SUCCESS'];
} else {
return ['result_code' => 'FAIL', 'err_code_des' => $result['message']];
}
}
//post请求
public function curl_post($url, $data, $headers = array(), $is_post = true)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//设置header头
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($is_post) {
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
public function toUrlParams($values)
{
$buff = '';
foreach ($values as $k => $v) {
if ($k != 'sign' && $v != '' && !is_array($v)) {
$buff .= $k . '=' . $v . '&';
}
}
return trim($buff, '&');
}
public function makeSign($values, $key = null)
{
//签名步骤一:按字典序排序参数
ksort($values);
$string = $this->toUrlParams($values);
//签名步骤二:在string后加入KEY
$string = $string . '&key=' . $key;
$string = md5($string);
$string = hash_hmac("sha256", $string, $key);
//签名步骤四:所有字符转为大写
$result = strtoupper($string);
return $result;
}
public function getV3Sign($http_method = 'POST', $url = '', $body = '', $config)
{
//私密证书 serial_no 使用 openssl x509 -in cert.pem -noout -serial获取
$mch_private_key = $config['key_pem'];
$timestamp = time();//时间戳
$nonce = getRandom(32);//随机串
//构造签名串
$message = $http_method . "\n" . $url . "\n" . $timestamp . "\n" . $nonce . "\n" . $body . "\n";
//计算签名值
openssl_sign($message, $raw_sign, $mch_private_key, 'SHA256');
$sign = base64_encode($raw_sign);
//设置HTTP头
$token = sprintf('WECHATPAY2-SHA256-RSA2048 mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
$config['mchid'], $nonce, $timestamp, $config['serial_no'], $sign);
$headers = [
'Accept: application/json',
'User-Agent: */*',
'Content-Type: application/json; charset=utf-8',
'Authorization: ' . $token,
];
return $headers;
}
PHP 商家转账到零钱
最新推荐文章于 2024-05-11 08:31:42 发布