微信支付-商家给小程序用户转账教程(全网最详细JAVA版)

一.首页要开通商家转账到零钱功能,点产品设置,开启验密批量API,设置接口IP,这里的接口ip不是指用户的,而是你本机的 ip或服务器的ip微信支付-商家给小程序用户转账教程(全网最详细JAVA版)_第1张图片微信支付-商家给小程序用户转账教程(全网最详细JAVA版)_第2张图片
二.获取微信平台证书,注意微信平台证书和商户证书是有区别的,别弄混淆了

1.根据链接把获取平台证书的代码下载下来
https://github.com/wechatpay-apiv3/CertificateDownloader
2.可以根据github上面教程操作,我这边由于用jar包跑一直报错,所以直接修改源代码run方法,赋值变量

微信支付-商家给小程序用户转账教程(全网最详细JAVA版)_第3张图片
微信支付-商家给小程序用户转账教程(全网最详细JAVA版)_第4张图片
微信支付-商家给小程序用户转账教程(全网最详细JAVA版)_第5张图片

三. pom导入所需要的包
<dependency>
            <groupId>com.github.wechatpay-apiv3</groupId>
            <artifactId>wechatpay-apache-httpclient</artifactId>
            <version>0.4.8</version>
        </dependency>
四.接下来就是核心代码了,注意区分微信平台证书序列号和商户平台证书序列号,微信平台证书序列号是你上面保存文件下划线后面的数字,我这里把证书都放在了resources文件下面

在这里插入图片描述

package com.develop.utils;

import cn.hutool.json.JSONUtil;
import com.develop.commons.config.WxChatConfig;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import javax.crypto.IllegalBlockSizeException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.security.PrivateKey;
import java.util.*;
import static com.aliyun.oss.common.utils.HttpHeaders.CONTENT_TYPE;
import static com.google.common.net.HttpHeaders.ACCEPT;
import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON;

public class testUtils {
    /**
     * 微信通讯client
     *
     * @return CloseableHttpClient
     */
    public CloseableHttpClient getClient() {
        /**商户私钥文件*/
        InputStream privateKeyInputStream = this.getClass().getResourceAsStream("/WEB-INF/apiclient_key.pem"); //我这里把证书等文件都放在了resources文件下面
        /**微信平台公钥文件*/
        InputStream platformCertInputStream = this.getClass().getResourceAsStream("/WEB-INF/wechatpay.pem");
        PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKeyInputStream);
        WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
                .withMerchant(WxChatConfig.getMchIds() //商户号
                        , "*****EA4D372C4271C42E5FC285019A******" //商户证书序列号
                        , merchantPrivateKey)
                .withWechatPay(Arrays.asList(PemUtil.loadCertificate(platformCertInputStream)));
        CloseableHttpClient httpClient = builder.build();
        return httpClient;
    }


    /**
     * 发起批量转账API
     *
     * @throws IllegalBlockSizeException
     * @throws IOException
     */
    public static void batchPay(BigDecimal amount,String note,String openid) throws Exception{
        CloseableHttpClient httpClient = new testUtils().getClient();
        Map<String, Object> map = new HashMap<>();
        map.put("mchid", WxChatConfig.getMchIds()); //商户号
        Long batchNo = NumberUtils.createBatchNo();
        map.put("out_batch_no", "plfk2020042013"); //批次号
        map.put("batch_remark", "佣金提现"); //批次备注
        map.put("batch_name", "佣金提现"); //批次名称
        map.put("total_amount", 30); //金额 以分为单位
        map.put("total_num", 1);  //转账总笔数
        List<Map> list = new ArrayList<>();
        Map<String, Object> subMap = new HashMap<>(4);
        subMap.put("out_detail_no", "x23zy545Bd5436"); //商家明细单号
        subMap.put("transfer_amount", 30); //金额 以分为单位
        subMap.put("transfer_remark", "佣金提现"); //转账备注
        subMap.put("openid", openid); //openid
//        subMap.put("user_name", RsaCryptoUtil.encryptOAEP("收款用户姓名", x509Certificate));
        list.add(subMap);
        map.put("transfer_detail_list", list); //转账明细列表
        map.put("appid", WxChatConfig.getAppIds()); //appid
        String body = JSONUtil.toJsonStr(map);
        System.out.println("请求参数:" + body);


        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/transfer/batches");
        httpPost.addHeader(ACCEPT, APPLICATION_JSON.toString());
        httpPost.addHeader(CONTENT_TYPE, APPLICATION_JSON.toString());
        httpPost.addHeader("Wechatpay-Serial", "***FB92373276993B22AC815***F");//微信平台证书序列号

        httpPost.setEntity(new StringEntity(body, "UTF-8"));
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            String bodyAsString = EntityUtils.toString(response.getEntity());
            System.out.println("返回参数:" + bodyAsString);
        } finally {
            response.close();
        }
    }

    public static void main(String[] args) {
        try {
            batchPay(BigDecimal.valueOf(0.3),"备注","openid");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
五.运行main方法,返回下面参数就成功了
返回参数:{"batch_id":"1030001027501526891912022101301451696421","create_time":"2022-10-13T15:42:43+08:00","out_batch_no":"20221013154242073"}

微信支付-商家给小程序用户转账教程(全网最详细JAVA版)_第6张图片

最后:
有什么问题可以欢迎留言私信我

你可能感兴趣的:(java,微信支付,java,微信,小程序)