1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| import org.springframework.util.StringUtils;
import java.util.*;
public class WxPayUtil {
public static String APP_ID = "(appid)"; public static String MCH_ID = "(mchid)"; public static String API_KEY = "(api key)"; public static String NOTIFY_URL = "(notify url)";
public static String WXPAY_UNIFIE = "https://api.mch.weixin.qq.com/pay/unifiedorder"; public static String WXPAY_QUERY = "https://api.mch.weixin.qq.com/pay/orderquery"; public static String WXPAY_CLOSE = "https://api.mch.weixin.qq.com/pay/closeorder"; public static String WXPAY_BILL = "https://api.mch.weixin.qq.com/pay/downloadbill";
private static TreeMap<String, String> initParam() { TreeMap<String, String> map = new TreeMap<>(); map.put("appid", APP_ID); map.put("mch_id", MCH_ID); map.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", "")); return map; }
private static String createSign(SortedMap<String, String> packageParams) { StringBuffer sb = new StringBuffer(); Set<Map.Entry<String, String>> es = packageParams.entrySet(); for (Map.Entry<String, String> entry : es) { String k = entry.getKey(); String v = entry.getValue(); if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) { sb.append(k + "=" + v + "&"); } } sb.append("key=" + API_KEY); return MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase(); }
public static String wxpayCreate(PayDto dto) { TreeMap<String, String> map = initParam(); map.put("body", dto.getOrderdes()); map.put("out_trade_no", dto.getOid()); map.put("total_fee", dto.getPrice() + ""); map.put("spbill_create_ip", "117.159.15.221"); map.put("notify_url", NOTIFY_URL); map.put("trade_type", "NATIVE"); map.put("sign", createSign(map)); String requestXml = XmlUtil.createXML(map); System.err.println("------->" + requestXml); String responseXml = HttpUtil.postXml(WXPAY_UNIFIE, requestXml); System.err.println("------->" + responseXml); if (!StringUtils.isEmpty(responseXml)) { Map<String, String> res = XmlUtil.parseXml(responseXml); if (res != null) { return res.get("code_url"); } } return null; }
public static String wxpayQuery(String oid) { TreeMap<String, String> map = initParam(); map.put("out_trade_no", oid); map.put("sign", createSign(map)); String requestXml = XmlUtil.createXML(map); String responseXml = HttpUtil.postXml(WXPAY_QUERY, requestXml); if (!StringUtils.isEmpty(responseXml)) { Map<String, String> res = XmlUtil.parseXml(responseXml); if (res != null) { return res.get("trade_state"); } } return null; }
public static String wxpayClose(String oid) { TreeMap<String, String> map = initParam(); map.put("out_trade_no", oid); map.put("sign", createSign(map)); String requestXml = XmlUtil.createXML(map); String responseXml = HttpUtil.postXml(WXPAY_CLOSE, requestXml); if (!StringUtils.isEmpty(responseXml)) { Map<String, String> res = XmlUtil.parseXml(responseXml); if (res != null) { return res.get("result_code"); } } return null; } }
|