1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
@PostMapping("/pay/wap/{merchantId}") public CallbackRespDTO orderWapPayResult(HttpServletRequest request, HttpServletResponse response, @PathVariable("merchantId") Long merchantId) {
Transaction transaction = null; try { transaction = callbackCheckSign(request, Transaction.class, merchantId); } catch (Exception e) { log.error("微信支付回调处理异常,异常原因:{}", e.getMessage()); e.printStackTrace(); return CallbackRespDTO.fail(response); }
if (ObjectUtil.isEmpty(transaction)) { log.error("微信支付回调参数解析失败!"); return CallbackRespDTO.fail(response); }
xxxx.xxxxxxx(transaction)
return CallbackRespDTO.success(); }
private <T> T callbackCheckSign(HttpServletRequest request, Class<T> clazz, Long merchantId) { String nonceStr = request.getHeader("Wechatpay-Nonce");
String signature = request.getHeader("Wechatpay-Signature");
String serialNo = request.getHeader("Wechatpay-Serial");
String timestamp = request.getHeader("Wechatpay-Timestamp");
String signatureType = request.getHeader("Wechatpay-Signature-Type");
String body = getRequestBody(request); log.info("微信支付回调验签nonce:{},signature:{},serialNo:{},timestamp:{},body:{}", nonceStr, signature, serialNo, timestamp, body);
BesaPayReqDTO besaPayReqDTO = redisService.getCacheObject(RedisWechatPayKeyConstant.WECHAT_PAY_CONFIG_CACHE + merchantId);
RequestParam requestParam = new RequestParam.Builder() .serialNumber(serialNo) .nonce(nonceStr) .signature(signature) .timestamp(timestamp) .signType(signatureType) .body(body) .build();
NotificationConfig config = wechatPayService.getConfig(besaPayReqDTO); NotificationParser parser = new NotificationParser(config);
return parser.parse(requestParam, clazz); }
private String getRequestBody(HttpServletRequest request) {
try { ByteArrayOutputStream result = new ByteArrayOutputStream(); ServletInputStream inputStream = request.getInputStream(); byte[] buffer = new byte[1024]; for (int length; (length = inputStream.read(buffer)) != -1; ) { result.write(buffer, 0, length); } return result.toString("UTF-8"); } catch (Exception e) { log.error("读取数据流异常:{}", e); }
return null;
}
|