代金券发放API优化

This commit is contained in:
xiafang
2020-11-26 11:28:21 +08:00
parent d69db1788f
commit 5074156c24
5 changed files with 36 additions and 35 deletions

View File

@@ -52,20 +52,20 @@ public class WechatPayConfiguration {
* @return the wechat pay service * @return the wechat pay service
*/ */
@Bean @Bean
public WechatPayV3Client wechatPayService(SignatureProvider signatureProvider) { public WechatPayClient wechatPayService(SignatureProvider signatureProvider) {
return new WechatPayV3Client(signatureProvider); return new WechatPayClient(signatureProvider);
} }
/** /**
* Wechat pay v3 api. * 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 * @param wechatMetaBean the wechat meta bean
* @return the wechat pay v 3 api * @return the wechat pay v 3 api
*/ */
@Bean @Bean
public WechatPayApi wechatPayApi(WechatPayV3Client wechatPayV3Client, WechatMetaBean wechatMetaBean) { public WechatPayApi wechatPayApi(WechatPayClient wechatPayClient, WechatMetaBean wechatMetaBean) {
return new WechatPayApi(wechatPayV3Client,wechatMetaBean); return new WechatPayApi(wechatPayClient,wechatMetaBean);
} }
/** /**

View File

@@ -3,7 +3,7 @@ package com.enongm.dianji.payment.wechat.enumeration;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
/** /**
* The enum Pay type. * The Wechat Pay V3 type.
* *
* @author Dax * @author Dax
* @since 14 :25 * @since 14 :25
@@ -62,7 +62,7 @@ public enum WechatPayV3Type {
/** /**
* 营销图片上传API. * 营销图片上传API.
*/ */
IMAGE_UPLOAD(HttpMethod.POST, "%s/v3/marketing/favor/media/image-upload"); MARKETING_IMAGE_UPLOAD(HttpMethod.POST, "%s/v3/marketing/favor/media/image-upload");

View File

@@ -37,7 +37,7 @@ import java.util.Objects;
*/ */
public class WechatPayApi { public class WechatPayApi {
private static final ObjectMapper MAPPER = new ObjectMapper(); private static final ObjectMapper MAPPER = new ObjectMapper();
private final WechatPayV3Client wechatPayV3Client; private final WechatPayClient wechatPayClient;
private final WechatMetaBean wechatMetaBean; private final WechatMetaBean wechatMetaBean;
static { static {
@@ -49,11 +49,11 @@ public class WechatPayApi {
/** /**
* Wechat Pay V3 Api. * 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 * @param wechatMetaBean the wechat meta bean
*/ */
public WechatPayApi(WechatPayV3Client wechatPayV3Client, WechatMetaBean wechatMetaBean) { public WechatPayApi(WechatPayClient wechatPayClient, WechatMetaBean wechatMetaBean) {
this.wechatPayV3Client = wechatPayV3Client; this.wechatPayClient = wechatPayClient;
this.wechatMetaBean = wechatMetaBean; this.wechatMetaBean = wechatMetaBean;
} }
@@ -66,7 +66,7 @@ public class WechatPayApi {
*/ */
public WechatResponseEntity<ObjectNode> createStock(StocksCreateParams params) { public WechatResponseEntity<ObjectNode> createStock(StocksCreateParams params) {
WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>(); WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
wechatPayV3Client.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_COUPON_STOCKS, params) wechatPayClient.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_COUPON_STOCKS, params)
.function(this::createStocksFunction) .function(this::createStocksFunction)
.consumer(wechatResponseEntity::convert) .consumer(wechatResponseEntity::convert)
.request(); .request();
@@ -96,7 +96,7 @@ public class WechatPayApi {
*/ */
public WechatResponseEntity<ObjectNode> startStock(String stockId) { public WechatResponseEntity<ObjectNode> startStock(String stockId) {
WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>(); WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
wechatPayV3Client.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_START, stockId) wechatPayClient.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_START, stockId)
.function(this::startStockFunction) .function(this::startStockFunction)
.consumer(wechatResponseEntity::convert) .consumer(wechatResponseEntity::convert)
.request(); .request();
@@ -128,7 +128,7 @@ public class WechatPayApi {
*/ */
public WechatResponseEntity<ObjectNode> queryStockDetail(String stockId) { public WechatResponseEntity<ObjectNode> queryStockDetail(String stockId) {
WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>(); WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
wechatPayV3Client.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_DETAIL, stockId) wechatPayClient.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_DETAIL, stockId)
.function(this::stockDetailFunction) .function(this::stockDetailFunction)
.consumer(wechatResponseEntity::convert) .consumer(wechatResponseEntity::convert)
.request(); .request();
@@ -157,7 +157,7 @@ public class WechatPayApi {
*/ */
public WechatResponseEntity<ObjectNode> queryMerchantsByStockId(StocksQueryParams params) { public WechatResponseEntity<ObjectNode> queryMerchantsByStockId(StocksQueryParams params) {
WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>(); WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
wechatPayV3Client.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_MERCHANTS, params) wechatPayClient.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS_MERCHANTS, params)
.function(this::queryStocksFunction) .function(this::queryStocksFunction)
.consumer(wechatResponseEntity::convert) .consumer(wechatResponseEntity::convert)
.request(); .request();
@@ -174,7 +174,7 @@ public class WechatPayApi {
*/ */
public WechatResponseEntity<ObjectNode> queryStocksByMch(StocksQueryParams params) { public WechatResponseEntity<ObjectNode> queryStocksByMch(StocksQueryParams params) {
WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>(); WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
wechatPayV3Client.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS, params) wechatPayClient.withType(WechatPayV3Type.MARKETING_FAVOR_STOCKS, params)
.function(this::queryStocksFunction) .function(this::queryStocksFunction)
.consumer(wechatResponseEntity::convert) .consumer(wechatResponseEntity::convert)
.request(); .request();
@@ -231,7 +231,7 @@ public class WechatPayApi {
*/ */
public WechatResponseEntity<ObjectNode> sendStocks(StocksSendParams params) { public WechatResponseEntity<ObjectNode> sendStocks(StocksSendParams params) {
WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>(); WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
wechatPayV3Client.withType(WechatPayV3Type.MARKETING_FAVOR_USERS_COUPONS, params) wechatPayClient.withType(WechatPayV3Type.MARKETING_FAVOR_USERS_COUPONS, params)
.function(this::sendStocksFunction) .function(this::sendStocksFunction)
.consumer(wechatResponseEntity::convert) .consumer(wechatResponseEntity::convert)
.request(); .request();
@@ -264,7 +264,7 @@ public class WechatPayApi {
*/ */
public WechatResponseEntity<ObjectNode> appPay(AppPayParams payParams) { public WechatResponseEntity<ObjectNode> appPay(AppPayParams payParams) {
WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>(); WechatResponseEntity<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
wechatPayV3Client.withType(WechatPayV3Type.APP, payParams) wechatPayClient.withType(WechatPayV3Type.APP, payParams)
.function(this::appPayFunction) .function(this::appPayFunction)
.consumer(wechatResponseEntity::convert) .consumer(wechatResponseEntity::convert)
.request(); .request();

View File

@@ -18,7 +18,7 @@ import java.util.function.Consumer;
* @author Dax * @author Dax
* @since 11 :43 * @since 11 :43
*/ */
public class WechatPayV3Client { public class WechatPayClient {
private final PayFilterChain payFilterChain; private final PayFilterChain payFilterChain;
/** /**
@@ -26,10 +26,11 @@ public class WechatPayV3Client {
* *
* @param signatureProvider the signature provider * @param signatureProvider the signature provider
*/ */
public WechatPayV3Client(SignatureProvider signatureProvider) { public WechatPayClient(SignatureProvider signatureProvider) {
DefaultPayFilterChain defaultPayFilterChain = new DefaultPayFilterChain(); DefaultPayFilterChain defaultPayFilterChain = new DefaultPayFilterChain();
// 构造私钥签名 // 构造私钥签名
defaultPayFilterChain.register(new HeaderFilter(signatureProvider)); defaultPayFilterChain.register(new HeaderFilter(signatureProvider));
// 向微信支付服务器发起请求
defaultPayFilterChain.register(new HttpRequestFilter(signatureProvider)); defaultPayFilterChain.register(new HttpRequestFilter(signatureProvider));
this.payFilterChain = defaultPayFilterChain; this.payFilterChain = defaultPayFilterChain;
} }
@@ -38,9 +39,9 @@ public class WechatPayV3Client {
/** /**
* 构造 {@link WechatRequestEntity}. * 构造 {@link WechatRequestEntity}.
* *
* @param <M> the type parameter * @param <M> the type parameter
* @param wechatPayV3Type the v 3 pay type * @param wechatPayV3Type the v 3 pay type
* @param m the m * @param m the m
* @return the executor * @return the executor
*/ */
public <M> Executor<M> withType(WechatPayV3Type wechatPayV3Type, M m) { public <M> Executor<M> withType(WechatPayV3Type wechatPayV3Type, M m) {
@@ -57,7 +58,7 @@ public class WechatPayV3Client {
/** /**
* The V 3 pay type. * The V 3 pay type.
*/ */
private final WechatPayV3Type wechatPayV3Type; private final WechatPayV3Type wechatPayV3Type;
/** /**
* The Pay filter chain. * The Pay filter chain.
@@ -78,9 +79,9 @@ public class WechatPayV3Client {
/** /**
* Instantiates a new Executor. * Instantiates a new Executor.
* *
* @param wechatPayV3Type the v 3 pay type * @param wechatPayV3Type the v 3 pay type
* @param model the model * @param model the model
* @param payFilterChain the pay filter chain * @param payFilterChain the pay filter chain
*/ */
public Executor(WechatPayV3Type wechatPayV3Type, public Executor(WechatPayV3Type wechatPayV3Type,
M model, M model,

View File

@@ -46,17 +46,17 @@ public class HeaderFilter implements PayFilter {
String body = requestEntity.hasBody() ? Objects.requireNonNull(requestEntity.getBody()).toString() : ""; String body = requestEntity.hasBody() ? Objects.requireNonNull(requestEntity.getBody()).toString() : "";
String authorization = signatureProvider.requestSign(httpMethod.name(), canonicalUrl, body); String authorization = signatureProvider.requestSign(httpMethod.name(), canonicalUrl, body);
HttpHeaders headers = requestEntity.getHeaders(); HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.addAll(requestEntity.getHeaders());
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
// 兼容图片上传,自定义优先级最高 // 兼容图片上传,自定义优先级最高
if (Objects.isNull(headers.getContentType())) { if (Objects.isNull(httpHeaders.getContentType())) {
headers.setContentType(MediaType.APPLICATION_JSON); httpHeaders.setContentType(MediaType.APPLICATION_JSON);
} }
headers.add("Authorization", authorization); httpHeaders.add("Authorization", authorization);
headers.add("User-Agent", "X-Pay-Service"); httpHeaders.add("User-Agent", "X-Pay-Service");
chain.doChain(requestEntity.headers(headers)); chain.doChain(requestEntity.headers(httpHeaders));
} }