com.appleframework.pay.trade.utils.WeiXinPayUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.appleframework.pay.trade.utils.WeiXinPayUtils.java

Source

/*
 * Copyright 2015-2102 RonCoo(http://www.appleframework.com) Group.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.appleframework.pay.trade.utils;

import com.appleframework.pay.common.core.utils.StringUtil;
import com.appleframework.pay.trade.entity.weixinpay.WeiXinPrePay;
import com.appleframework.pay.trade.enums.weixinpay.WeiXinTradeTypeEnum;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

/**
 * <b>:
 * </b>
 * @author  Cruise.Xu
 * <a href="http://www.appleframework.com">appleframework(http://www.appleframework.com)</a>
 */
public class WeiXinPayUtils {

    private static final Logger LOG = LoggerFactory.getLogger(WeiXinPayUtils.class);

    /**
     * ??xml?,?
     * @param requestUrl
     * @param requestMethod
     * @param xmlStr
     * @return
     */
    public static Map<String, Object> httpXmlRequest(String requestUrl, String requestMethod, String xmlStr) {
        // ?HashMap
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            HttpsURLConnection urlCon = (HttpsURLConnection) (new URL(requestUrl)).openConnection();
            urlCon.setDoInput(true);
            urlCon.setDoOutput(true);
            // ?GET/POST
            urlCon.setRequestMethod(requestMethod);

            if ("GET".equalsIgnoreCase(requestMethod)) {
                urlCon.connect();
            }

            urlCon.setRequestProperty("Content-Length", String.valueOf(xmlStr.getBytes().length));
            urlCon.setUseCaches(false);
            // gbk?????
            if (null != xmlStr) {
                OutputStream outputStream = urlCon.getOutputStream();
                outputStream.write(xmlStr.getBytes("UTF-8"));
                outputStream.flush();
                outputStream.close();
            }
            InputStream inputStream = urlCon.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            // ??
            SAXReader reader = new SAXReader();
            Document document = reader.read(inputStreamReader);
            // xml
            Element root = document.getRootElement();
            // ?
            @SuppressWarnings("unchecked")
            List<Element> elementList = root.elements();
            // ???
            for (Element e : elementList) {
                map.put(e.getName(), e.getText());
            }
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            urlCon.disconnect();
        } catch (MalformedURLException e) {
            LOG.error(e.getMessage());
        } catch (IOException e) {
            LOG.error(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }
        return map;
    }

    /**
     *  ?XML
     * @param weiXinPrePay
     * @param partnerKey
     * @return
     */
    public static String getPrePayXml(WeiXinPrePay weiXinPrePay, String partnerKey) {

        getPrePaySign(weiXinPrePay, partnerKey);//???

        StringBuilder sb = new StringBuilder();
        sb.append("<xml><appid>").append(weiXinPrePay.getAppid()).append("</appid>");
        sb.append("<body>").append(weiXinPrePay.getBody()).append("</body>");
        sb.append("<device_info>").append(weiXinPrePay.getDeviceInfo()).append("</device_info>");
        sb.append("<mch_id>").append(weiXinPrePay.getMchId()).append("</mch_id>");
        sb.append("<nonce_str>").append(weiXinPrePay.getNonceStr()).append("</nonce_str>");
        sb.append("<notify_url>").append(weiXinPrePay.getNotifyUrl()).append("</notify_url>");
        if (WeiXinTradeTypeEnum.NATIVE.name().equals(weiXinPrePay.getTradeType())) {
            sb.append("<product_id>").append(weiXinPrePay.getProductId()).append("</product_id>");
        } else if (WeiXinTradeTypeEnum.JSAPI.name().equals(weiXinPrePay.getTradeType())) {
            sb.append("<openid>").append(weiXinPrePay.getOpenid()).append("</openid>");
        }
        sb.append("<out_trade_no>").append(weiXinPrePay.getOutTradeNo()).append("</out_trade_no>");
        sb.append("<spbill_create_ip>").append(weiXinPrePay.getSpbillCreateIp()).append("</spbill_create_ip>");
        sb.append("<time_start>").append(weiXinPrePay.getTimeStart()).append("</time_start>");
        sb.append("<time_expire>").append(weiXinPrePay.getTimeExpire()).append("</time_expire>");
        sb.append("<total_fee>").append(weiXinPrePay.getTotalFee()).append("</total_fee>");
        sb.append("<trade_type>").append(weiXinPrePay.getTradeType().name()).append("</trade_type>");
        sb.append("<sign>").append(weiXinPrePay.getSign()).append("</sign>");
        sb.append("</xml>");

        return sb.toString();
    }

