refactor: 交易账单和资金账单现在能够正常的下载文件了

This commit is contained in:
felord
2021-05-25 12:38:13 +08:00
parent 9d01a0fa3a
commit 0c230547a9
3 changed files with 45 additions and 29 deletions

View File

@@ -27,6 +27,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@@ -89,7 +90,7 @@ public abstract class AbstractApi {
*/ */
private void applyObjectMapper(ObjectMapper mapper) { private void applyObjectMapper(ObjectMapper mapper) {
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE) mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
// empty string error // empty string error
.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)
.setSerializationInclusion(JsonInclude.Include.NON_NULL) .setSerializationInclusion(JsonInclude.Include.NON_NULL)
@@ -217,13 +218,15 @@ public abstract class AbstractApi {
throw new PayException("wechat app pay json failed"); throw new PayException("wechat app pay json failed");
} }
} }
/** /**
* 对账单内容下载,非流文件。 * 对账单CSV内容下载,非流文件。
* *
* @param link the link * @param link the link
* @return 对账单内容 ,有可能为空字符 “” * @return 对账单内容 ,有可能为空字符 “”
* @see AbstractApi#billResource(String)
*/ */
protected String billDownload(String link) { protected String billCsvDownload(String link) {
return this.client().withType(WechatPayV3Type.FILE_DOWNLOAD, link) return this.client().withType(WechatPayV3Type.FILE_DOWNLOAD, link)
.function((type, downloadUrl) -> { .function((type, downloadUrl) -> {
URI uri = UriComponentsBuilder.fromHttpUrl(downloadUrl) URI uri = UriComponentsBuilder.fromHttpUrl(downloadUrl)
@@ -234,22 +237,6 @@ public abstract class AbstractApi {
.download(); .download();
} }
/**
* 对账单下载,流文件。
*
* @param link the link
* @return response entity
*/
protected ResponseEntity<Resource> billResource(String link) {
return this.client().withType(WechatPayV3Type.FILE_DOWNLOAD, link)
.function((type, downloadUrl) -> {
URI uri = UriComponentsBuilder.fromHttpUrl(downloadUrl)
.build()
.toUri();
return Get(uri);
})
.resource();
}
/** /**
* 申请交易账单API * 申请交易账单API
@@ -267,7 +254,9 @@ public abstract class AbstractApi {
* @param tradeBillParams tradeBillParams * @param tradeBillParams tradeBillParams
* @since 1.0.3.RELEASE * @since 1.0.3.RELEASE
*/ */
public final void downloadTradeBill(TradeBillParams tradeBillParams) { public ResponseEntity<Resource> downloadTradeBill(TradeBillParams tradeBillParams) {
WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
this.client().withType(WechatPayV3Type.TRADEBILL, tradeBillParams) this.client().withType(WechatPayV3Type.TRADEBILL, tradeBillParams)
.function((wechatPayV3Type, params) -> { .function((wechatPayV3Type, params) -> {
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>(); MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
@@ -291,8 +280,12 @@ public abstract class AbstractApi {
.build().toUri(); .build().toUri();
return Get(uri); return Get(uri);
}) })
.consumer(response -> this.billDownload(Objects.requireNonNull(response.getBody()).get("download_url").asText())) .consumer(wechatResponseEntity::convert)
.request(); .request();
String downloadUrl = Objects.requireNonNull(wechatResponseEntity.getBody())
.get("download_url")
.asText();
return this.billResource(downloadUrl);
} }
/** /**
@@ -309,7 +302,8 @@ public abstract class AbstractApi {
* @param fundFlowBillParams fundFlowBillParams * @param fundFlowBillParams fundFlowBillParams
* @since 1.0.3.RELEASE * @since 1.0.3.RELEASE
*/ */
public final void downloadFundFlowBill(FundFlowBillParams fundFlowBillParams) { public ResponseEntity<Resource> downloadFundFlowBill(FundFlowBillParams fundFlowBillParams) {
WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
this.client().withType(WechatPayV3Type.FUNDFLOWBILL, fundFlowBillParams) this.client().withType(WechatPayV3Type.FUNDFLOWBILL, fundFlowBillParams)
.function((wechatPayV3Type, params) -> { .function((wechatPayV3Type, params) -> {
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>(); MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
@@ -328,7 +322,29 @@ public abstract class AbstractApi {
.build().toUri(); .build().toUri();
return Get(uri); return Get(uri);
}) })
.consumer(response -> this.billDownload(Objects.requireNonNull(response.getBody()).get("download_url").asText())) .consumer(wechatResponseEntity::convert)
.request(); .request();
String downloadUrl = Objects.requireNonNull(wechatResponseEntity.getBody())
.get("download_url")
.asText();
return this.billResource(downloadUrl);
}
/**
* 对账单下载,流文件。
*
* @param link the link
* @return response entity
*/
protected ResponseEntity<Resource> billResource(String link) {
return this.client().withType(WechatPayV3Type.FILE_DOWNLOAD, link)
.function((type, downloadUrl) -> {
URI uri = UriComponentsBuilder.fromHttpUrl(downloadUrl)
.build()
.toUri();
return Get(uri);
})
.resource();
} }
} }

View File

@@ -87,7 +87,7 @@ public class WechatDirectPayApi extends AbstractApi {
.toString() .toString()
.replaceAll("-", ""); .replaceAll("-", "");
String prepayId = body.get("prepay_id").asText(); String prepayId = body.get("prepay_id").asText();
String paySign = signatureProvider.doRequestSign(true,privateKey, appId, timestamp, nonceStr, prepayId); String paySign = signatureProvider.doRequestSign(true, privateKey, appId, timestamp, nonceStr, prepayId);
String mchId = wechatMetaBean.getV3().getMchId(); String mchId = wechatMetaBean.getV3().getMchId();
body.put("appid", appId); body.put("appid", appId);
@@ -136,7 +136,7 @@ public class WechatDirectPayApi extends AbstractApi {
.toString() .toString()
.replaceAll("-", ""); .replaceAll("-", "");
String packageStr = "prepay_id=" + body.get("prepay_id").asText(); String packageStr = "prepay_id=" + body.get("prepay_id").asText();
String paySign = signatureProvider.doRequestSign(true,privateKey, appId, timestamp, nonceStr, packageStr); String paySign = signatureProvider.doRequestSign(true, privateKey, appId, timestamp, nonceStr, packageStr);
body.put("appId", appId); body.put("appId", appId);
body.put("timeStamp", timestamp); body.put("timeStamp", timestamp);

View File

@@ -493,7 +493,7 @@ public class WechatMarketingFavorApi extends AbstractApi {
* *
* @param stockId the stock id * @param stockId the stock id
* @return the wechat response entity * @return the wechat response entity
* @see AbstractApi#billDownload(String) AbstractApi#billDownload(String)对账单下载api * @see AbstractApi#billCsvDownload(String) AbstractApi#billDownload(String)对账单下载api
*/ */
public WechatResponseEntity<ObjectNode> downloadStockUseFlow(String stockId) { public WechatResponseEntity<ObjectNode> downloadStockUseFlow(String stockId) {
WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>(); WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
@@ -501,7 +501,7 @@ public class WechatMarketingFavorApi extends AbstractApi {
.function(this::downloadFlowFunction) .function(this::downloadFlowFunction)
.consumer(wechatResponseEntity::convert) .consumer(wechatResponseEntity::convert)
.request(); .request();
String csv = this.billDownload(wechatResponseEntity.getBody().get("url").asText()); String csv = this.billCsvDownload(wechatResponseEntity.getBody().get("url").asText());
wechatResponseEntity.getBody().put("csv", csv); wechatResponseEntity.getBody().put("csv", csv);
return wechatResponseEntity; return wechatResponseEntity;
} }
@@ -515,7 +515,7 @@ public class WechatMarketingFavorApi extends AbstractApi {
* *
* @param stockId the stock id * @param stockId the stock id
* @return the wechat response entity * @return the wechat response entity
* @see AbstractApi#billDownload(String) AbstractApi#billDownload(String)对账单下载api * @see AbstractApi#billCsvDownload(String) AbstractApi#billDownload(String)对账单下载api
*/ */
public WechatResponseEntity<ObjectNode> downloadStockRefundFlow(String stockId) { public WechatResponseEntity<ObjectNode> downloadStockRefundFlow(String stockId) {
WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>(); WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
@@ -523,7 +523,7 @@ public class WechatMarketingFavorApi extends AbstractApi {
.function(this::downloadFlowFunction) .function(this::downloadFlowFunction)
.consumer(wechatResponseEntity::convert) .consumer(wechatResponseEntity::convert)
.request(); .request();
String csv = this.billDownload(wechatResponseEntity.getBody().get("url").asText()); String csv = this.billCsvDownload(wechatResponseEntity.getBody().get("url").asText());
wechatResponseEntity.getBody().put("csv", csv); wechatResponseEntity.getBody().put("csv", csv);
return wechatResponseEntity; return wechatResponseEntity;
} }