From 91dc8e8f8a7ef428ba42ad9a17eb7dc8061c610d Mon Sep 17 00:00:00 2001 From: felord Date: Thu, 27 May 2021 18:37:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=20=E7=8E=B0=E5=9C=A8=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E4=BA=A4=E6=98=93=E8=B4=A6=E5=8D=95=E5=92=8C=E8=B5=84?= =?UTF-8?q?=E9=87=91=E8=B4=A6=E5=8D=95=E6=9B=B4=E5=8A=A0=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E4=BA=86=20-=20=E7=9B=B4=E6=8E=A5=E8=BF=94=E5=9B=9E=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E6=B5=81=EF=BC=8C=E5=8F=AF=E4=BB=A5=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=BB=99=E5=89=8D=E7=AB=AF=EF=BC=8C=E4=BC=9A?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=B8=8B=E8=BD=BD=E6=88=90txt=E6=88=96?= =?UTF-8?q?=E8=80=85gzip=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../felord/payment/wechat/v3/AbstractApi.java | 45 +++++++++++++++++-- .../wechat/v3/WechatBatchTransferApi.java | 2 +- .../payment/wechat/v3/WechatPayClient.java | 4 +- 3 files changed, 44 insertions(+), 7 deletions(-) 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 56c15ac..0792178 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 @@ -30,6 +30,7 @@ import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.core.io.Resource; +import org.springframework.http.ContentDisposition; import org.springframework.http.HttpHeaders; import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; @@ -224,7 +225,7 @@ public abstract class AbstractApi { * * @param link the link * @return 对账单内容 ,有可能为空字符 “” - * @see AbstractApi#billResource(String) + * @see AbstractApi#billResource(String) AbstractApi#billResource(String)AbstractApi#billResource(String) */ protected String billCsvDownload(String link) { return this.client().withType(WechatPayV3Type.FILE_DOWNLOAD, link) @@ -241,6 +242,8 @@ public abstract class AbstractApi { /** * 申请交易账单API *

+ * 返回值直接返回前端,会下载tradebill-前缀加上日期的txt文件(默认)或者gzip文件。 + *

* 微信支付按天提供交易账单文件,商户可以通过该接口获取账单文件的下载地址。文件内包含交易相关的金额、时间、营销等信息,供商户核对订单、退款、银行到账等情况。 *

* 注意: @@ -252,6 +255,7 @@ public abstract class AbstractApi { * * * @param tradeBillParams tradeBillParams + * @return the response entity * @since 1.0.3.RELEASE */ public ResponseEntity downloadTradeBill(TradeBillParams tradeBillParams) { @@ -285,7 +289,10 @@ public abstract class AbstractApi { String downloadUrl = Objects.requireNonNull(wechatResponseEntity.getBody()) .get("download_url") .asText(); - return this.billResource(downloadUrl); + + String ext = Objects.equals(TarType.GZIP, tradeBillParams.getTarType()) ? ".gzip" : ".txt"; + String filename = "tradebill-" + tradeBillParams.getBillDate().toString() + ext; + return fileEntity(downloadUrl, filename); } /** @@ -300,6 +307,7 @@ public abstract class AbstractApi { * * * @param fundFlowBillParams fundFlowBillParams + * @return the response entity * @since 1.0.3.RELEASE */ public ResponseEntity downloadFundFlowBill(FundFlowBillParams fundFlowBillParams) { @@ -327,9 +335,19 @@ public abstract class AbstractApi { String downloadUrl = Objects.requireNonNull(wechatResponseEntity.getBody()) .get("download_url") .asText(); - return this.billResource(downloadUrl); + + String ext = Objects.equals(TarType.GZIP, fundFlowBillParams.getTarType()) ? ".gzip" : ".txt"; + String filename = "fundflowbill-" + fundFlowBillParams.getBillDate().toString() + ext; + return fileEntity(downloadUrl, filename); } + private ResponseEntity fileEntity(String downloadUrl, String filename) { + ResponseEntity responseEntity = this.billResource(downloadUrl); + HttpHeaders httpHeaders = new HttpHeaders(); + // utf is not support + httpHeaders.setContentDisposition(ContentDisposition.attachment().filename(filename).build()); + return ResponseEntity.ok().headers(httpHeaders).body(responseEntity.getBody()); + } /** * 对账单下载,流文件。 @@ -345,6 +363,25 @@ public abstract class AbstractApi { .toUri(); return Get(uri); }) - .resource(); + .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); } } diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatBatchTransferApi.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatBatchTransferApi.java index 5cf77af..25dd953 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatBatchTransferApi.java +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v3/WechatBatchTransferApi.java @@ -254,6 +254,6 @@ public class WechatBatchTransferApi extends AbstractApi { .request(); String downloadUrl = wechatResponseEntity.getBody().get("download_url").asText(); Assert.hasText(downloadUrl, "download url has no text"); - return this.billResource(downloadUrl); + return this.billResource(downloadUrl, false); } } 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 b1b9957..27a6d16 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 @@ -185,10 +185,10 @@ public class WechatPayClient { * @return the string * @since 1.0.6.RELEASE */ - public ResponseEntity resource() { + protected ResponseEntity resource(boolean newLine) { RequestEntity requestEntity = this.requestEntityBiFunction.apply(this.wechatPayV3Type, this.model); WechatRequestEntity wechatRequestEntity = WechatRequestEntity.of(requestEntity, this.responseBodyConsumer); - return this.doResource(this.header(false,wechatRequestEntity)); + return this.doResource(this.header(newLine,wechatRequestEntity)); }