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)); }