java 接入新版微信商家转账接口

发布于:2025-06-25 ⋅ 阅读:(16) ⋅ 点赞:(0)
Double price = preOrders.getPrice() * 100;


        //时间戳
        long timestamp = System.currentTimeMillis() / 1000;
        //加密算法。固定值,目前不支持其他方式
        String authType = "WECHATPAY2-SHA256-RSA2048";
        //生成nonce_str
        String nonceStr = WxPayKit.generateStr();
        //商家转账到零钱的请求地址后缀
        String urlSuffix = "/v3/fund-app/mch-transfer/transfer-bills";
        //发起转账的商户号
        //非对称加密,公钥证书的证书序列号
        String serialNo = apiKey;
        //非对称加密的私钥
        String privateKey = publicKey;

        //转账业务信息
        JSONObject rootJSON = baseInfo(preOrders.getOrderNo(), preUsers.getOpenId(), price.longValue());
        String body = JSONUtil.toJsonStr(rootJSON);
        System.out.println("入参json:" + body);
        // 调用IJPay框架的工具类生成待签名参数
        String buildSignMessage = PayKit.buildSignMessage(RequestMethodEnum.POST, urlSuffix, timestamp, nonceStr, body);
        //调用IJPay框架的工具类生成签名
        String signature = null;
        try {
            signature = PayKit.createSign(buildSignMessage, keyPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("signature:" + signature);
        // 调用IJPay框架的工具类,根据平台规则生成请求头 authorization
        String authorization = PayKit.getAuthorization(mchId, serialNo, nonceStr, String.valueOf(timestamp), signature, authType);
        System.out.println("Authorization:" + authorization);
        //完整的"商家转账到零钱"的请求链接
        String url = "https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills";
        //调用Hutool包的工具类发起post请求
        HttpResponse httpResponse = cn.hutool.http.HttpRequest.post(url)
                .header("Authorization", authorization)
                .header("Accept", "application/json")
                .header("Wechatpay-Serial", apiKey)
                .header("Content-Type", "application/json")
                .body(body)
                .execute();
        com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(httpResponse.body());

上面是转账接口

注意:用户唤醒转账后,24小时未领取则进行锁单。这时候就需要查询订单是否转出,未转出则重新更改订单号

下面是 查询接口

 Config config =
                new RSAPublicKeyConfig.Builder()
                        .merchantId(mchId) //微信支付的商户号
                        .privateKeyFromPath(keyPath) // 商户API证书私钥的存放路径
                        .publicKeyFromPath(pub_key) //微信支付公钥的存放路径
                        .publicKeyId(publicKey) //微信支付公钥ID
                        .merchantSerialNumber(apiKey) //商户API证书序列号
                        .apiV3Key(apiKey3) //APIv3密钥
                        .build();
        String requestPath = "https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills/out-bill-no/{out_bill_no}";
        requestPath = requestPath.replace("{out_bill_no}", UrlEncoder.urlEncode(outBillNo));
        HttpHeaders headers = new HttpHeaders();
        headers.addHeader("Accept", MediaType.APPLICATION_JSON.getValue());
        headers.addHeader("Content-Type", MediaType.APPLICATION_JSON.getValue());
        HttpRequest httpRequest =
                new HttpRequest.Builder()
                        .httpMethod(HttpMethod.GET)
                        .url(requestPath)
                        .headers(headers)
                        .build();
        HttpClient httpClient = new DefaultHttpClientBuilder().config(config).build();
        com.wechat.pay.java.core.http.HttpResponse<TransferBatchEntity> execute = httpClient.execute(httpRequest, TransferBatchEntity.class);
        log.info("WxPayService.getTransferDetailByOutNoNew response:{}"+execute.getBody().toString());
        return execute.getBody().toString();