关于支付,一直想参与开发,现在根据项目中已有及参见的微信开发文档,将自己对于微信开发的流程进行简单的总结,以备后用和帮助后来者。
一、相关官方文档
微信支付官方文档:https://pay.weixin.qq.com/wiki/doc/api/index.html
二、参考博文
(待添加,等我找找..)
三、自己参看文档时候的简单文档整理,
ps:只为了方便自己记忆和联想
四、根据官方文档中,标记后台主要做的工作流程
五、参看项目代码时候,整理的demo,来源于网络
(1)MD5Util ---- 生成签名时候使用
package com.weixin.test; import java.security.MessageDigest; /** * 微信测试MD5 * @author Administrator * */ public class MD5Util { private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) resultSb.append(byteToHexString(b[i])); return resultSb.toString(); } private static String byteToHexString(byte b) { int n = b; if (n < 0) n += 256; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } public static String MD5Encode(String origin, String charsetname) { String resultString = null; try { resultString = new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); if (charsetname == null || "".equals(charsetname)) resultString = byteArrayToHexString(md.digest(resultString.getBytes())); else resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname))); } catch (Exception exception) { } return resultString; } private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; }
(2)PayTest
package com.weixin.test; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; public class PayTest { // http://mch.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3 private static String Key = "192006250b4c09247ec02edce69f6a2d"; /** * @param args */ public static void main(String[] args) { System.out.println(">>>模拟微信支付<<<"); System.out.println("==========华丽的分隔符=========="); // 微信api提供的参数 String appid = "wxd930ea5d5a258f4f"; String mch_id = "10000100"; String device_info = "1000"; String body = "test"; String nonce_str = "ibuaiVcKdpRxkhJA"; SortedMap<Object, Object> parameters = new TreeMap<Object, Object>(); parameters.put("appid", appid); parameters.put("mch_id", mch_id); parameters.put("device_info", device_info); parameters.put("body", body); parameters.put("nonce_str", nonce_str); String characterEncoding = "UTF-8"; String weixinApiSign = "9A0A8659F005D6984697E2CA0A9CF3B7"; System.out.println("微信的签名是:" + weixinApiSign); String mySign = createSign(characterEncoding, parameters); System.out.println("我 的签名是:" + mySign); if (weixinApiSign.equals(mySign)) { System.out.println("恭喜你成功了~"); } else { System.out.println("不行啊,再接再厉~"); } String userAgent = "Mozilla/5.0(iphone;CPU iphone OS 5_1_1 like Mac OS X) AppleWebKit/534.46(KHTML,like Geocko) Mobile/9B206 MicroMessenger/5.0"; char agent = userAgent.charAt(userAgent.indexOf("MicroMessenger") + 15); System.out.println("微信的版本号:" + new String(new char[] { agent })); } /** * 微信支付签名算法sign * @param characterEncoding * @param parameters * @return */ @SuppressWarnings("unchecked") public static String createSign(String characterEncoding, SortedMap<Object, Object> parameters) { StringBuffer sb = new StringBuffer(); Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序(升序) Iterator it = es.iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); String k = (String) entry.getKey(); Object v = entry.getValue(); if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) { sb.append(k + "=" + v + "&"); } } sb.append("key=" + Key); String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase(); return sign; } }
六、项目中相关代码及使用的util情况
(待续....)
时间: 2024-10-20 09:33:08