Pu Zhibing
2025-03-26 7f26677ab7f9b83697370fa142dd1686cdf4082a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.ruoyi.order.util.payment.wechat;
 
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;
import com.wechat.pay.java.service.transferbatch.model.*;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 微信支付工具类
 * @author zhibing.pu
 * @Date 2025/1/3 11:25
 */
public class TransferUtil {
    
    
    
    public static TransferBatchService service;
    
    
    /**
     * 初始化转账服务
     */
    public static void init(){
        // 初始化商户配置
        Config config = new RSAAutoCertificateConfig.Builder()
                        .merchantId(WechatProperty.merchantId)
                        // 使用 com.wechat.pay.java.core.util
                        // 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
                        .privateKeyFromPath(WechatProperty.privateKeyPath)
                        .merchantSerialNumber(WechatProperty.merchantSerialNumber)
                        .apiV3Key(WechatProperty.apiV3Key)
                        .build();
        
        // 初始化服务
        service = new TransferBatchService.Builder().config(config).build();
    }
    
    
    /**
     * 通过微信批次单号查询批次单
     * @return
     */
    public static TransferBatchEntity getTransferBatchByNo() {
        if(null == service){
            init();
        }
        GetTransferBatchByNoRequest request = new GetTransferBatchByNoRequest();
        
        return service.getTransferBatchByNo(request);
    }
    
    /** 通过商家批次单号查询批次单 */
    public static TransferBatchEntity getTransferBatchByOutNo() {
        GetTransferBatchByOutNoRequest request = new GetTransferBatchByOutNoRequest();
        return service.getTransferBatchByOutNo(request);
    }
    
    /**
     * 发起商家转账
     * @param outBatchNo
     * @param batchName
     * @param batchRemark
     * @param totalAmount
     * @param totalNum
     * @param notifyUrl
     * @param out_detail_no
     * @param transfer_amount
     * @param transferRemark
     * @param openid
     * @return
     */
    public static InitiateBatchTransferResponse initiateBatchTransfer(String outBatchNo, String batchName, String batchRemark, Long totalAmount,
                                                                      Integer totalNum, String notifyUrl, String out_detail_no, Long transfer_amount,
                                                                      String transferRemark, String openid) {
        InitiateBatchTransferRequest request = new InitiateBatchTransferRequest();
        request.setAppid(WechatProperty.appid);
        request.setOutBatchNo(outBatchNo);
        request.setBatchName(batchName);
        request.setBatchRemark(batchRemark);
        request.setTotalAmount(totalAmount);
        request.setTotalNum(totalNum);
        
        List<TransferDetailInput> transferDetailList = new ArrayList<>();
        TransferDetailInput transferDetailInput = new TransferDetailInput();
        transferDetailInput.setOutDetailNo(out_detail_no);
        transferDetailInput.setTransferAmount(transfer_amount);
        transferDetailInput.setTransferRemark(transferRemark);
        transferDetailInput.setOpenid(openid);
        transferDetailList.add(transferDetailInput);
        request.setTransferDetailList(transferDetailList);
        request.setNotifyUrl(notifyUrl);
        return service.initiateBatchTransfer(request);
    }
    
    /**
     * 通过微信明细单号查询明细单
     * @return
     */
    public static TransferDetailEntity getTransferDetailByNo() {
        GetTransferDetailByNoRequest request = new GetTransferDetailByNoRequest();
        return service.getTransferDetailByNo(request);
    }
    
    /**
     * 通过商家明细单号查询明细单
     * @return
     */
    public static TransferDetailEntity getTransferDetailByOutNo() {
        GetTransferDetailByOutNoRequest request = new GetTransferDetailByOutNoRequest();
        return service.getTransferDetailByOutNo(request);
    }
    
    
    
    
}