From 7b58dbb17ba76b68fe19500286e3a00e30b40877 Mon Sep 17 00:00:00 2001 From: "felord.cn" Date: Fri, 20 Nov 2020 23:22:40 +0800 Subject: [PATCH] rest --- .../dianji/payment/wechat/v3/PayFilter.java | 2 +- .../payment/wechat/v3/WechatPayV3Api.java | 113 ++++++++++-------- .../payment/wechat/v3/WechatPayV3Client.java | 2 +- .../wechat/v3/filter/HeaderFilter.java | 7 +- .../wechat/v3/filter/HttpRequestFilter.java | 2 +- 5 files changed, 70 insertions(+), 56 deletions(-) diff --git a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/PayFilter.java b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/PayFilter.java index 20be17d..aae5799 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/PayFilter.java +++ b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/PayFilter.java @@ -13,6 +13,6 @@ public interface PayFilter { * Do filter. * @param requestEntity the request entity * @param chain the chain*/ - void doFilter(WechatRequestEntity requestEntity, PayFilterChain chain); + void doFilter(WechatRequestEntity requestEntity, PayFilterChain chain); } diff --git a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayV3Api.java b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayV3Api.java index 824dc6b..a0053bb 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayV3Api.java +++ b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayV3Api.java @@ -56,27 +56,31 @@ public class WechatPayV3Api { */ public WechatResponseEntity startStocks(String stockId) { WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); - wechatPayV3Client.withPayType(V3PayType.MARKETING_FAVOR_STOCKS_START, stockId) - .function((v3PayType, s) -> { - WechatPayProperties.V3 v3 = wechatMetaBean.getWechatPayProperties().getV3(); - String mchId = v3.getMchId(); - String httpUrl = v3PayType.uri(WeChatServer.CHINA); - URI uri = UriComponentsBuilder.fromHttpUrl(httpUrl).build().expand(stockId).toUri(); - - Map map = new HashMap<>(); - map.put("stock_creator_mchid", mchId); - try { - return RequestEntity.post(uri) - .body(MAPPER.writeValueAsString(map)); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - throw new PayException("wechat app pay json failed"); - }).consumer(wechatResponseEntity::convert).request(); - + wechatPayV3Client.withType(V3PayType.MARKETING_FAVOR_STOCKS_START, stockId) + .function(this::startStocksFunction) + .consumer(wechatResponseEntity::convert) + .request(); return wechatResponseEntity; } + private RequestEntity startStocksFunction(V3PayType type, String stockId) { + WechatPayProperties.V3 v3 = wechatMetaBean.getWechatPayProperties().getV3(); + String mchId = v3.getMchId(); + String httpUrl = type.uri(WeChatServer.CHINA); + URI uri = UriComponentsBuilder.fromHttpUrl(httpUrl).build().expand(stockId).toUri(); + + Map map = new HashMap<>(); + map.put("stock_creator_mchid", mchId); + try { + return RequestEntity.post(uri) + .body(MAPPER.writeValueAsString(map)); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + throw new PayException("wechat app pay json failed"); + } + + /** * 查询代金券可用商户API * @@ -84,27 +88,32 @@ public class WechatPayV3Api { */ public WechatResponseEntity queryMerchantsByStockId(StocksMchQueryParams params) { WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); - wechatPayV3Client.withPayType(V3PayType.MARKETING_FAVOR_STOCKS_MERCHANTS, params) - .function((v3PayType, par) -> { - - MultiValueMap queryParams = new LinkedMultiValueMap<>(); - queryParams.add("offset", String.valueOf(par.getOffset())); - queryParams.add("limit", String.valueOf(par.getLimit())); - queryParams.add("stock_creator_mchid", par.getStockCreatorMchid()); - String httpUrl = v3PayType.uri(WeChatServer.CHINA); - - URI uri = UriComponentsBuilder.fromHttpUrl(httpUrl) - .queryParams(queryParams) - .build() - .expand(par.getStockId()).toUri(); - - return RequestEntity.get(uri).build(); - }).consumer(wechatResponseEntity::convert).request(); + wechatPayV3Client.withType(V3PayType.MARKETING_FAVOR_STOCKS_MERCHANTS, params) + .function(this::queryMerchantsFunction) + .consumer(wechatResponseEntity::convert) + .request(); return wechatResponseEntity; } + + private RequestEntity queryMerchantsFunction(V3PayType type, StocksMchQueryParams params) { + + MultiValueMap queryParams = new LinkedMultiValueMap<>(); + queryParams.add("offset", String.valueOf(params.getOffset())); + queryParams.add("limit", String.valueOf(params.getLimit())); + queryParams.add("stock_creator_mchid", params.getStockCreatorMchid()); + String httpUrl = type.uri(WeChatServer.CHINA); + + URI uri = UriComponentsBuilder.fromHttpUrl(httpUrl) + .queryParams(queryParams) + .build() + .expand(params.getStockId()).toUri(); + + return RequestEntity.get(uri).build(); + } + /** * APP下单API. * @@ -112,22 +121,26 @@ public class WechatPayV3Api { */ public WechatResponseEntity appPay(AppPayParams payParams) { WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); - wechatPayV3Client.withPayType(V3PayType.APP, payParams) - .function((v3PayType, par) -> { - WechatPayProperties.V3 v3 = wechatMetaBean.getWechatPayProperties().getV3(); - par.setAppid(v3.getAppId()); - par.setMchid(v3.getMchId()); - String httpUrl = v3PayType.uri(WeChatServer.CHINA); - URI uri = UriComponentsBuilder.fromHttpUrl(httpUrl).build().toUri(); - try { - return RequestEntity.post(uri) - .body(MAPPER.writeValueAsString(par)); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - throw new PayException("wechat app pay json failed"); - }).consumer(wechatResponseEntity::convert).request(); - + wechatPayV3Client.withType(V3PayType.APP, payParams) + .function(this::appPayFunction) + .consumer(wechatResponseEntity::convert) + .request(); return wechatResponseEntity; } + + private RequestEntity appPayFunction(V3PayType type, AppPayParams payParams) { + WechatPayProperties.V3 v3 = wechatMetaBean.getWechatPayProperties().getV3(); + payParams.setAppid(v3.getAppId()); + payParams.setMchid(v3.getMchId()); + String httpUrl = type.uri(WeChatServer.CHINA); + URI uri = UriComponentsBuilder.fromHttpUrl(httpUrl).build().toUri(); + try { + return RequestEntity.post(uri) + .body(MAPPER.writeValueAsString(payParams)); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + throw new PayException("wechat app pay json failed"); + } + } diff --git a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayV3Client.java b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayV3Client.java index ff41862..6a90caf 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayV3Client.java +++ b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayV3Client.java @@ -43,7 +43,7 @@ public class WechatPayV3Client { * @param m the m * @return the executor */ - public Executor withPayType(V3PayType v3PayType,M m) { + public Executor withType(V3PayType v3PayType, M m) { return new Executor<>(v3PayType,m ,this.payFilterChain); } diff --git a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/filter/HeaderFilter.java b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/filter/HeaderFilter.java index 6afa006..d929907 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/filter/HeaderFilter.java +++ b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/filter/HeaderFilter.java @@ -30,7 +30,7 @@ public class HeaderFilter implements PayFilter { } @Override - public void doFilter(WechatRequestEntity requestEntity, PayFilterChain chain) { + public void doFilter(WechatRequestEntity requestEntity, PayFilterChain chain) { UriComponents uri = UriComponentsBuilder.fromUri(requestEntity.getUrl()).build(); String canonicalUrl = uri.getPath(); @@ -42,14 +42,15 @@ public class HeaderFilter implements PayFilter { // 签名 HttpMethod httpMethod = requestEntity.getMethod(); Assert.notNull(httpMethod, "httpMethod is required"); - String body = httpMethod.matches("GET") ? "" : Objects.requireNonNull(requestEntity.getBody()).toString(); + + String body = requestEntity.hasBody() ? Objects.requireNonNull(requestEntity.getBody()).toString() : ""; String authorization = signatureProvider.requestSign(httpMethod.name(), canonicalUrl, body); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.setContentType(MediaType.APPLICATION_JSON); headers.add("Authorization", authorization); - headers.add("User-Agent", "pay-service"); + headers.add("User-Agent", "X-Pay-Service"); chain.doChain(requestEntity.headers(headers)); } diff --git a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/filter/HttpRequestFilter.java b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/filter/HttpRequestFilter.java index 420ae9a..c3cc5ba 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/filter/HttpRequestFilter.java +++ b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/filter/HttpRequestFilter.java @@ -39,7 +39,7 @@ public class HttpRequestFilter implements PayFilter { } @Override - public void doFilter(WechatRequestEntity requestEntity, PayFilterChain chain) { + public void doFilter(WechatRequestEntity requestEntity, PayFilterChain chain) { ResponseEntity responseEntity = restOperations.exchange(requestEntity, ObjectNode.class); HttpHeaders headers = responseEntity.getHeaders();