|
| 1 | +package com.wechat.pay.contrib.apache.httpclient.notification; |
| 2 | + |
| 3 | + |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 6 | +import com.wechat.pay.contrib.apache.httpclient.auth.Verifier; |
| 7 | +import com.wechat.pay.contrib.apache.httpclient.exception.ParseException; |
| 8 | +import com.wechat.pay.contrib.apache.httpclient.exception.ValidationException; |
| 9 | +import com.wechat.pay.contrib.apache.httpclient.notification.Notification.Resource; |
| 10 | +import com.wechat.pay.contrib.apache.httpclient.util.AesUtil; |
| 11 | +import java.io.IOException; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.security.GeneralSecurityException; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author lianup |
| 17 | + */ |
| 18 | +public class NotificationHandler { |
| 19 | + |
| 20 | + private final Verifier verifier; |
| 21 | + private final byte[] apiV3Key; |
| 22 | + private static final ObjectMapper objectMapper = new ObjectMapper(); |
| 23 | + |
| 24 | +public NotificationHandler(Verifier verifier, byte[] apiV3Key) { |
| 25 | + if (verifier == null) { |
| 26 | + throw new IllegalArgumentException("verifier为空"); |
| 27 | + } |
| 28 | + if (apiV3Key == null || apiV3Key.length == 0) { |
| 29 | + throw new IllegalArgumentException("apiV3Key为空"); |
| 30 | + } |
| 31 | + this.verifier = verifier; |
| 32 | + this.apiV3Key = apiV3Key; |
| 33 | +} |
| 34 | + |
| 35 | + /** |
| 36 | + * 解析微信支付通知请求结果 |
| 37 | + * |
| 38 | + * @param request 微信支付通知请求 |
| 39 | + * @return 微信支付通知报文解密结果 |
| 40 | + * @throws ValidationException 1.输入参数不合法 2.参数被篡改导致验签失败 3.请求和验证的平台证书不一致导致验签失败 |
| 41 | + * @throws ParseException 1.解析请求体为Json失败 2.请求体无对应参数 3.AES解密失败 |
| 42 | + */ |
| 43 | + public Notification parse(Request request) |
| 44 | + throws ValidationException, ParseException { |
| 45 | + // 验签 |
| 46 | + validate(request); |
| 47 | + // 解析请求体 |
| 48 | + return parseBody(request.getBody()); |
| 49 | + } |
| 50 | + |
| 51 | + private void validate(Request request) throws ValidationException { |
| 52 | + if (request == null) { |
| 53 | + throw new ValidationException("request为空"); |
| 54 | + } |
| 55 | + String serialNumber = request.getSerialNumber(); |
| 56 | + byte[] message = request.getMessage(); |
| 57 | + String signature = request.getSignature(); |
| 58 | + if (serialNumber == null || serialNumber.isEmpty()) { |
| 59 | + throw new ValidationException("serialNumber为空"); |
| 60 | + } |
| 61 | + if (message == null || message.length == 0) { |
| 62 | + throw new ValidationException("message为空"); |
| 63 | + } |
| 64 | + if (signature == null || signature.isEmpty()) { |
| 65 | + throw new ValidationException("signature为空"); |
| 66 | + } |
| 67 | + if (!verifier.verify(serialNumber, message, signature)) { |
| 68 | + String errorMessage = String |
| 69 | + .format("验签失败:serial=[%s] message=[%s] sign=[%s]", serialNumber, new String(message), signature); |
| 70 | + throw new ValidationException(errorMessage); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * 解析请求体 |
| 76 | + * |
| 77 | + * @param body 请求体 |
| 78 | + * @return 解析结果 |
| 79 | + * @throws ParseException 解析body失败 |
| 80 | + */ |
| 81 | + private Notification parseBody(String body) throws ParseException { |
| 82 | + ObjectReader objectReader = objectMapper.reader(); |
| 83 | + Notification notification; |
| 84 | + try { |
| 85 | + notification = objectReader.readValue(body, Notification.class); |
| 86 | + } catch (IOException ioException) { |
| 87 | + throw new ParseException("解析body失败,body:" + body, ioException); |
| 88 | + } |
| 89 | + validateNotification(notification); |
| 90 | + setDecryptData(notification); |
| 91 | + return notification; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * 校验解析后的通知结果 |
| 96 | + * |
| 97 | + * @param notification 通知结果 |
| 98 | + * @throws ParseException 参数不合法 |
| 99 | + */ |
| 100 | + private void validateNotification(Notification notification) throws ParseException { |
| 101 | + if (notification == null) { |
| 102 | + throw new ParseException("body解析为空"); |
| 103 | + } |
| 104 | + String id = notification.getId(); |
| 105 | + if (id == null || id.isEmpty()) { |
| 106 | + throw new ParseException("body不合法,id为空。body:" + notification.toString()); |
| 107 | + } |
| 108 | + String createTime = notification.getCreateTime(); |
| 109 | + if (createTime == null || createTime.isEmpty()) { |
| 110 | + throw new ParseException("body不合法,createTime为空。body:" + notification.toString()); |
| 111 | + } |
| 112 | + String eventType = notification.getEventType(); |
| 113 | + if (eventType == null || eventType.isEmpty()) { |
| 114 | + throw new ParseException("body不合法,eventType为空。body:" + notification.toString()); |
| 115 | + } |
| 116 | + String summary = notification.getSummary(); |
| 117 | + if (summary == null || summary.isEmpty()) { |
| 118 | + throw new ParseException("body不合法,summary为空。body:" + notification.toString()); |
| 119 | + } |
| 120 | + String resourceType = notification.getResourceType(); |
| 121 | + if (resourceType == null || resourceType.isEmpty()) { |
| 122 | + throw new ParseException("body不合法,resourceType为空。body:" + notification.toString()); |
| 123 | + } |
| 124 | + Resource resource = notification.getResource(); |
| 125 | + if (resource == null) { |
| 126 | + throw new ParseException("body不合法,resource为空。notification:" + notification.toString()); |
| 127 | + } |
| 128 | + String algorithm = resource.getAlgorithm(); |
| 129 | + if (algorithm == null || algorithm.isEmpty()) { |
| 130 | + throw new ParseException("body不合法,algorithm为空。body:" + notification.toString()); |
| 131 | + } |
| 132 | + String originalType = resource.getOriginalType(); |
| 133 | + if (originalType == null || originalType.isEmpty()) { |
| 134 | + throw new ParseException("body不合法,original_type为空。body:" + notification.toString()); |
| 135 | + } |
| 136 | + String ciphertext = resource.getCiphertext(); |
| 137 | + if (ciphertext == null || ciphertext.isEmpty()) { |
| 138 | + throw new ParseException("body不合法,ciphertext为空。body:" + notification.toString()); |
| 139 | + } |
| 140 | + String nonce = resource.getNonce(); |
| 141 | + if (nonce == null || nonce.isEmpty()) { |
| 142 | + throw new ParseException("body不合法,nonce为空。body:" + notification.toString()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * 获取解密数据 |
| 148 | + * |
| 149 | + * @param notification 解析body得到的通知结果 |
| 150 | + * @throws ParseException 解析body失败 |
| 151 | + */ |
| 152 | + private void setDecryptData(Notification notification) throws ParseException { |
| 153 | + |
| 154 | + Resource resource = notification.getResource(); |
| 155 | + String getAssociateddData = ""; |
| 156 | + if (resource.getAssociatedData() != null) { |
| 157 | + getAssociateddData = resource.getAssociatedData(); |
| 158 | + } |
| 159 | + byte[] associatedData = getAssociateddData.getBytes(StandardCharsets.UTF_8); |
| 160 | + byte[] nonce = resource.getNonce().getBytes(StandardCharsets.UTF_8); |
| 161 | + String ciphertext = resource.getCiphertext(); |
| 162 | + AesUtil aesUtil = new AesUtil(apiV3Key); |
| 163 | + String decryptData; |
| 164 | + try { |
| 165 | + decryptData = aesUtil.decryptToString(associatedData, nonce, ciphertext); |
| 166 | + } catch (GeneralSecurityException e) { |
| 167 | + throw new ParseException("AES解密失败,resource:" + resource.toString(), e); |
| 168 | + } |
| 169 | + notification.setDecryptData(decryptData); |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments