微信最新版小程序支付支付回调的java代码
时间: 2023-08-31 13:13:35 浏览: 101
微信小程序支付,java封装,拿来即用
5星 · 资源好评率100%
以下是微信最新版小程序支付的支付回调Java代码示例:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WxPayNotifyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String SUCCESS = "SUCCESS";
private static final String FAIL = "FAIL";
private static final String SIGN_TYPE_MD5 = "MD5";
private static final String SIGN_TYPE_HMAC_SHA256 = "HMAC-SHA256";
private static final String APP_ID = "your_app_id";
private static final String MCH_ID = "your_mch_id";
private static final String API_KEY = "your_api_key";
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
String notifyXml = buffer.toString();
Map<String, String> notifyMap = parseXmlToMap(notifyXml);
String returnCode = notifyMap.get("return_code");
if (SUCCESS.equals(returnCode)) {
String appId = notifyMap.get("appid");
String mchId = notifyMap.get("mch_id");
String nonceStr = notifyMap.get("nonce_str");
String sign = notifyMap.get("sign");
String signType = notifyMap.get("sign_type");
String resultCode = notifyMap.get("result_code");
String transactionId = notifyMap.get("transaction_id");
String outTradeNo = notifyMap.get("out_trade_no");
String totalFee = notifyMap.get("total_fee");
String attach = notifyMap.get("attach");
if (APP_ID.equals(appId) && MCH_ID.equals(mchId)) {
if (validateSign(notifyMap, API_KEY, signType, sign)) {
// 处理支付成功逻辑
// 更新订单状态等操作
response.getWriter().write(SUCCESS);
return;
}
}
}
response.getWriter().write(FAIL);
}
private Map<String, String> parseXmlToMap(String xml) {
Map<String, String> map = new HashMap<String, String>();
String[] strings = xml.split("<|>");
for (int i = 2; i < strings.length; i += 4) {
map.put(strings[i], strings[i + 2]);
}
return map;
}
private boolean validateSign(Map<String, String> map, String apiKey, String signType, String sign) {
String signStr = generateSign(map, apiKey, signType);
return signStr.equals(sign);
}
private String generateSign(Map<String, String> map, String apiKey, String signType) {
StringBuffer buffer = new StringBuffer();
for (String key : map.keySet()) {
if (!"sign".equals(key) && !"key".equals(key) && map.get(key) != null && !"".equals(map.get(key))) {
buffer.append(key).append("=").append(map.get(key)).append("&");
}
}
buffer.append("key=").append(apiKey);
String signStr = "";
if (SIGN_TYPE_MD5.equals(signType)) {
signStr = md5(buffer.toString()).toUpperCase();
} else if (SIGN_TYPE_HMAC_SHA256.equals(signType)) {
signStr = hmacSha256(buffer.toString(), apiKey).toUpperCase();
}
return signStr;
}
private String md5(String str) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "";
}
byte[] bytes = md5.digest(str.getBytes());
StringBuffer stringBuffer = new StringBuffer();
for (byte b : bytes) {
int bt = b & 0xff;
if (bt < 16) {
stringBuffer.append(0);
}
stringBuffer.append(Integer.toHexString(bt));
}
return stringBuffer.toString();
}
private String hmacSha256(String str, String key) {
try {
Mac sha256Hmac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256Hmac.init(secretKey);
byte[] bytes = sha256Hmac.doFinal(str.getBytes("UTF-8"));
StringBuffer stringBuffer = new StringBuffer();
for (byte b : bytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
stringBuffer.append('0');
}
stringBuffer.append(hex);
}
return stringBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}
```
请注意,上面的代码示例仅供参考,具体实现需要根据自己的业务需求进行调整。
阅读全文