java 支付宝即时到帐提交订单dome
package com.tian.batis; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.codec.digest.DigestUtils; import javax.servlet.http.HttpServletResponse; public class alipay { /** * alipay 支付接口 * * @param _input_charset * 编码 * @param subject * 商品名 (必填) * @param notify_url * 异步执行地址 * @param body * 商品描述 * @param payment_type * 付款类型 * @param out_trade_no * 订单号 (必填) * @param partner * 商家id * @param service * 服务 * @param total_fee * 金额 (必填) * @param return_url * 返回展示地址 * @param sign_type * 加密方式(默认md5) * @param seller_id * 同商家id * @param response * @param sessionId * sessionId * @return 成功 返回url;-1失败(参数);-2处理异常 */ public String AlipayPayment(String _input_charset, String subject, String notify_url, String body, String payment_type, String out_trade_no, String partner, String service, Integer total_fee, String return_url, String sign_type, String seller_id, HttpServletResponse response, String sessionId) { try { String strSandbox = "https://mapi.alipay.com/gateway.do";// 地址 ConfigManager configManager = ConfigManager.getInstance("alipayconfig.properties"); Map<String, String> map = new HashMap<String, String>(); if (null != subject && subject.trim().length() > 0) { map.put("subject", subject); } else { return "-1"; } if (null != out_trade_no && out_trade_no.trim().length() > 0) { map.put("out_trade_no", out_trade_no); } else { return "-1"; } if (null != total_fee && total_fee > 0) { map.put("total_fee", total_fee + ""); // map.put("total_fee", "0.01"); } else { return "-1"; } if (null != _input_charset && _input_charset.trim().length() > 0) { map.put("_input_charset", _input_charset); } else { map.put("_input_charset", configManager.getConfigItem("alipay._input_charset").toString()); } if (null != notify_url && notify_url.trim().length() > 0) { map.put("notify_url", notify_url); } else { map.put("notify_url", configManager.getConfigItem("alipay.notify_url1").toString()); } if (null != return_url && return_url.trim().length() > 0) { map.put("return_url", return_url); } else { map.put("return_url", configManager.getConfigItem("alipay.return_url").toString()); } if (null != payment_type && payment_type.trim().length() > 0) { map.put("payment_type", payment_type); } else { map.put("payment_type", configManager.getConfigItem("alipay.payment_type").toString()); } if (null != partner && partner.trim().length() > 0) { map.put("partner", partner); } else { map.put("partner", configManager.getConfigItem("alipay.partner").toString()); } if (null != service && service.trim().length() > 0) { map.put("service", service); } else { map.put("service", configManager.getConfigItem("alipay.service").toString()); } if (null != sign_type && sign_type.trim().length() > 0) { map.put("sign_type", sign_type); } else { map.put("sign_type", configManager.getConfigItem("alipay.sign_type").toString()); } if (null != seller_id && seller_id.trim().length() > 0) { map.put("seller_id", seller_id); } else { map.put("seller_id", configManager.getConfigItem("alipay.seller_id").toString()); } if (null != body && body.trim().length() > 0) { map.put("body", body); } if (null != sessionId && sessionId.trim().length() > 0) { map.put("extra_common_param", sessionId); } Map<String, String> sPara = paraFilter(map); // 生成签名结果 String sign = buildRequestMysign(sPara, configManager.getConfigItem("alipay.key").toString(), configManager.getConfigItem("alipay._input_charset").toString()); map.put("sign", sign); String str = ""; for (Map.Entry<String, String> entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key.equals("subject") || key.equals("body")) { value = URLEncoder.encode(value, "UTF-8"); } if (null != str && str.trim().length() > 0) { str += "&" + key + "=" + value; } else { str += key + "=" + value; } } String url = strSandbox; if (null != str && str.trim().length() > 0) { url += "?" + str; } return url; // 注:如果失败考虑 // 1.response 重定向编码 // 2.sign_type 放置参数末尾 // 3.url 追加 _input_charset=utf-8 } catch (Exception e) { e.printStackTrace(); return "-2"; } } /////////////////////////////////////////////////////// /** * 除去数组中的空值和签名参数 * * @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 (value == null || value.equals("") || key.equalsIgnoreCase("sign") || key.equalsIgnoreCase("sign_type")) { continue; } result.put(key, value); } return result; } /** * 生成签名结果 * * @param sPara * 要签名的数组 * @return 签名结果字符串 */ public static String buildRequestMysign(Map<String, String> sPara, String key, String input_charset) { String prestr = createLinkString(sPara); // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 String mysign = ""; mysign = sign(prestr, key, input_charset); return mysign; } /** * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串 * * @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; } /** * 签名字符串 * * @param text * 需要签名的字符串 * @param key * 密钥 * @param input_charset * 编码格式 * @return 签名结果 */ public static String sign(String text, String key, String input_charset) { text = text + key; return DigestUtils.md5Hex(getContentBytes(text, input_charset)); } /** * @param content * @param charset * @return * @throws SignatureException * @throws UnsupportedEncodingException */ private static byte[] getContentBytes(String content, String charset) { if (charset == null || "".equals(charset)) { return content.getBytes(); } try { return content.getBytes(charset); } catch (UnsupportedEncodingException e) { throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset); } } }