处理退款回调
This commit is contained in:
@ -22,6 +22,26 @@ import java.io.StringWriter;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.*;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import java.io.StringReader;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import java.security.Security;
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
|
||||
public class WXPayUtil {
|
||||
|
||||
@ -164,4 +184,60 @@ public class WXPayUtil {
|
||||
Map<String, String> data = xmlToMap(xmlString);
|
||||
return isSignatureValid(data, key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将XML字符串转换为指定类型的实体对象
|
||||
*
|
||||
* @param xmlString XML字符串
|
||||
* @param clazz 实体类Class
|
||||
* @param <T> 实体类型
|
||||
* @return 实体对象
|
||||
* @throws Exception 转换异常
|
||||
*/
|
||||
public static <T> T xmlToEntity(String xmlString, Class<T> clazz) throws Exception {
|
||||
JAXBContext context = JAXBContext.newInstance(clazz);
|
||||
Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||
return clazz.cast(unmarshaller.unmarshal(new StringReader(xmlString)));
|
||||
}
|
||||
|
||||
|
||||
public static String decryptReqInfo(String reqInfo, String apiKey) throws Exception {
|
||||
try {
|
||||
// 步骤1: 对加密串A做base64解码,得到加密串B
|
||||
byte[] encryptedData = Base64.decodeBase64(reqInfo);
|
||||
|
||||
// 步骤2: 对商户key做md5,得到32位小写key*
|
||||
String key = DigestUtils.md5Hex(apiKey).toLowerCase();
|
||||
|
||||
// 步骤3: 用key*对加密串B做AES-256-ECB解密(PKCS7Padding)
|
||||
byte[] decryptedData = decrypt(encryptedData, key);
|
||||
|
||||
return new String(decryptedData, "UTF-8");
|
||||
} catch (Exception e) {
|
||||
logger.error("解密微信退款通知req_info失败", e);
|
||||
throw new Exception("解密失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AES解密方法
|
||||
*
|
||||
* @param encryptedData 加密数据
|
||||
* @param key 解密密钥
|
||||
* @return 解密后的字节数组
|
||||
* @throws Exception 解密异常
|
||||
*/
|
||||
private static byte[] decrypt(byte[] encryptedData, String key) throws Exception {
|
||||
try {
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
|
||||
return cipher.doFinal(encryptedData);
|
||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException |
|
||||
IllegalBlockSizeException | BadPaddingException e) {
|
||||
logger.error("AES解密失败", e);
|
||||
throw new Exception("AES解密失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user