快捷搜索: 王者荣耀 脱发

java进行微信h5支付开发

下面是开发流程图

我们只需要开发红色标记的模块就可以了。

新手第一次写,写的不好。

1.下面是下单接口

比较麻烦是签名的获取:以下是代码(摘自网络)

/** * 获取支付所需签名 * * @param bizObj * @param key * @return * @throws Exception */ private String getPayCustomSign(Map map, String key) throws Exception { String bizString = FormatBizQueryParaMap(map, false); return sign(bizString, key); } /** * 进行字典排序 * * @param paraMap * @param urlencode * @return * @throws Exception */ private String FormatBizQueryParaMap(Map paraMap, boolean urlencode) throws Exception { String sf = ""; try { List<Entry> infoIds = new ArrayList<Entry>(paraMap.entrySet()); Collections.sort(infoIds, new Comparator<Entry>() { public int compare(Map.Entry o1, Map.Entry o2) { return (o1.getKey().toString().compareTo(o2.getKey().toString())); } }); for (int i = 0; i < infoIds.size(); i++) { Map.Entry entryItem = infoIds.get(i); if (entryItem.getKey() != "") { String key = (String) entryItem.getKey(); String value = (String) entryItem.getValue(); if (urlencode) { value = URLEncoder.encode(value, "utf-8"); } sf += key + "=" + value + "&"; } } if (sf.isEmpty() == false) { sf = sf.substring(0, sf.length() - 1); } } catch (Exception e) { throw new Exception(e.getMessage()); } return sf; } /** * 生成 签名 * * @param content * @param key * @return */ private String sign(String content, String key) { String signStr = content + "&key=" + key; return MD5(signStr).toUpperCase(); } /** * MD5 加密 * * @param str * @return */ private final String MD5(String str) { char hexDigits[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F }; try { byte[] btInput = str.getBytes(); MessageDigest mdInst = MessageDigest.getInstance("MD5"); mdInst.update(btInput); byte[] md = mdInst.digest(); int j = md.length; char str2[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str2[k++] = hexDigits[byte0 >>> 4 & 0xf]; str2[k++] = hexDigits[byte0 & 0xf]; } return new String(str2); } catch (Exception e) { e.printStackTrace(); } return null; }

生成随机数

/** * 随机生成 32 位的字符串 * * @return */ private String getRandomString() { Random random = new Random(); StringBuffer nonceStrSf = new StringBuffer(); for (int i = 0; i < 32; i++) { nonceStrSf.append(nonce_str.charAt(random.nextInt(nonce_str.length() - 1))); } return nonceStrSf.toString(); }

map转xml

/** * map 转 xml * * @param map * @return */ private String mapToXml(Map<String, String> map) { StringBuffer sf = new StringBuffer("<xml>"); Iterator<Entry<String, String>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, String> entry = iterator.next(); String key = entry.getKey(); String value = entry.getValue(); sf.append("<" + key + ">" + value + "</" + key + ">"); } sf.append("</xml>"); return sf.toString(); }

第一次写这么长的博客,不足之处请留言告之便于本人成长,再次感谢你的阅读。

经验分享 程序员 微信小程序 职场和发展