    /**
     * ???
     * @param appid ?ID
     * @param mch_id    ?
     * @param device_info   ?
     * @param trade_type    
     * @param prePay    Map
     * @param partnerKey    ??EY
     * @return
     */
    public static String geWeiXintPrePaySign(String appid, String mch_id, String device_info, String trade_type,
            Map<String, Object> prePay, String partnerKey) {
        Map<String, Object> preParams = new HashMap<String, Object>();
        if (!StringUtil.isEmpty(prePay.get("return_code"))) {
            preParams.put("return_code", prePay.get("return_code"));
        }
        if (!StringUtil.isEmpty(prePay.get("return_msg"))) {
            preParams.put("return_msg", prePay.get("return_msg"));
        }
        if (!StringUtil.isEmpty(prePay.get("appid"))) {
            preParams.put("appid", appid);
        }
        if (!StringUtil.isEmpty(prePay.get("mch_id"))) {
            preParams.put("mch_id", mch_id);
        }
        if (!StringUtil.isEmpty(prePay.get("device_info"))) {
            preParams.put("device_info", device_info);
        }
        if (!StringUtil.isEmpty(prePay.get("nonce_str"))) {
            preParams.put("nonce_str", prePay.get("nonce_str"));
        }
        if (!StringUtil.isEmpty(prePay.get("result_code"))) {
            preParams.put("result_code", prePay.get("result_code"));
        }
        if ("FAIL".equals(prePay.get("result_code"))) {
            if (!StringUtil.isEmpty(prePay.get("err_code"))) {
                preParams.put("err_code", prePay.get("err_code"));
            }
            if (!StringUtil.isEmpty(prePay.get("err_code_des"))) {
                preParams.put("err_code_des", prePay.get("err_code_des"));
            }
        }
        if (!StringUtil.isEmpty(prePay.get("trade_type"))) {
            preParams.put("trade_type", trade_type);
        }
        if (!StringUtil.isEmpty(prePay.get("prepay_id"))) {
            preParams.put("prepay_id", prePay.get("prepay_id"));
        }
        if (!StringUtil.isEmpty(prePay.get("code_url"))) {
            preParams.put("code_url", prePay.get("code_url"));
        }
        String argPreSign = getStringByMap(preParams) + "&key=" + partnerKey;
        String preSign = MD5Util.encode(argPreSign).toUpperCase();
        return preSign;
    }

