Java App微信支付后台代码

WXPayUtil

package example.wx;


import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.util.*;

public class WXPayUtil {

    private static String APP_ID;
    private static String MCH_ID;
    private static String API_KEY;
    private static String NOTIFY_URL;

    public static void initWxPay(String appId, String mchId, String apiKey, String notifyUrl) {
        APP_ID = appId;
        MCH_ID = mchId;
        API_KEY = apiKey;
        NOTIFY_URL = notifyUrl;
    }

    private static final String UNIFIEDORDER_ADDRESS = "https://api.mch.weixin.qq.com/pay/unifiedorder";

    public static class WXPayReq {

        private String appid;
        private String partnerid;
        private String prepayid;
        private String packageValue; // package
        private String noncestr;
        private String timestamp;
        private String sign;

        private String getAppid() {
            return appid;
        }

        private void setAppid(String appid) {
            this.appid = appid;
        }

        private String getPartnerid() {
            return partnerid;
        }

        private void setPartnerid(String partnerid) {
            this.partnerid = partnerid;
        }

        private String getPrepayid() {
            return prepayid;
        }

        private void setPrepayid(String prepayid) {
            this.prepayid = prepayid;
        }

        private String getPackageValue() {
            return packageValue;
        }

        private void setPackageValue(String packageValue) {
            this.packageValue = packageValue;
        }

        private String getNoncestr() {
            return noncestr;
        }

        private void setNoncestr(String noncestr) {
            this.noncestr = noncestr;
        }

        private String getTimestamp() {
            return timestamp;
        }

        private void setTimestamp(String timestamp) {
            this.timestamp = timestamp;
        }

        public String getSign() {
            return sign;
        }

        public void setSign(String sign) {
            this.sign = sign;
        }
    }

