|
|
|
@@ -3,9 +3,10 @@ package com.iformall.douyin.pay; |
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.nio.charset.Charset; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.TreeMap; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.security.MessageDigest; |
|
|
|
import java.security.NoSuchAlgorithmException; |
|
|
|
import java.util.*; |
|
|
|
import java.util.Map.Entry; |
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
@@ -27,6 +28,75 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
*/ |
|
|
|
@Slf4j |
|
|
|
public class DouYinPayHelper { |
|
|
|
|
|
|
|
private static final String SALT = "41dcZnKe0RovCfuVEL7IlXFfy206HPVtWJ5q7KJr"; |
|
|
|
|
|
|
|
public static String getSign(Map<String, Object> paramsMap) { |
|
|
|
List<String> paramsArr = new ArrayList<>(); |
|
|
|
for (Map.Entry<String, Object> entry : paramsMap.entrySet()) { |
|
|
|
String key = entry.getKey(); |
|
|
|
if (key.equals("other_settle_params")) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
String value = entry.getValue().toString(); |
|
|
|
|
|
|
|
value = value.trim(); |
|
|
|
if (value.startsWith("\"") && value.endsWith("\"") && value.length() > 1) { |
|
|
|
value = value.substring(1, value.length() - 1); |
|
|
|
} |
|
|
|
value = value.trim(); |
|
|
|
if (value.equals("") || value.equals("null")) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
switch (key) { |
|
|
|
case "app_id": |
|
|
|
case "thirdparty_id": |
|
|
|
case "sign": |
|
|
|
break; |
|
|
|
default: |
|
|
|
paramsArr.add(value); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
paramsArr.add(SALT); |
|
|
|
Collections.sort(paramsArr); |
|
|
|
StringBuilder signStr = new StringBuilder(); |
|
|
|
String sep = ""; |
|
|
|
for (String s : paramsArr) { |
|
|
|
signStr.append(sep).append(s); |
|
|
|
sep = "&"; |
|
|
|
} |
|
|
|
return md5FromStr(signStr.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
public static String md5FromStr(String inStr) { |
|
|
|
MessageDigest md5; |
|
|
|
try { |
|
|
|
md5 = MessageDigest.getInstance("MD5"); |
|
|
|
} catch (NoSuchAlgorithmException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return ""; |
|
|
|
} |
|
|
|
|
|
|
|
byte[] byteArray = inStr.getBytes(StandardCharsets.UTF_8); |
|
|
|
byte[] md5Bytes = md5.digest(byteArray); |
|
|
|
StringBuilder hexValue = new StringBuilder(); |
|
|
|
for (byte md5Byte : md5Bytes) { |
|
|
|
int val = ((int) md5Byte) & 0xff; |
|
|
|
if (val < 16) { |
|
|
|
hexValue.append("0"); |
|
|
|
} |
|
|
|
hexValue.append(Integer.toHexString(val)); |
|
|
|
} |
|
|
|
return hexValue.toString(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* urlEncode |
|
|
|
@@ -123,7 +193,8 @@ public class DouYinPayHelper { |
|
|
|
Map map = new HashMap(); |
|
|
|
map.put("app_id",appId); |
|
|
|
map.put("out_order_no",orderNo); |
|
|
|
map.put("sign",createSign(map, secrect)); |
|
|
|
// map.put("sign",createSign(map, secrect)); |
|
|
|
map.put("sign",getSign(map)); |
|
|
|
String response = doPost("https://developer.toutiao.com/api/apps/ecpay/v1/query_order", map); |
|
|
|
JSONObject jsonObject = JSON.parseObject(response); |
|
|
|
JSONObject info = jsonObject.getJSONObject("payment_info"); |
|
|
|
|