New file |
| | |
| | | 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; |
| | | } |
| | | } |