This commit is contained in:
felord.cn
2020-11-20 23:22:40 +08:00
parent a4e5384430
commit 7b58dbb17b
5 changed files with 70 additions and 56 deletions

View File

@@ -13,6 +13,6 @@ public interface PayFilter {
* Do filter.
* @param requestEntity the request entity
* @param chain the chain*/
void doFilter(WechatRequestEntity<?> requestEntity, PayFilterChain chain);
<T> void doFilter(WechatRequestEntity<T> requestEntity, PayFilterChain chain);
}

View File

@@ -56,27 +56,31 @@ public class WechatPayV3Api {
*/
public WechatResponseEntity<?> startStocks(String stockId) {
WechatResponseEntity<ObjectNode> 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<String, String> 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<String, String> 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<ObjectNode> wechatResponseEntity = new WechatResponseEntity<>();
wechatPayV3Client.withPayType(V3PayType.MARKETING_FAVOR_STOCKS_MERCHANTS, params)
.function((v3PayType, par) -> {
MultiValueMap<String, String> 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<String, String> 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<ObjectNode> 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");
}
}

View File

@@ -43,7 +43,7 @@ public class WechatPayV3Client {
* @param m the m
* @return the executor
*/
public <M> Executor<M> withPayType(V3PayType v3PayType,M m) {
public <M> Executor<M> withType(V3PayType v3PayType, M m) {
return new Executor<>(v3PayType,m ,this.payFilterChain);
}

View File

@@ -30,7 +30,7 @@ public class HeaderFilter implements PayFilter {
}
@Override
public void doFilter(WechatRequestEntity<?> requestEntity, PayFilterChain chain) {
public <T> void doFilter(WechatRequestEntity<T> 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));
}

View File

@@ -39,7 +39,7 @@ public class HttpRequestFilter implements PayFilter {
}
@Override
public void doFilter(WechatRequestEntity<?> requestEntity, PayFilterChain chain) {
public <T> void doFilter(WechatRequestEntity<T> requestEntity, PayFilterChain chain) {
ResponseEntity<ObjectNode> responseEntity = restOperations.exchange(requestEntity, ObjectNode.class);
HttpHeaders headers = responseEntity.getHeaders();