微信-商家转账

<?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

打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025-3-25 11:39
版权所有:易码农
文章标题:微信-商家转账
除非注明,文章均为 易码农 原创,请勿用于任何商业用途,禁止转载

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

sitemap