Java 微信商家打款到零钱(旧版本接口)

旧版微信支付接口要求请求时携带证书请求

构建请求参数

    /**
     * 付款到零钱
     *
     * @param withdrawalDto dto撤军
     * @return {@link Map }<{@link String }, {@link Object }>
     * @throws Exception 异常
     * @Author chen 2023-07-27 15:04
     */
    private Map<String, Object> payToUser(WithdrawalDto withdrawalDto) throws Exception {
        log.info("发起微信提现零钱请求");
        Map<String, String> paramMap = new HashMap<>();
        // 公众账号appid
        paramMap.put("mch_appid", wechatConfig.getAppId());
        // 微信支付分配的商户号
        paramMap.put("mchid", wechatConfig.getMchId());
        // 随机字符串,不长于32位。
        paramMap.put("nonce_str", RandomUtil.randomString(16));
        // 商户订单号,需保持唯一性
        paramMap.put("partner_trade_no", withdrawalDto.getSerialNo());
        // 商户appid下,某用户的openid
        paramMap.put("openid", withdrawalDto.getCardNo());
        //校验用户姓名选项   NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名
        paramMap.put("check_name", "FORCE_CHECK");
        //收款用户姓名,如果check_name设置为FORCE_CHECK,则必填用户真实姓名
        paramMap.put("re_user_name", withdrawalDto.getIdCardName());
        // 企业付款金额,金额必须为整数 单位为分
        //元转分
        BigDecimal applyMoney = withdrawalDto.getApplyMoney();
        BigDecimal amount = applyMoney.multiply(BigDecimal.valueOf(100));
        paramMap.put("amount", String.valueOf(amount.intValue()));
        // 企业付款描述信息
        paramMap.put("desc", "钱包提现");
        // 根据微信签名规则,生成签名
        paramMap.put("sign", WechatUtils.sign(paramMap, wechatConfig.getMchApiKey()));
        log.info("请求参数:{}", paramMap);
        // 把参数转换成XML数据格式
        String xmlParam = XmlUtil.mapToXmlStr(paramMap, "xml");
        String resXml = WechatUtils.httpsSSLPost(wechatConfig.getWithdrawalHost(), wechatConfig.getMchId(), xmlParam);
        Map<String, Object> resultMap = XmlUtil.xmlToMap(resXml);
        log.info("响应报文:{}", resultMap);
        return resultMap;
    }

签名

/**
     * 微信支付签名sign
     *
     * @param param
     * @param key
     * @return {@link String }
     * @Author chen 2023-07-27 14:37
     */
    public static String sign(Map<String, String> param, String key) {
        try {
            //签名步骤一:按字典排序参数
            List<Object> list = new ArrayList<>(param.keySet());
            Object[] ary = list.toArray();
            Arrays.sort(ary);
            list = Arrays.asList(ary);
            StringBuilder str = new StringBuilder();
            for (Object o : list) {
                str.append(o).append("=").append(param.get(String.valueOf(o))).append("&");
            }
            //签名步骤二:加上key
            str.append("key=").append(key);
            //步骤三:加密并大写
            return DigestUtil.md5Hex(str.toString(), "utf-8").toUpperCase();
        } catch (Exception e) {
            log.error("微信签名失败:{}", e.getMessage());
            return null;
        }
    }

http 请求工具类--携带证书请求

证书路径为相对路径(放在 resources 目录下):youPath/cert.p12

/**
     * https ssl post
     *
     * @param url    url
     * @param mchId
     * @param params 参数个数
     * @return {@link String }
     * @throws Exception 异常
     * @Author chen 2023-07-27 14:45
     */
    public static String httpsSSLPost(String url, String mchId, String params) throws Exception {
        String text = "";
        // 指定读取证书格式为PKCS12
        KeyStore keyStore = KeyStore.getInstance(CERT_TYPE);
        // 读取本机存放的PKCS12证书文件
        try (InputStream stream = ResourceUtil.getStream(CERT_P12_PATH)) {
            // 指定PKCS12的密码(商户ID)
            keyStore.load(stream, mchId.toCharArray());
        }
        // Trust own CA and all self-signed certs
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchId.toCharArray()).build();
        // Allow TLSv1 protocol only
        // 指定TLS版本
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{TLS}, null,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        // 设置httpclient的SSLSocketFactory
        try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) {
            HttpPost post = new HttpPost(url);
            StringEntity s = new StringEntity(params, "utf-8");
            s.setContentType(CONTENT_TYPE);
            post.setEntity(s);
            HttpResponse res = httpclient.execute(post);
            HttpEntity entity = res.getEntity();
            text = EntityUtils.toString(entity, "utf-8");
        }
        return text;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值