    public static boolean notifySign(Map<String, String> result, String sign, String partnerKey) {
        String argNotifySign = getStringByStringMap(result) + "&key=" + partnerKey;
        String notifySign = MD5Util.encode(argNotifySign).toUpperCase();
        if (notifySign.equals(sign)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * ???
     * @param weiXinPrePay
     * @param partnerKey
     * @return
     */
    private static void getPrePaySign(WeiXinPrePay weiXinPrePay, String partnerKey) {

        Map<String, Object> prePayMap = new HashMap<String, Object>();
        prePayMap.put("appid", weiXinPrePay.getAppid());// ?ID
        prePayMap.put("mch_id", weiXinPrePay.getMchId()); // ?
        prePayMap.put("device_info", weiXinPrePay.getDeviceInfo());
        prePayMap.put("nonce_str", weiXinPrePay.getNonceStr()); // ?
        prePayMap.put("body", weiXinPrePay.getBody()); // ???
        prePayMap.put("out_trade_no", weiXinPrePay.getOutTradeNo()); // ??
        prePayMap.put("total_fee", weiXinPrePay.getTotalFee()); // ?
        prePayMap.put("spbill_create_ip", weiXinPrePay.getSpbillCreateIp()); // IP
        prePayMap.put("time_start", weiXinPrePay.getTimeStart()); // 
        prePayMap.put("time_expire", weiXinPrePay.getTimeExpire()); // 
        prePayMap.put("notify_url", weiXinPrePay.getNotifyUrl()); // URL
        prePayMap.put("trade_type", weiXinPrePay.getTradeType().name()); // 
        if (WeiXinTradeTypeEnum.NATIVE.name().equals(weiXinPrePay.getTradeType())) {
            prePayMap.put("product_id", weiXinPrePay.getProductId()); //?ID
        } else if (WeiXinTradeTypeEnum.JSAPI.name().equals(weiXinPrePay.getTradeType())) {
            prePayMap.put("openid", weiXinPrePay.getOpenid()); // openid
        }

        String argPreSign = getStringByMap(prePayMap) + "&key=" + partnerKey;
        String preSign = MD5Util.encode(argPreSign).toUpperCase();
        weiXinPrePay.setSign(preSign);
    }

    /**
     * ???
     * @param weiXinPrePay
     * @param partnerKey
     * @return
     */
    public static Map<String, String> getPrePayMapForAPP(Map<String, Object> prePayRequest, String partnerKey) {

        // ID appid String(32)  wx8888888888888888 ?APPID
        // ? partnerid String(32)  1900000109 ??
        // ?ID prepayid String(32)  WX1217752501201407033233368018
        // ?ID
        //  package String(128)  Sign=WXPay Sign=WXPay
        // ? noncestr String(32)  5K8264ILTKCH16CQ2502SI8ZNMTM67VS
        // ??32?????
        //  timestamp String(10)  1412000000 ??-?
        // ?? sign String(32)  C380BEC2BFD727A4B6845133519F3AD6 ??????

        Map<String, String> prePayMap = new HashMap<String, String>();
        prePayMap.put("appid", prePayRequest.get("appid").toString());// ?ID
        prePayMap.put("partnerid", prePayRequest.get("mch_id").toString()); // ?
        prePayMap.put("prepayid", prePayRequest.get("prepay_id").toString()); // ?ID
        prePayMap.put("noncestr", prePayRequest.get("nonce_str").toString()); // ?

        prePayMap.put("package", "Sign=WXPay"); // 
        prePayMap.put("timestamp", String.valueOf(System.currentTimeMillis()).substring(0, 10)); // 

        prePayMap.put("sign", getPrePaySignForApp(prePayMap, partnerKey)); // 
        return prePayMap;
    }

    public static String getPrePaySignForApp(Map<String, String> result, String partnerKey) {
        String argNotifySign = getStringByStringMap(result) + "&key=" + partnerKey;
        if (LOG.isInfoEnabled()) {
            LOG.info("argNotifySign=" + argNotifySign);
        }
        String notifySign = MD5Util.encode(argNotifySign).toUpperCase();
        if (LOG.isInfoEnabled()) {
            LOG.info("notifySign=" + notifySign);
        }
        return notifySign;
    }

    /**
     * ?Map???
     * @param map
     * @return
     */
    public static String getStringByMap(Map<String, Object> map) {
        SortedMap<String, Object> smap = new TreeMap<String, Object>(map);
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, Object> m : smap.entrySet()) {
            sb.append(m.getKey()).append("=").append(m.getValue()).append("&");
        }
        sb.delete(sb.length() - 1, sb.length());
        return sb.toString();
    }

    public static String getStringByStringMap(Map<String, String> map) {
        SortedMap<String, Object> smap = new TreeMap<String, Object>(map);
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, Object> m : smap.entrySet()) {
            sb.append(m.getKey()).append("=").append(m.getValue()).append("&");
        }
        sb.delete(sb.length() - 1, sb.length());
        return sb.toString();
    }

    /**
     * ???XML
     *
     * @param inputStream
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> parseXml(InputStream inputStream) throws Exception {

        if (inputStream == null) {
            return null;
        }

        Map<String, String> map = new HashMap<String, String>();// ?HashMap
        SAXReader reader = new SAXReader();// ??
        Document document = reader.read(inputStream);
        Element root = document.getRootElement();// xml
        List<Element> elementList = root.elements();// ?
        for (Element e : elementList) { // ???
            map.put(e.getName(), e.getText());
        }

        inputStream.close(); // ?
        inputStream = null;

        return map;
    }

}