    public static WXPayReq requestPayReq(String subject, String body, String price, String paymentCode, String ip) {
        List<KeyValue<String, String>> orderInfo = generateOrderInfo(subject, body, price, paymentCode, ip);
        String orderInfoStr = toXml(orderInfo);
        WXPayReq payReq = new WXPayReq();
        try {
            URL httpUrl = new URL(UNIFIEDORDER_ADDRESS);
            HttpURLConnection httpURLConnection = (HttpURLConnection) httpUrl.openConnection();
            httpURLConnection.setRequestProperty("Host", "api.mch.weixin.qq.com");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10 * 1000);
            httpURLConnection.setReadTimeout(10 * 1000);
            httpURLConnection.connect();
            OutputStream outputStream = httpURLConnection.getOutputStream();
            outputStream.write(orderInfoStr.getBytes("UTF-8"));
            //获取内容
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            final StringBuffer stringBuffer = new StringBuffer();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line);
            }
            String result = stringBuffer.toString();
            bufferedReader.close();
            inputStream.close();
            outputStream.close();
            Map<String, String> xml = decodeXml(result);
            payReq.setAppid(APP_ID);
            payReq.setNoncestr(genNonceStr());
            payReq.setPackageValue("Sign=WXPay");
            payReq.setPartnerid(MCH_ID);
            payReq.setPrepayid(xml.get("prepay_id"));
            payReq.setTimestamp(String.valueOf(System.currentTimeMillis() / 1000));
            payReq.setSign(generateSign(payReq));
            return payReq;
        } catch (Exception e) {
            return null;
        }
    }

    private static String generateSign(WXPayReq payReq) {
        List<KeyValue<String, String>> signParams = new LinkedList<KeyValue<String, String>>();
        signParams.add(new KeyValue<>("appid", payReq.getAppid()));
        signParams.add(new KeyValue<>("noncestr", payReq.getNoncestr()));
        signParams.add(new KeyValue<>("package", payReq.getPackageValue()));
        signParams.add(new KeyValue<>("partnerid", payReq.getPartnerid()));
        signParams.add(new KeyValue<>("prepayid", payReq.getPrepayid()));
        signParams.add(new KeyValue<>("timestamp", payReq.getTimestamp()));
        StringBuilder sb = new StringBuilder();
        for (KeyValue<String, String> keyValue : signParams) {
            sb.append(keyValue.getKey()).append('=').append(keyValue.getValue()).append('&');
        }
        sb.append("key=");
        sb.append(API_KEY);
        return MD5.getMessageDigest(sb.toString().getBytes()).toUpperCase();
    }

    private static Map<String, String> decodeXml(String strXML) {
        try {
            Map<String, String> data = new HashMap<>();
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
            org.w3c.dom.Document doc = documentBuilder.parse(stream);
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getDocumentElement().getChildNodes();
            for (int idx = 0; idx < nodeList.getLength(); ++idx) {
                Node node = nodeList.item(idx);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    org.w3c.dom.Element element = (org.w3c.dom.Element) node;
                    data.put(element.getNodeName(), element.getTextContent());
                }
            }
            try {
                stream.close();
            } catch (Exception ex) {
            }
            return data;
        } catch (Exception ex) {
            return null;
        }
    }

    private static List<KeyValue<String, String>> generateOrderInfo(String subject, String body, String price, String paymentCode, String ip) {
        String nonceStr = genNonceStr();
        List<KeyValue<String, String>> orderInfo = new LinkedList<>();
        try {
            orderInfo.add(new KeyValue<>("appid", APP_ID));
            orderInfo.add(new KeyValue<>("body", new String(subject.getBytes("UTF-8"))));
            orderInfo.add(new KeyValue<>("detail", new String(body.getBytes("UTF-8"))));
            orderInfo.add(new KeyValue<>("mch_id", MCH_ID));
            orderInfo.add(new KeyValue<>("nonce_str", nonceStr));
            orderInfo.add(new KeyValue<>("notify_url", NOTIFY_URL));
            orderInfo.add(new KeyValue<>("out_trade_no", paymentCode));
            orderInfo.add(new KeyValue<>("spbill_create_ip", ip));
            orderInfo.add(new KeyValue<>("total_fee", price));
            orderInfo.add(new KeyValue<>("trade_type", "APP"));
            orderInfo.add(new KeyValue<>("sign", genPackageSign(orderInfo)));
        } catch (Exception e) {
        }
        return orderInfo;
    }

    /**
     * 生成签名
     */
    private static String genPackageSign(List<KeyValue<String, String>> orderInfo) {
        StringBuilder sb = new StringBuilder();
        for (KeyValue<String, String> keyValue : orderInfo) {
            sb.append(keyValue.getKey());
            sb.append('=');
            sb.append(keyValue.getValue());
            sb.append('&');
        }
        sb.append("key=");
        sb.append(API_KEY);
        return MD5.getMessageDigest(sb.toString().getBytes()).toUpperCase();
    }

    private static String toXml(List<KeyValue<String, String>> params) {
        StringBuilder sb = new StringBuilder();
        sb.append("<xml>");
        for (KeyValue<String, String> keyValue : params) {
            sb.append("<").append(keyValue.getKey()).append(">").append(keyValue.getValue()).append("</").append(keyValue.getKey()).append(">");
        }
        sb.append("</xml>");
        return sb.toString();
    }

    private static String genNonceStr() {
        Random random = new Random();
        return MD5.getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());
    }

    private static class MD5 {
        private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7",
                "8", "9", "a", "b", "c", "d", "e", "f"};

        private MD5() {
        }

        private final static String getMessageDigest(byte[] buffer) {
            char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
            try {
                MessageDigest mdTemp = MessageDigest.getInstance("MD5");
                mdTemp.update(buffer);
                byte[] md = mdTemp.digest();
                int j = md.length;
                char str[] = new char[j * 2];
                int k = 0;
                for (int i = 0; i < j; i++) {
                    byte byte0 = md[i];
                    str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                    str[k++] = hexDigits[byte0 & 0xf];
                }
                return new String(str);
            } catch (Exception e) {
                return null;
            }
        }

        /**
         * MD5编码
         *
         * @param origin 原始字符串
         * @return 经过MD5加密之后的结果
         */
        public static String MD5Encode(String origin) {
            String resultString = null;
            try {
                resultString = origin;
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(resultString.getBytes("UTF-8"));
                resultString = byteArrayToHexString(md.digest());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return resultString;
        }

        /**
         * 转换字节数组为16进制字串
         *
         * @param b 字节数组
         * @return 16进制字串
         */
        public static String byteArrayToHexString(byte[] b) {
            StringBuilder resultSb = new StringBuilder();
            for (byte aB : b) {
                resultSb.append(byteToHexString(aB));
            }
            return resultSb.toString();
        }

        /**
         * 转换byte到16进制
         *
         * @param b 要转换的byte
         * @return 16进制格式
         */
        private static String byteToHexString(byte b) {
            int n = b;
            if (n < 0) {
                n = 256 + n;
            }
            int d1 = n / 16;
            int d2 = n % 16;
            return hexDigits[d1] + hexDigits[d2];
        }
    }

    private static class KeyValue<K, V> implements Map.Entry<K, V> {

        private K key;
        private V value;

        public KeyValue(K key, V value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public K getKey() {
            return key;
        }

        @Override
        public V getValue() {
            return value;
        }

        @Override
        public V setValue(V value) {
            return this.value = value;
        }

    }
}

使用方法

WXPayUtil.initWxPay(APP_ID, MCH_ID, API_KEY, NOTIFY_URL);
        WXPayUtil.WXPayReq req = WXPayUtil.requestPayReq("Subject", "Body", "2", "hhh" + System.currentTimeMillis(), "127.0.0.1");
        System.out.println(new Gson().toJson(req));
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,921评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,635评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,393评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,836评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,833评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,685评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,043评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,694评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,671评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,670评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,779评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,424评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,027评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,984评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,214评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,108评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,517评论 2 343

推荐阅读更多精彩内容