com.jiusit.onePurchase.pay.Pay.java Source code

Java tutorial

Introduction

Here is the source code for com.jiusit.onePurchase.pay.Pay.java

Source

/**
 * ???(JAVA) SDK
 * (c) 2012-2013 ____? <wmails@126.cn>, MIT Licensed
 * http://www.jeasyuicn.com/wechat
 */
package com.jiusit.onePurchase.pay;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import org.apache.commons.codec.digest.DigestUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.PropKit;
import com.jiusit.common.utils.StringUtil;

/**
 * 
 * @author L.cm
 * email: 596392912@qq.com
 * site:  http://www.dreamlu.net
 *
 */
public class Pay {

    // ??
    private static final String DELIVERNOTIFY_URL = "https://api.weixin.qq.com/pay/delivernotify?access_token=";

    /**
     * ? paySign ??appid?timestamp?noncestr?package ? appkey
     *  signType ????Package?
     * @param params
     * @return
     * @throws UnsupportedEncodingException 
     */
    public static String getPackage(Map<String, String> params) throws UnsupportedEncodingException {
        String partnerKey = PropKit.get("partnerKey");
        String partnerId = PropKit.get("partnerId");
        String notifyUrl = PropKit.get("notify_url");
        // ?
        params.put("bank_type", "WX");
        params.put("attach", "yongle");
        params.put("partner", partnerId);
        params.put("notify_url", notifyUrl);
        params.put("input_charset", "UTF-8");
        return packageSign(params, partnerKey);
    }

    /**
     * ??
     * @param params
     * @param encode
     * @return
     * @throws UnsupportedEncodingException 
     */
    public static String createSign(Map<String, String> params, boolean encode)
            throws UnsupportedEncodingException {
        Set<String> keysSet = params.keySet();
        Object[] keys = keysSet.toArray();
        Arrays.sort(keys);
        StringBuffer temp = new StringBuffer();
        boolean first = true;
        for (Object key : keys) {
            if (first) {
                first = false;
            } else {
                temp.append("&");
            }
            temp.append(key).append("=");
            Object value = params.get(key);
            String valueString = "";
            if (null != value) {
                valueString = value.toString();
            }
            if (encode) {
                temp.append(URLEncoder.encode(valueString, "UTF-8"));
            } else {
                temp.append(valueString);
            }
        }
        return temp.toString();
    }

    /**
     * package, ??
     * @param params
     * @param paternerKey
     * @return
     * @throws UnsupportedEncodingException 
     */
    private static String packageSign(Map<String, String> params, String paternerKey)
            throws UnsupportedEncodingException {
        String string1 = createSign(params, false);
        String stringSignTemp = string1 + "&key=" + paternerKey;
        String signValue = DigestUtils.md5Hex(stringSignTemp).toUpperCase();
        String string2 = createSign(params, true);
        return string2 + "&sign=" + signValue;
    }

    /**
     * package, ??
     * @param params
     * @param paternerKey
     * @return
     * @throws UnsupportedEncodingException 
     */
    public static String setSign(Map<String, String> params, String paternerKey)
            throws UnsupportedEncodingException {
        String string1 = createSign(params, false);
        String stringSignTemp = string1 + "&key=" + paternerKey;
        String signValue = DigestUtils.md5Hex(stringSignTemp).toUpperCase();
        return signValue;
    }

    /**
     * package, ??
     * @param params
     * @param paternerKey
     * @return
     * @throws UnsupportedEncodingException 
     */
    public static String setPaySign(Map<String, String> params) throws UnsupportedEncodingException {
        String string1 = createSign(params, false);
        String signValue = DigestUtils.md5Hex(string1).toUpperCase();
        return signValue;
    }

    /**
     * package, ??
     * @param params
     * @param paternerKey
     * @return
     * @throws UnsupportedEncodingException 
     */
    public static String setJsapiSign(Map<String, String> params) throws UnsupportedEncodingException {
        String string1 = createSign(params, false);
        String signValue = DigestUtils.sha1Hex(string1);
        return signValue;
    }

    /**
     * ??
     * @param timestamp
     * @param noncestr
     * @param packages
     * @return
     * @throws UnsupportedEncodingException 
     */
    public static String paySign(String timestamp, String noncestr, String packages)
            throws UnsupportedEncodingException {
        Map<String, String> paras = new HashMap<String, String>();
        paras.put("appid", PropKit.get("appId"));
        paras.put("timestamp", timestamp);
        paras.put("noncestr", noncestr);
        paras.put("package", packages);
        paras.put("appkey", PropKit.get("paySignKey"));
        // appid?timestamp?noncestr?package ? appkey
        String string1 = createSign(paras, false);
        String paySign = DigestUtils.sha1Hex(string1);
        return paySign;
    }

    /**
     * ??
     * @param timestamp
     * @param noncestr
     * @param openid
     * @param issubscribe
     * @param appsignature
     * @return
     * @throws UnsupportedEncodingException 
     */
    public static boolean verifySign(long timestamp, String noncestr, String openid, int issubscribe,
            String appsignature) throws UnsupportedEncodingException {
        Map<String, String> paras = new HashMap<String, String>();
        paras.put("appid", PropKit.get("appId"));
        paras.put("appkey", PropKit.get("paySignKey"));
        paras.put("timestamp", String.valueOf(timestamp));
        paras.put("noncestr", noncestr);
        paras.put("openid", openid);
        paras.put("issubscribe", String.valueOf(issubscribe));
        // appid?appkey?productid?timestamp?noncestr?openid?issubscribe
        String string1 = createSign(paras, false);
        String paySign = DigestUtils.sha1Hex(string1);
        return paySign.equalsIgnoreCase(appsignature);
    }

    /**
     * ???
     * @param paras
     * @return
     * @throws UnsupportedEncodingException
     * 
     * @? appid?appkey?openid?transid?out_trade_no?deliver_timestamp?deliver_status?deliver_msg
     */
    private static String deliverSign(Map<String, String> paras) throws UnsupportedEncodingException {
        paras.put("appkey", PropKit.get("paySignKey"));
        String string1 = createSign(paras, false);
        String paySign = DigestUtils.sha1Hex(string1);
        return paySign;
    }

    public static String keyvalue2XML(String keyvalues) {
        String result = "<xml>";
        String keyvalue[] = keyvalues.split("&");
        for (int i = 0; i < keyvalue.length; i++) {
            String key = keyvalue[i].split("=")[0];
            String value = keyvalue[i].split("=")[1];
            result += "<" + key + ">" + value + "</" + key + ">";
        }
        result += "</xml>";
        return result;
    }

    public static Map<String, String> xml2Map(String xml) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            Document document = DocumentHelper.parseText(xml);
            Element root = document.getRootElement();
            for (Iterator it = root.elementIterator(); it.hasNext();) {
                Element element = (Element) it.next();
                map.put(element.getName(), element.getTextTrim());
            }
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }
}