Java tutorial
/** * ???(JAVA) SDK * (c) 2012-2013 ____? <wmails@126.cn>, MIT Licensed * http://www.jeasyuicn.com/wechat */ package com.lr.backer.controller; 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.Map; import java.util.Set; import java.util.concurrent.ExecutionException; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.StringUtils; import com.alibaba.fastjson.JSONObject; /** * * @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 = ConfKit.get("partnerKey"); String partnerId = ConfKit.get("partnerId"); String notifyUrl = ConfKit.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; } /** * ?? * @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", ConfKit.get("AppId")); paras.put("timestamp", timestamp); paras.put("noncestr", noncestr); paras.put("package", packages); paras.put("appkey", ConfKit.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", ConfKit.get("AppId")); paras.put("appkey", ConfKit.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", ConfKit.get("paySignKey")); String string1 = createSign(paras, false); String paySign = DigestUtils.sha1Hex(string1); return paySign; } }