From 5074156c2473f8eced916f0bd5abebe9f4ee8f4b Mon Sep 17 00:00:00 2001 From: xiafang Date: Thu, 26 Nov 2020 11:28:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E9=87=91=E5=88=B8=E5=8F=91=E6=94=BEAP?= =?UTF-8?q?I=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wechat/WechatPayConfiguration.java | 10 ++++---- .../wechat/enumeration/WechatPayV3Type.java | 4 ++-- .../payment/wechat/v3/WechatPayApi.java | 24 +++++++++---------- ...tPayV3Client.java => WechatPayClient.java} | 17 ++++++------- .../wechat/v3/filter/HeaderFilter.java | 16 ++++++------- 5 files changed, 36 insertions(+), 35 deletions(-) rename payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/{WechatPayV3Client.java => WechatPayClient.java} (88%) diff --git a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/WechatPayConfiguration.java b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/WechatPayConfiguration.java index c1c512a..32a70ad 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/WechatPayConfiguration.java +++ b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/WechatPayConfiguration.java @@ -52,20 +52,20 @@ public class WechatPayConfiguration { * @return the wechat pay service */ @Bean - public WechatPayV3Client wechatPayService(SignatureProvider signatureProvider) { - return new WechatPayV3Client(signatureProvider); + public WechatPayClient wechatPayService(SignatureProvider signatureProvider) { + return new WechatPayClient(signatureProvider); } /** * Wechat pay v3 api. * - * @param wechatPayV3Client the wechat pay v 3 client + * @param wechatPayClient the wechat pay v 3 client * @param wechatMetaBean the wechat meta bean * @return the wechat pay v 3 api */ @Bean - public WechatPayApi wechatPayApi(WechatPayV3Client wechatPayV3Client, WechatMetaBean wechatMetaBean) { - return new WechatPayApi(wechatPayV3Client,wechatMetaBean); + public WechatPayApi wechatPayApi(WechatPayClient wechatPayClient, WechatMetaBean wechatMetaBean) { + return new WechatPayApi(wechatPayClient,wechatMetaBean); } /** diff --git a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/enumeration/WechatPayV3Type.java b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/enumeration/WechatPayV3Type.java index 1bb7af9..491f54a 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/enumeration/WechatPayV3Type.java +++ b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/enumeration/WechatPayV3Type.java @@ -3,7 +3,7 @@ package com.enongm.dianji.payment.wechat.enumeration; import org.springframework.http.HttpMethod; /** - * The enum Pay type. + * The Wechat Pay V3 type. * * @author Dax * @since 14 :25 @@ -62,7 +62,7 @@ public enum WechatPayV3Type { /** * 营销图片上传API. */ - IMAGE_UPLOAD(HttpMethod.POST, "%s/v3/marketing/favor/media/image-upload"); + MARKETING_IMAGE_UPLOAD(HttpMethod.POST, "%s/v3/marketing/favor/media/image-upload"); diff --git a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayApi.java b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayApi.java index 54cbf51..4a39c89 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayApi.java +++ b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayApi.java @@ -37,7 +37,7 @@ import java.util.Objects; */ public class WechatPayApi { private static final ObjectMapper MAPPER = new ObjectMapper(); - private final WechatPayV3Client wechatPayV3Client; + private final WechatPayClient wechatPayClient; private final WechatMetaBean wechatMetaBean; static { @@ -49,11 +49,11 @@ public class WechatPayApi { /** * Wechat Pay V3 Api. * - * @param wechatPayV3Client the wechat pay v 3 client - * @param wechatMetaBean the wechat meta bean + * @param wechatPayClient the wechat pay v 3 client + * @param wechatMetaBean the wechat meta bean */ - public WechatPayApi(WechatPayV3Client wechatPayV3Client, WechatMetaBean wechatMetaBean) { - this.wechatPayV3Client = wechatPayV3Client; + public WechatPayApi(WechatPayClient wechatPayClient, WechatMetaBean wechatMetaBean) { + this.wechatPayClient = wechatPayClient; this.wechatMetaBean = wechatMetaBean; } @@ -66,7 +66,7 @@ public class WechatPayApi { */ public WechatResponseEntity createStock(StocksCreateParams params) { WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); - wechatPayV3Client.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_COUPON_STOCKS, params) + wechatPayClient.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_COUPON_STOCKS, params) .function(this::createStocksFunction) .consumer(wechatResponseEntity::convert) .request(); @@ -96,7 +96,7 @@ public class WechatPayApi { */ public WechatResponseEntity startStock(String stockId) { WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); - wechatPayV3Client.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_START, stockId) + wechatPayClient.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_START, stockId) .function(this::startStockFunction) .consumer(wechatResponseEntity::convert) .request(); @@ -128,7 +128,7 @@ public class WechatPayApi { */ public WechatResponseEntity queryStockDetail(String stockId) { WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); - wechatPayV3Client.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_DETAIL, stockId) + wechatPayClient.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_DETAIL, stockId) .function(this::stockDetailFunction) .consumer(wechatResponseEntity::convert) .request(); @@ -157,7 +157,7 @@ public class WechatPayApi { */ public WechatResponseEntity queryMerchantsByStockId(StocksQueryParams params) { WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); - wechatPayV3Client.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_MERCHANTS, params) + wechatPayClient.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_MERCHANTS, params) .function(this::queryStocksFunction) .consumer(wechatResponseEntity::convert) .request(); @@ -174,7 +174,7 @@ public class WechatPayApi { */ public WechatResponseEntity queryStocksByMch(StocksQueryParams params) { WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); - wechatPayV3Client.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS, params) + wechatPayClient.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS, params) .function(this::queryStocksFunction) .consumer(wechatResponseEntity::convert) .request(); @@ -231,7 +231,7 @@ public class WechatPayApi { */ public WechatResponseEntity sendStocks(StocksSendParams params) { WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); - wechatPayV3Client.withType(WechatPayV3Type.MARKETING_FAVOR_USERS_COUPONS, params) + wechatPayClient.withType(WechatPayV3Type.MARKETING_FAVOR_USERS_COUPONS, params) .function(this::sendStocksFunction) .consumer(wechatResponseEntity::convert) .request(); @@ -264,7 +264,7 @@ public class WechatPayApi { */ public WechatResponseEntity appPay(AppPayParams payParams) { WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); - wechatPayV3Client.withType(WechatPayV3Type.APP, payParams) + wechatPayClient.withType(WechatPayV3Type.APP, payParams) .function(this::appPayFunction) .consumer(wechatResponseEntity::convert) .request(); 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/WechatPayClient.java similarity index 88% rename from payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayV3Client.java rename to payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatPayClient.java index f4b41e2..7431c80 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/WechatPayClient.java @@ -18,7 +18,7 @@ import java.util.function.Consumer; * @author Dax * @since 11 :43 */ -public class WechatPayV3Client { +public class WechatPayClient { private final PayFilterChain payFilterChain; /** @@ -26,10 +26,11 @@ public class WechatPayV3Client { * * @param signatureProvider the signature provider */ - public WechatPayV3Client(SignatureProvider signatureProvider) { + public WechatPayClient(SignatureProvider signatureProvider) { DefaultPayFilterChain defaultPayFilterChain = new DefaultPayFilterChain(); // 构造私钥签名 defaultPayFilterChain.register(new HeaderFilter(signatureProvider)); + // 向微信支付服务器发起请求 defaultPayFilterChain.register(new HttpRequestFilter(signatureProvider)); this.payFilterChain = defaultPayFilterChain; } @@ -38,9 +39,9 @@ public class WechatPayV3Client { /** * 构造 {@link WechatRequestEntity}. * - * @param the type parameter + * @param the type parameter * @param wechatPayV3Type the v 3 pay type - * @param m the m + * @param m the m * @return the executor */ public Executor withType(WechatPayV3Type wechatPayV3Type, M m) { @@ -57,7 +58,7 @@ public class WechatPayV3Client { /** * The V 3 pay type. */ - private final WechatPayV3Type wechatPayV3Type; + private final WechatPayV3Type wechatPayV3Type; /** * The Pay filter chain. @@ -78,9 +79,9 @@ public class WechatPayV3Client { /** * Instantiates a new Executor. * - * @param wechatPayV3Type the v 3 pay type - * @param model the model - * @param payFilterChain the pay filter chain + * @param wechatPayV3Type the v 3 pay type + * @param model the model + * @param payFilterChain the pay filter chain */ public Executor(WechatPayV3Type wechatPayV3Type, M model, 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 6446826..d977441 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 @@ -46,17 +46,17 @@ public class HeaderFilter implements PayFilter { String body = requestEntity.hasBody() ? Objects.requireNonNull(requestEntity.getBody()).toString() : ""; String authorization = signatureProvider.requestSign(httpMethod.name(), canonicalUrl, body); - HttpHeaders headers = requestEntity.getHeaders(); - - headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.addAll(requestEntity.getHeaders()); + httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); // 兼容图片上传,自定义优先级最高 - if (Objects.isNull(headers.getContentType())) { - headers.setContentType(MediaType.APPLICATION_JSON); + if (Objects.isNull(httpHeaders.getContentType())) { + httpHeaders.setContentType(MediaType.APPLICATION_JSON); } - headers.add("Authorization", authorization); - headers.add("User-Agent", "X-Pay-Service"); + httpHeaders.add("Authorization", authorization); + httpHeaders.add("User-Agent", "X-Pay-Service"); - chain.doChain(requestEntity.headers(headers)); + chain.doChain(requestEntity.headers(httpHeaders)); }