diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/AbstractApi.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/AbstractApi.java index 0792178..693da19 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/AbstractApi.java +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/AbstractApi.java @@ -225,7 +225,7 @@ public abstract class AbstractApi { * * @param link the link * @return 对账单内容 ,有可能为空字符 “” - * @see AbstractApi#billResource(String) AbstractApi#billResource(String)AbstractApi#billResource(String) + * @see AbstractApi#billResource(String) AbstractApi#billResource(String)AbstractApi#billResource(String)AbstractApi#billResource(String) */ protected String billCsvDownload(String link) { return this.client().withType(WechatPayV3Type.FILE_DOWNLOAD, link) @@ -292,7 +292,7 @@ public abstract class AbstractApi { String ext = Objects.equals(TarType.GZIP, tradeBillParams.getTarType()) ? ".gzip" : ".txt"; String filename = "tradebill-" + tradeBillParams.getBillDate().toString() + ext; - return fileEntity(downloadUrl, filename); + return downloadBillResponse(downloadUrl, filename); } /** @@ -338,10 +338,17 @@ public abstract class AbstractApi { String ext = Objects.equals(TarType.GZIP, fundFlowBillParams.getTarType()) ? ".gzip" : ".txt"; String filename = "fundflowbill-" + fundFlowBillParams.getBillDate().toString() + ext; - return fileEntity(downloadUrl, filename); + return this.downloadBillResponse(downloadUrl, filename); } - private ResponseEntity fileEntity(String downloadUrl, String filename) { + /** + * 调用{@code /v3/billdownload/file}直接下载为文件. + * + * @param downloadUrl 格式为 {@code https://api.mch.weixin.qq.com/v3/billdownload/file?token=xxx} + * @param filename 文件名称包含扩展名 + * @return the response entity + */ + public ResponseEntity downloadBillResponse(String downloadUrl, String filename) { ResponseEntity responseEntity = this.billResource(downloadUrl); HttpHeaders httpHeaders = new HttpHeaders(); // utf is not support @@ -350,7 +357,7 @@ public abstract class AbstractApi { } /** - * 对账单下载,流文件。 + * 调用{@code /v3/billdownload/file}下载返回的原始文件流 * * @param link the link * @return response entity @@ -363,25 +370,6 @@ public abstract class AbstractApi { .toUri(); return Get(uri); }) - .resource(true); - } - - - /** - * todo 对账单下载,流文件。 - * - * @param link the link - * @param newLine the new line - * @return response entity - */ - protected ResponseEntity billResource(String link, boolean newLine) { - return this.client().withType(WechatPayV3Type.FILE_DOWNLOAD, link) - .function((type, downloadUrl) -> { - URI uri = UriComponentsBuilder.fromHttpUrl(downloadUrl) - .build() - .toUri(); - return Get(uri); - }) - .resource(newLine); + .resource(); } } diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/SignatureProvider.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/SignatureProvider.java index 60e24c0..f3af733 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/SignatureProvider.java +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/SignatureProvider.java @@ -118,7 +118,6 @@ public class SignatureProvider { /** * 我方请求前用 SHA256withRSA 加签,使用API证书. * - * @param newLine 签名协议不兼容而增加的开关 github issues#18 * @param tenantId the properties key * @param method the method * @param canonicalUrl the canonical url @@ -126,7 +125,7 @@ public class SignatureProvider { * @return the string */ @SneakyThrows - public String requestSign(boolean newLine, String tenantId, String method, String canonicalUrl, String body) { + public String requestSign(String tenantId, String method, String canonicalUrl, String body) { long timestamp = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")); String nonceStr = nonceStrGenerator.generateId() @@ -134,7 +133,7 @@ public class SignatureProvider { .replaceAll("-", ""); WechatMetaBean wechatMetaBean = wechatMetaContainer.getWechatMeta(tenantId); PrivateKey privateKey = wechatMetaBean.getKeyPair().getPrivate(); - String encode = this.doRequestSign(newLine, privateKey, method, canonicalUrl, String.valueOf(timestamp), nonceStr, body); + String encode = this.doRequestSign(privateKey, method, canonicalUrl, String.valueOf(timestamp), nonceStr, body); // 序列号 String serialNo = wechatMetaBean.getSerialNumber(); // 生成token @@ -148,17 +147,16 @@ public class SignatureProvider { /** * Do request sign. * - * @param newLine the has suffix * @param privateKey the private key * @param orderedComponents the orderedComponents * @return the string * @since 1.0.4.RELEASE */ @SneakyThrows - public String doRequestSign(boolean newLine, PrivateKey privateKey, String... orderedComponents) { + public String doRequestSign(PrivateKey privateKey, String... orderedComponents) { Signature signer = Signature.getInstance("SHA256withRSA", BC_PROVIDER); signer.initSign(privateKey); - final String signatureStr = createSign(newLine, orderedComponents); + final String signatureStr = createSign(true, orderedComponents); signer.update(signatureStr.getBytes(StandardCharsets.UTF_8)); return Base64Utils.encodeToString(signer.sign()); } @@ -206,7 +204,7 @@ public class SignatureProvider { } // 签名 HttpMethod httpMethod = WechatPayV3Type.CERT.method(); - String authorization = requestSign(true, tenantId, httpMethod.name(), canonicalUrl, ""); + String authorization = requestSign(tenantId, httpMethod.name(), canonicalUrl, ""); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); @@ -223,7 +221,7 @@ public class SignatureProvider { ArrayNode certificates = bodyObjectNode.withArray("data"); if (certificates.isArray() && certificates.size() > 0) { CERTIFICATE_MAP.remove(tenantId); - final CertificateFactory certificateFactory = CertificateFactory.getInstance("X509",BC_PROVIDER); + final CertificateFactory certificateFactory = CertificateFactory.getInstance("X509", BC_PROVIDER); certificates.forEach(objectNode -> { JsonNode encryptCertificate = objectNode.get("encrypt_certificate"); String associatedData = encryptCertificate.get("associated_data").asText(); diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatDirectPayApi.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatDirectPayApi.java index bab1ce0..f3ba6a7 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatDirectPayApi.java +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatDirectPayApi.java @@ -87,7 +87,7 @@ public class WechatDirectPayApi extends AbstractApi { .toString() .replaceAll("-", ""); String prepayId = body.get("prepay_id").asText(); - String paySign = signatureProvider.doRequestSign(true, privateKey, appId, timestamp, nonceStr, prepayId); + String paySign = signatureProvider.doRequestSign(privateKey, appId, timestamp, nonceStr, prepayId); String mchId = wechatMetaBean.getV3().getMchId(); body.put("appid", appId); @@ -136,7 +136,7 @@ public class WechatDirectPayApi extends AbstractApi { .toString() .replaceAll("-", ""); String packageStr = "prepay_id=" + body.get("prepay_id").asText(); - String paySign = signatureProvider.doRequestSign(true, privateKey, appId, timestamp, nonceStr, packageStr); + String paySign = signatureProvider.doRequestSign(privateKey, appId, timestamp, nonceStr, packageStr); body.put("appId", appId); body.put("timeStamp", timestamp); diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatPartnerPayApi.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatPartnerPayApi.java index 908c15b..a25cda6 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatPartnerPayApi.java +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatPartnerPayApi.java @@ -87,7 +87,7 @@ public class WechatPartnerPayApi extends AbstractApi { .toString() .replaceAll("-", ""); String prepayId = body.get("prepay_id").asText(); - String paySign = signatureProvider.doRequestSign(true, privateKey, subAppid, timestamp, nonceStr, prepayId); + String paySign = signatureProvider.doRequestSign(privateKey, subAppid, timestamp, nonceStr, prepayId); body.put("appid", subAppid); body.put("partnerid", partnerPayParams.getSubMchid()); @@ -136,7 +136,7 @@ public class WechatPartnerPayApi extends AbstractApi { .toString() .replaceAll("-", ""); String packageStr = "prepay_id=" + body.get("prepay_id").asText(); - String paySign = signatureProvider.doRequestSign(true, privateKey, subAppid, timestamp, nonceStr, packageStr); + String paySign = signatureProvider.doRequestSign(privateKey, subAppid, timestamp, nonceStr, packageStr); body.put("appId", subAppid); body.put("timeStamp", timestamp); diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatPayClient.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatPayClient.java index 27a6d16..fb69c71 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatPayClient.java +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatPayClient.java @@ -164,7 +164,7 @@ public class WechatPayClient { public void request() { RequestEntity requestEntity = this.requestEntityBiFunction.apply(this.wechatPayV3Type, this.model); WechatRequestEntity wechatRequestEntity = WechatRequestEntity.of(requestEntity, this.responseBodyConsumer); - this.doExecute(this.header(true,wechatRequestEntity)); + this.doExecute(this.header(wechatRequestEntity)); } @@ -176,7 +176,7 @@ public class WechatPayClient { public String download() { RequestEntity requestEntity = this.requestEntityBiFunction.apply(this.wechatPayV3Type, this.model); WechatRequestEntity wechatRequestEntity = WechatRequestEntity.of(requestEntity, this.responseBodyConsumer); - return this.doDownload(this.header(true,wechatRequestEntity)); + return this.doDownload(this.header(wechatRequestEntity)); } /** @@ -185,10 +185,10 @@ public class WechatPayClient { * @return the string * @since 1.0.6.RELEASE */ - protected ResponseEntity resource(boolean newLine) { + protected ResponseEntity resource() { RequestEntity requestEntity = this.requestEntityBiFunction.apply(this.wechatPayV3Type, this.model); WechatRequestEntity wechatRequestEntity = WechatRequestEntity.of(requestEntity, this.responseBodyConsumer); - return this.doResource(this.header(newLine,wechatRequestEntity)); + return this.doResource(this.header(wechatRequestEntity)); } @@ -199,7 +199,7 @@ public class WechatPayClient { * @param requestEntity the request entity * @return the wechat request entity */ - private WechatRequestEntity header(boolean newLine,WechatRequestEntity requestEntity) { + private WechatRequestEntity header(WechatRequestEntity requestEntity) { UriComponents uri = UriComponentsBuilder.fromUri(requestEntity.getUrl()).build(); String canonicalUrl = uri.getPath(); @@ -220,7 +220,7 @@ public class WechatPayClient { } String tenantId = Objects.requireNonNull(headers.get("Pay-TenantId")).get(0); - String authorization = signatureProvider.requestSign(newLine,tenantId, httpMethod.name(), canonicalUrl, body); + String authorization = signatureProvider.requestSign(tenantId, httpMethod.name(), canonicalUrl, body); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.addAll(headers);