栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 微信企业付款到个人银行卡-调用获取RSA公钥API

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 微信企业付款到个人银行卡-调用获取RSA公钥API

一、使用微信sdk的Xml转换工具类
import org.w3c.dom.document;

import javax.xml.XMLConstants;
import javax.xml.parsers.documentBuilder;
import javax.xml.parsers.documentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


public final class WXPayXmlUtil {
    public static documentBuilder newdocumentBuilder() throws ParserConfigurationException {
        documentBuilderFactory documentBuilderFactory = documentBuilderFactory.newInstance();
        documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        documentBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        documentBuilderFactory.setXIncludeAware(false);
        documentBuilderFactory.setExpandEntityReferences(false);

        return documentBuilderFactory.newdocumentBuilder();
    }

    public static document newdocument() throws ParserConfigurationException {
        return newdocumentBuilder().newdocument();
    }
}
二、进行携带证书的http的请求,获取公钥

package co.ganxiang.mp.test;
import java.io.*;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.*;

import javax.net.ssl.SSLContext;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import co.ganxiang.mp.utils.base64;
import co.ganxiang.mp.utils.RSAUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class HttpClientSSL {
    Logger log = LogManager.getLogger(HttpClientSSL.class);

    private static String payUrl = "https://fraud.mch.weixin.qq.com/risk/getpublickey";
    //商户key
    private static String KEY = "xxxxxxxxxx";
    //商户mch_id
    private static String mchId = "xxxxxxxxx";

    public static void main(String[] args) throws Exception {
        Map parameterMap = new HashMap<>();
        parameterMap.put("mch_id", mchId);
        parameterMap.put("nonce_str", generateNonceStr());
        try{

            parameterMap.put("sign_type", "MD5");
            parameterMap.put("sign", HttpClientSSL.generateSignature(parameterMap,KEY,"MD5"));
            //要以xml格式传递
            String postDataXML = HttpClientSSL.mapToXml(parameterMap);
            String result = HttpClientSSL.wxRefundlink(postDataXML, mchId );

            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    
    public static String mapToXml(Map data) throws Exception {
        org.w3c.dom.document document = WXPayXmlUtil.newdocument();
        org.w3c.dom.Element root = document.createElement("xml");
        document.appendChild(root);
        for (String key: data.keySet()) {
            String value = data.get(key);
            if (value == null) {
                value = "";
            }
            value = value.trim();
            org.w3c.dom.Element filed = document.createElement(key);
            filed.appendChild(document.createTextNode(value));
            root.appendChild(filed);
        }
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        String output = writer.getBuffer().toString(); //.replaceAll("n|r", "");
        try {
            writer.close();
        }
        catch (Exception ex) {
        }
        return output;
    }

    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final Random RANDOM = new SecureRandom();
    
    public static String generateNonceStr() {
        char[] nonceChars = new char[32];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars);
    }
    public static String generateSignature(Map data, String key, String signType) throws Exception {
        Set keySet = data.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray) {
            if (k.equals("sign")) {
                continue;
            }
            if (data.get(k).trim().length() > 0) // 参数值为空,则不参与签名
                sb.append(k).append("=").append(data.get(k).trim()).append("&");
        }
        sb.append("key=").append(key);
        if ("MD5".equals(signType)) {
            return MD5(sb.toString()).toUpperCase();
        }
        else {
            throw new Exception(String.format("Invalid sign_type: %s", signType));
        }
    }
    
    public static String MD5(String data) throws Exception {
        java.security.MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }

    private static String  wxRefundlink(String postDataXML,String mch_id) throws Exception{
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        try{
            //获取微信支付api证书
            KeyStore keyStore = getCertificate(mch_id);
            SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore, mch_id.toCharArray()).build();
            SSLConnectionSocketFactory sslf = new SSLConnectionSocketFactory(sslContext);
            httpClient = HttpClients.custom().setSSLSocketFactory(sslf).build();
            HttpPost httpPost = new HttpPost(payUrl);

            StringEntity reqEntity = new StringEntity(postDataXML);
            // 设置类型
            reqEntity.setContentType("application/x-www-form-urlencoded");
            httpPost.setEntity(reqEntity);
            String result = null;
            httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            result = EntityUtils.toString(httpEntity, "UTF8");
            EntityUtils.consume(httpEntity);
            return result;
        }finally {//关流
            httpClient.close();
            httpResponse.close();
        }

    }

    
    private static KeyStore getCertificate(String mch_id){
        //try-with-resources 关流
        try (FileInputStream inputStream = new FileInputStream(new File("C:\Users\Administrator\Desktop\apiclient_cert.p12"))//证书文件位置) {
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            keyStore.load(inputStream, mch_id.toCharArray());
            return keyStore;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

三、打印出来的公钥可以到SSL在线工具-在线RSA公私钥格式转换|公钥PKCS1与PKCS8转换|私钥PKCS1与PKCS8转换-SSLeye官网进行转换

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/666315.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号