php 商户转账到微信零钱

文章详细介绍了如何使用微信商户API进行转账操作,包括开通权限、设置appid、获取商户号和证书信息,以及使用PHP编写代码进行转账请求,涉及签名验证和加密过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先在微信商户开通此功能(开通可能不易)

拿到  商户号   证书 还有证书序列号   关联商户的appid(如果是app在开放平台申请appid小程序的话那就是小程序的appid)   注意一定要关联appid   

在微信商户号里   开通 api 权限 以及添加服务器的ip

上代码

public function zhuanzhang()
{
    $post_data = [
                "appid" => $setUp->wxc_app_id,//appid
                "out_bill_no" => $order_on,//商家单号
                "transfer_scene_id" => "1010",//商家批次单号
                'openid' => $tInfo->zhifubaozhanghu,
                "transfer_amount" => intval(strval($tInfo->daozhangjine * 100)),// 转账金额单位为“分”
                "transfer_remark" => "用户提现转账", // 转账总笔数
                "transfer_scene_report_infos" => [
                    [
                        'info_type' => "回收商品名称",
                        'info_content' => "回收旧衣服",
                    ]
                ]
            ];
            $url = 'https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills';
            $result= self::wx_post($url, json_encode($post_data,
                JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
    $result = json_decode($result, true);
                
    if(isset($result['code'])){
        $this->error($result['massage']);
    }
}



public static function wx_post($url, $param)
    {
        $authorization = self::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;
    }
    
    
    
    public static function getV3Sign($url, $http_method, $body)
    {
        $nonce = strtoupper(self::createNonceStr(32));
        $timestamp = time();
        $url_parts = parse_url($url);
        $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
        $cert_dir = '证书路径'; //绝对路径
        $sslKeyPath = $cert_dir."apiclient_key.pem";
        //拼接参数
        $message = $http_method . "\n" .
            $canonical_url . "\n" .
            $timestamp . "\n" .
            $nonce . "\n" .
            $body . "\n";
        $private_key = self::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"', "商户号", $nonce, $timestamp, "证书序列号", $sign);
        return $token;
    }



    public static 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;
    }


    public static function getPrivateKey($filepath = '')
    {
        if (empty($filepath)) {
            //私钥位置
            $cert_dir = '证书位置';
            $filepath = $cert_dir."apiclient_key.pem";
        }
        return openssl_get_privatekey(file_get_contents($filepath));
    }


到此微信商户转账到用户零钱结束   (注意微信商户号里一定要有钱)  //不然会报错

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值