无关风月
2025-04-09 cda724da35beb1ffd84955cc42bda5ab399ebbe1
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
package com.ruoyi.order.vx;
 
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.constant.WechatConstants;
import com.ruoyi.common.core.utils.StringUtils;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
 
public class TestWxPay {
    public static void main(String[] args) {
        weChatPay(BigDecimal.valueOf(0.1), "ouqOk6xtWA3JDmpd9HpZf9ijESIs", "1", "空调挂机");
    }
    private static boolean weChatPay(BigDecimal orderMoney, String openId, String withdrawId, String serverName) {
        if (StringUtils.isBlank(openId)) {
            return false;
        }
        BigDecimal maxTransferAmount = new BigDecimal("200000"); // 单次转账限额,单位为分
        int totalTransfers = orderMoney.multiply(new BigDecimal("100")).divide(maxTransferAmount, 0, RoundingMode.UP).intValue();
        boolean allTransfersSuccessful = true;
        for (int i = 0; i < totalTransfers; i++) {
            BigDecimal transferAmount;
            if (i < totalTransfers - 1) {
                transferAmount = maxTransferAmount;
            } else {
                // 最后一笔转账,金额为剩余金额
                transferAmount = orderMoney.multiply(new BigDecimal("100")).subtract(maxTransferAmount.multiply(new BigDecimal(i))).setScale(0, RoundingMode.DOWN);
            }
 
            Map<String, Object> postMap = new HashMap<>(8);
            postMap.put(WechatConstants.APP_ID, "wx98563d0ec9cf21c8");
            // 订单号
            postMap.put("out_bill_no", String.valueOf(UUID.randomUUID()).replaceAll("-", ""));
            System.err.println("====="+postMap.get("out_bill_no"));
            postMap.put(WechatConstants.OPEN_ID, openId);
            // 转账金额
            postMap.put("transfer_amount", transferAmount);
            postMap.put("transfer_scene_id", "1010");
            // 转账备注
            postMap.put("transfer_remark", "二手回收提现确认收款");
            // 回调地址
            postMap.put("notify_url", "https://hyhsbqgc.com/api/ruoyi-order/wx/wxChatPay");
            // 转账场景报备信息
            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> info = new HashMap<>();
            info.put("info_type","回收商品名称");
            info.put("info_content",serverName);
            list.add(info);
            postMap.put("transfer_scene_report_infos", list);
            String result = HttpUtil.postTransBatRequest(
                    WechatConstants.WE_CHAT_PAY_URL_PRE,
                    JSONObject.toJSONString(postMap),
                    "7EEA04429B006E12AAA421C002EC48BBEED5BE94",
                    "1665330417",
                    "/usr/local/vx/apiclient_key.pem", "/v3/fund-app/mch-transfer/transfer-bills");
            JSONObject jsonObject = JSONObject.parseObject(result);
//            WithdrawDetail withdrawDetail = new WithdrawDetail();
//            withdrawDetail.setWithdrawId(withdrawId);
//            withdrawDetail.setMoney(transferAmount);
//            withdrawDetail.setOutBatchNo((String) postMap.get(WechatConstants.OUT_BATCH_NO));
//            withdrawDetailService.save(withdrawDetail);
            System.err.println(jsonObject);
            if (jsonObject.containsKey(WechatConstants.CREATE_TIME)) {
                // 转账成功
                //保存转账明细
//                WithdrawDetail withdrawDetail = new WithdrawDetail();
//                withdrawDetail.setWithdrawId(withdrawId);
//                withdrawDetail.setMoney(transferAmount);
//                withdrawDetail.setOutBatchNo((String) postMap.get(WechatConstants.OUT_BATCH_NO));
//                withdrawDetailService.save(withdrawDetail);
 
 
            } else {
                allTransfersSuccessful = false;
                break;
            }
 
        }
 
        return allTransfersSuccessful;
    }
}