Commit 59bacb0a463397fd26d9548d8d7db1f21e273bdb
1 parent
0ec7fd42a0
Exists in
master
小程序-支付接口修改
Showing 2 changed files with 84 additions and 4 deletions
talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/OrderController.java
View file @
59bacb0
| ... | ... | @@ -20,6 +20,7 @@ |
| 20 | 20 | import org.springframework.web.bind.annotation.RequestBody; |
| 21 | 21 | import org.springframework.web.bind.annotation.RequestMapping; |
| 22 | 22 | import org.springframework.web.bind.annotation.RestController; |
| 23 | +import sun.security.provider.MD5; | |
| 23 | 24 | |
| 24 | 25 | import javax.servlet.http.HttpServletRequest; |
| 25 | 26 | import javax.servlet.http.HttpServletResponse; |
| ... | ... | @@ -181,10 +182,20 @@ |
| 181 | 182 | String success = "SUCCESS"; |
| 182 | 183 | if (retMap != null && retMap.size() > 0 && success.equals(retMap.get("return_code")) && success.equals(retMap.get("result_code"))) |
| 183 | 184 | { |
| 184 | - data.put("payId",retMap.get("prepay_id")); | |
| 185 | - data.put("nonce_str",retMap.get("nonce_str")); | |
| 186 | - data.put("paySign",retMap.get("sign")); | |
| 187 | - data.put("timestamp",System.currentTimeMillis()/1000+""); | |
| 185 | + SortedMap<Object,Object> map=new TreeMap<>(); | |
| 186 | + String nonceStr=NumberUtil.getRandomString(16); | |
| 187 | + String timeStamp=System.currentTimeMillis()/1000+""; | |
| 188 | + map.put("appId",Constant.PAT_APP_ID); | |
| 189 | + map.put("nonceStr",nonceStr); | |
| 190 | + map.put("package","prepay_id="+retMap.get("prepay_id")); | |
| 191 | + map.put("signType","MD5"); | |
| 192 | + map.put("timeStamp",timeStamp); | |
| 193 | + System.out.println("createSign:"+createSign(null, map)); | |
| 194 | + | |
| 195 | + data.put("packageValue",retMap.get("prepay_id")); | |
| 196 | + data.put("nonceStr",nonceStr); | |
| 197 | + data.put("paySign",createSign(null, map)); | |
| 198 | + data.put("timeStamp",timeStamp); | |
| 188 | 199 | return data; |
| 189 | 200 | } |
| 190 | 201 | } |
| ... | ... | @@ -397,6 +408,34 @@ |
| 397 | 408 | String localSign = PayDigestUtil.getSign(map, Constant.REQ_KEY, "sign"); |
| 398 | 409 | String sign = map.get("sign"); |
| 399 | 410 | return localSign.equalsIgnoreCase(sign); |
| 411 | + } | |
| 412 | + | |
| 413 | + | |
| 414 | + /** | |
| 415 | + * 微信支付签名算法sign | |
| 416 | + * @param characterEncoding | |
| 417 | + * @param parameters | |
| 418 | + * @return | |
| 419 | + */ | |
| 420 | + public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){ | |
| 421 | + StringBuffer sb = new StringBuffer(); | |
| 422 | + Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序(升序) | |
| 423 | + Iterator it = es.iterator(); | |
| 424 | + while(it.hasNext()) { | |
| 425 | + Map.Entry entry = (Map.Entry)it.next(); | |
| 426 | + String k = (String)entry.getKey(); | |
| 427 | + Object v = entry.getValue(); | |
| 428 | + if(null != v && !"".equals(v) | |
| 429 | + && !"sign".equals(k) && !"key".equals(k)) { | |
| 430 | + sb.append(k + "=" + v + "&"); | |
| 431 | + } | |
| 432 | + } | |
| 433 | + | |
| 434 | + sb.append("key=" + Constant.REQ_KEY); | |
| 435 | + System.out.println("签名字符串:"+sb.toString()); | |
| 436 | + System.out.println("签名MD5未变大写:" + MD5Util.MD5Encode(sb.toString(), characterEncoding)); | |
| 437 | + String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase(); | |
| 438 | + return sign; | |
| 400 | 439 | } |
| 401 | 440 | |
| 402 | 441 |
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/MD5Util.java
View file @
59bacb0
| ... | ... | @@ -6,6 +6,9 @@ |
| 6 | 6 | /** |
| 7 | 7 | */ |
| 8 | 8 | public class MD5Util { |
| 9 | + private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", | |
| 10 | + "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" | |
| 11 | + }; | |
| 9 | 12 | |
| 10 | 13 | /** |
| 11 | 14 | * md5加密 |
| ... | ... | @@ -36,5 +39,43 @@ |
| 36 | 39 | return null; |
| 37 | 40 | } |
| 38 | 41 | } |
| 42 | + | |
| 43 | + /** | |
| 44 | + * md5加密并转为大写 | |
| 45 | + * @param origin 拼接的字符串 | |
| 46 | + * @return | |
| 47 | + */ | |
| 48 | + public static String MD5Encode(String origin, String charsetname) { | |
| 49 | + String resultString = null; | |
| 50 | + try { | |
| 51 | + resultString = new String(origin); | |
| 52 | + MessageDigest md = MessageDigest.getInstance("MD5"); | |
| 53 | + if (charsetname == null || "".equals(charsetname)) | |
| 54 | + resultString = byteArrayToHexString(md.digest(resultString | |
| 55 | + .getBytes())); | |
| 56 | + else | |
| 57 | + resultString = byteArrayToHexString(md.digest(resultString | |
| 58 | + .getBytes(charsetname))); | |
| 59 | + } catch (Exception exception) { | |
| 60 | + } | |
| 61 | + return resultString; | |
| 62 | + } | |
| 63 | + private static String byteArrayToHexString(byte b[]) { | |
| 64 | + StringBuffer resultSb = new StringBuffer(); | |
| 65 | + for (int i = 0; i < b.length; i++) | |
| 66 | + resultSb.append(byteToHexString(b[i])); | |
| 67 | + | |
| 68 | + return resultSb.toString(); | |
| 69 | + } | |
| 70 | + | |
| 71 | + private static String byteToHexString(byte b) { | |
| 72 | + int n = b; | |
| 73 | + if (n < 0) | |
| 74 | + n += 256; | |
| 75 | + int d1 = n / 16; | |
| 76 | + int d2 = n % 16; | |
| 77 | + return hexDigits[d1] + hexDigits[d2]; | |
| 78 | + } | |
| 79 | + | |
| 39 | 80 | } |