<?php
$url = 'https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills';
$serial_no = ''; // 证书编号
$mch_id = ''; // 商户号
$data = [
//appid
'appid' => '',
//单号
'out_bill_no' => 'HJH'.time(),
//转账场景ID
'transfer_scene_id' => '1000',
//用户openid
'openid' => '',
//转账金额
'transfer_amount' => 10,
//转账备注
'transfer_remark' => '大转盘中奖',
//转账场景报备信息参数 https://pay.weixin.qq.com/doc/v3/merchant/4013774588
'transfer_scene_report_infos'=>[
[
"info_type" => "活动名称",
"info_content" => "大转盘中奖"
],
[
"info_type" => "奖励说明",
"info_content" => "中三等奖"
]
]
];
// 生成签名
$timestamp = time();
$nonceStr = uniqid();
$body = json_encode($data, JSON_UNESCAPED_UNICODE);
// echo '<pre>';
// var_dump($body);die;
$signature = generateSignature($data, $timestamp, $nonceStr, 'POST', $url, $body);
// 构造请求头
$headers = [
'Content-Type: application/json',
'Accept: application/json',
'User-Agent: PHP-CURL', // 必须添加
'Authorization: '.sprintf(
'WECHATPAY2-SHA256-RSA2048 mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',
$mch_id,
$nonceStr,
$signature,
$timestamp,
$serial_no // 商户证书序列号 // 商户证书序列号
)
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
// 执行操作
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
function generateSignature($config, $timestamp, $nonceStr, $method, $url, $body)
{
$message = sprintf("%s\n%s\n%d\n%s\n%s\n",
$method,
parse_url($url, PHP_URL_PATH),
$timestamp,
$nonceStr,
$body
);
$privateKey = openssl_pkey_get_private(file_get_contents('./1709932101_20250410_cert/apiclient_key.pem'));
if (!$privateKey) {
throw new Exception('加载私钥失败');
}
openssl_sign($message, $signature, $privateKey, 'sha256WithRSAEncryption');
return base64_encode($signature);
}
?>
原创文章,作者:易码农,如若转载,请注明出处:https://moon0421.top/?post=247
打赏
微信扫一扫
支付宝扫一扫
在线ps网站
上一篇
2025-3-25 11:39