Java tutorial
/* * Project: guahao-portal-biz-core * * File Created at 2012-5-16 * * Copyright 2012 Greenline.com Corporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * Greenline Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Greenline.com. */ package com.greenline.guahao.biz.manager.payment.util; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.dom4j.Document; import org.dom4j.DocumentHelper; import com.greenline.guahao.biz.manager.payment.config.AlipayConstant; import com.greenline.guahao.biz.manager.payment.config.MobileAlipayConstant; /* * *??AlipayFunction *?? *???? *3.2 *2011-03-17 * *????????,??? *??????? */ public class AlipayCore { private static final Log log = LogFactory.getLog(AlipayCore.class); /** * ???? */ private static final String HTTPS_VERIFY_URL = "https://mapi.alipay.com/gateway.do?service=notify_verify&"; /** * ??? * * @param sArray ??? * @return ?? */ public static String buildMysign(Map<String, String> sArray) { sArray = paraFilter(sArray); String prestr = createLinkString(sArray); // ?=???&?? prestr = prestr + AlipayConstant.key; // ???? String mysign = AlipayMd5Encrypt.md5(prestr, AlipayConstant._input_charset); return mysign; } /** * html5??? * * @param sArray ??? * @return ?? */ public static String html5BuildMysign(Map<String, String> sArray) { sArray = paraFilter(sArray); String prestr = createLinkString(sArray); // ?=???&?? prestr = prestr + MobileAlipayConstant.key; // ???? String mysign = AlipayMd5Encrypt.md5(prestr, MobileAlipayConstant.input_charset); return mysign; } public static String html5BuildMysignForMk(Map<String, String> sArray) { sArray = paraFilter(sArray); String prestr = createLinkString(sArray); // ?=???&?? prestr = prestr + AlipayConstant.xyyy_key; // ???? String mysign = AlipayMd5Encrypt.md5(prestr, MobileAlipayConstant.input_charset); return mysign; } /** * ??? * * @param sArray ??? * @return ??????? */ public static Map<String, String> paraFilter(Map<String, String> sArray) { Map<String, String> result = new HashMap<String, String>(); if (sArray == null || sArray.size() <= 0) { return result; } for (String key : sArray.keySet()) { String value = sArray.get(key); if (StringUtils.isBlank(value) || "sign".equalsIgnoreCase(key) || "sign_type".equalsIgnoreCase(key)) { continue; } result.put(key, value); } return result; } /** * ??=???&?? * * @param params ???? * @return ? */ public static String createLinkString(Map<String, String> params) { List<String> keys = new ArrayList<String>(params.keySet()); Collections.sort(keys); String prestr = ""; for (int i = 0; i < keys.size(); i++) { String key = keys.get(i); String value = params.get(key); if (i == keys.size() - 1) {// ??& prestr = prestr + key + "=" + value; } else { prestr = prestr + key + "=" + value + "&"; } } return prestr; } /** * ?????token * * @param text ?? * @return ? * @throws Exception */ public static String getRequestToken(String text) throws Exception { String request_token = ""; // &? String[] strSplitText = text.split("&"); // ?????? Map<String, String> paraText = new HashMap<String, String>(); for (int i = 0; i < strSplitText.length; i++) { // =? int nPos = strSplitText[i].indexOf("="); // int nLen = strSplitText[i].length(); // ???? String strKey = strSplitText[i].substring(0, nPos); // String strValue = strSplitText[i].substring(nPos + 1, nLen); // MAP paraText.put(strKey, strValue); } if (paraText.get("res_data") != null) { String res_data = paraText.get("res_data"); // tokenres_data??res_data??token Document document = DocumentHelper.parseText(res_data); request_token = document.selectSingleNode("//direct_trade_create_res/request_token").getText(); } return request_token; } /** * html5???sign ? * * @param notfiyId * @return */ public static boolean verify(Map<String, String> params) { String sign = ""; if (params.get("sign") != null) { sign = params.get("sign"); log.info("??sign" + sign); } boolean isSign = getSignVeryfy(params, sign, true); return isSign; } /** * ???????? * * @param Params ?? * @param sign ?? * @return ??? */ private static boolean getSignVeryfy(Map<String, String> Params, String sign, boolean isSort) { // ?signsign_type? Map<String, String> sParaNew = AlipayCore.paraFilter(Params); // ??? String preSignStr = ""; if (isSort) { preSignStr = createLinkString(sParaNew); } else { preSignStr = createLinkStringNoSort(sParaNew); } boolean isSign = false; String signStr = preSignStr + MobileAlipayConstant.key; // ???? // ??? String mysign = AlipayMd5Encrypt.md5(signStr, MobileAlipayConstant.input_charset); log.info("???mysign" + mysign); if (sign.equals(mysign)) { isSign = true; } else { // ???? signStr = preSignStr + AlipayConstant.xyyy_key; // mk mysign = AlipayMd5Encrypt.md5(signStr, MobileAlipayConstant.input_charset); if (sign.equals(mysign)) { isSign = true; } } return isSign; } /** * ???=???&?? * * @param params ??? * @return ? */ public static String createLinkStringNoSort(Map<String, String> params) { Map<String, String> sParaSort = new HashMap<String, String>(); sParaSort.put("service", params.get("service")); sParaSort.put("v", params.get("v")); sParaSort.put("sec_id", params.get("sec_id")); sParaSort.put("notify_data", params.get("notify_data")); String prestr = ""; for (String key : sParaSort.keySet()) { prestr = prestr + key + "=" + sParaSort.get(key) + "&"; } prestr = prestr.substring(0, prestr.length() - 1); return prestr; } /** * ????????? * * @param params ?? * @return ? */ public static boolean verifyNotify(Map<String, String> params) throws Exception { // ??????? String responseTxt = "true"; try { // XML?notify_data??notify_id Document document = DocumentHelper.parseText(params.get("notify_data")); String notify_id = document.selectSingleNode("//notify/notify_id").getText(); responseTxt = verifyResponse(notify_id); } catch (Exception e) { responseTxt = e.toString(); } // ???? String sign = ""; if (params.get("sign") != null) { sign = params.get("sign"); } boolean isSign = getSignVeryfy(params, sign, false); if (isSign && "true".equals(responseTxt)) { return true; } else { return false; } } /** * ??ATN,?URL * * @param notify_id ID * @return ?ATN ? invalid?? ?partnerkey? true * ? false ?????? */ private static String verifyResponse(String notify_id) { // ??ATN?????? String partner = MobileAlipayConstant.partner; String veryfy_url = HTTPS_VERIFY_URL + "partner=" + partner + "¬ify_id=" + notify_id; return checkUrl(veryfy_url); } /** * ??ATN * * @param urlvalue URL? * @return ?ATN ? invalid?? ?partnerkey? true * ? false ?????? */ private static String checkUrl(String urlvalue) { String inputLine = ""; try { URL url = new URL(urlvalue); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); inputLine = in.readLine(); } catch (Exception e) { log.error(e); inputLine = ""; } return inputLine; } }