微信支付回调 java_Java微信支付-退款成功异步回调验签

该博客详细介绍了Java实现微信支付退款异步回调的验签过程。通过读取HTTP请求的XML数据,进行解密并验证签名,确保回调信息的安全。在解密成功后,执行相应的业务逻辑,并组装返回给微信的确认XML消息。
摘要由CSDN通过智能技术生成

/**

* 微信支付实现

*

* @author: Sun

* @create: 2019-10-10 17:54

* @version: v1.0

*/

@Service

@Slf4j

public class WxPayServiceImpl implements WxPayService {

@Autowired

private MyWxPayConfig myWxPayConfig;

@Autowired

private WxDecodeUtil wxDecodeUtil;

/**

* 微信响应字段

*/

private static final String RETURN_CODE = "return_code";

@Override

public String refundAsyncNotify(HttpServletRequest httpServletRequest) {

String returnXmlMessage = null;

String notifyXmlData = null;

try {

notifyXmlData = readXmlFromStream(httpServletRequest);

Map notifyMapData = WXPayUtil.xmlToMap(notifyXmlData);

log.info("[refundAsyncNotify] [xml转换为map数据成功] [notifyMapData:{}]", notifyMapData);

if (WXPayConstants.SUCCESS.equals(notifyMapData.get(RETURN_CODE))) {

// 获得加密信息

String reqInfo = notifyMapData.get("req_info");

// 进行AES解密 获取req_info中包含的相关信息(解密失败会抛出异常)

String refundDecryptedData = wxDecodeUtil.decryptData(reqInfo);

Map reqInfoMap = WXPayUtil.xmlToMap(refundDecryptedData);

log.info("[refundAsyncNotify] [reqInfo解密成功] [reqInfoMap:{}]", reqInfoMap);

// TODO 订单退款成功后相关业务逻辑...

// 组装返回给微信的xml数据

returnXmlMessage = setReturnXml(WXPayConstants.SUCCESS, "OK");

log.info("[refundAsyncNotify] [out_trade_no:{}] [out_refund_no:{}] [退款异步消息处理成功:{}]",

reqInfoMap.get("out_trade_no"), reqInfoMap.get("out_refund_no"), returnXmlMessage);

} else {

returnXmlMessage = setReturnXml(WXPayConstants.FAIL, "return_code不为success");

}

} catch (IOException e) {

log.error("[refundAsyncNotify] [读取微信服务器返回流中xml数据时发生异常:{}] ", ExceptionUtils.getStackTrace(e));

returnXmlMessage = setReturnXml(WXPayConstants.FAIL, "An exception occurred while reading the WeChat server returning xml data in the stream.");

} catch (Exception e) {

log.error("[refundAsyncNotify [处理异常]] [xml数据:{}] [异常:{}] ", notifyXmlData, ExceptionUtils.getStackTrace(e));

returnXmlMessage = setReturnXml(WXPayConstants.FAIL, "Refund successful, exception occurred during asynchronous notification processing.");

log.warn("[refundAsyncNotify] [退款异步消息处理失败:{}]", returnXmlMessage);

}

return returnXmlMessage;

}

/**

* 设置返回给微信服务器的xml信息

*

* @param returnCode

* @param returnMsg

* @return

*/

private String setReturnXml(String returnCode, String returnMsg) {

return "+ returnCode + "]]>+ returnMsg + "]]>";

}

/**

* 从流中读取微信返回的xml数据

*

* @param httpServletRequest

* @return

* @throws IOException

*/

private String readXmlFromStream(HttpServletRequest httpServletRequest) throws IOException {

InputStream inputStream = httpServletRequest.getInputStream();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

final StringBuffer sb = new StringBuffer();

String line = null;

try {

while ((line = bufferedReader.readLine()) != null) {

sb.append(line);

}

} finally {

bufferedReader.close();

inputStream.close();

}

return sb.toString();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值