From 0e1efd4e946a99ee1626492b3c8e9b3a1f593d9f Mon Sep 17 00:00:00 2001 From: xiafang Date: Tue, 1 Dec 2020 14:29:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=85=AC=E4=BC=97=E5=8F=B7?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=8F=B7=E6=8E=88=E6=9D=83=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...th2AuthorizationRequestRedirectProvider.java | 15 +++++++++++---- .../payment/wechat/oauth2/OAuth2Exchange.java | 17 +++++++++++++++++ .../payment/wechat/v3/WechatMarketingApi.java | 2 +- 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/oauth2/OAuth2Exchange.java diff --git a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/oauth2/OAuth2AuthorizationRequestRedirectProvider.java b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/oauth2/OAuth2AuthorizationRequestRedirectProvider.java index 212d713..32f1d28 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/oauth2/OAuth2AuthorizationRequestRedirectProvider.java +++ b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/oauth2/OAuth2AuthorizationRequestRedirectProvider.java @@ -2,8 +2,9 @@ package com.enongm.dianji.payment.wechat.oauth2; import com.enongm.dianji.payment.PayException; +import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.PropertyNamingStrategy; import lombok.SneakyThrows; import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; @@ -21,6 +22,9 @@ import java.util.Objects; /** * OAuth2 获取用户的公众号授权openid. + * 1.需要微信公众号服务号 + * 2.需要微信公众号服务号绑定微信开放平台 + * 3.需要正确设置授权回调 * * @author Dax * @since 11 :39 @@ -29,7 +33,7 @@ public class OAuth2AuthorizationRequestRedirectProvider { private static final String AUTHORIZATION_URI = "https://open.weixin.qq.com/connect/oauth2/authorize"; private static final String TOKEN_URI = "https://api.weixin.qq.com/sns/oauth2/access_token"; private final RestOperations restOperations = new RestTemplate(); - private final ObjectMapper objectMapper = new ObjectMapper(); + private final ObjectMapper objectMapper; private final String appId; private final String secret; @@ -40,6 +44,9 @@ public class OAuth2AuthorizationRequestRedirectProvider { * @param secret the secret */ public OAuth2AuthorizationRequestRedirectProvider(String appId, String secret) { + this.objectMapper = new ObjectMapper(); + objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); this.appId = appId; this.secret = secret; } @@ -72,7 +79,7 @@ public class OAuth2AuthorizationRequestRedirectProvider { * @return the string */ @SneakyThrows - public ObjectNode openId(String code, String state) { + public OAuth2Exchange exchange(String code, String state) { Assert.hasText(code, "wechat pay oauth2 code is required"); Assert.hasText(state, "wechat pay oauth2 state is required"); MultiValueMap queryParams = new LinkedMultiValueMap<>(); @@ -91,7 +98,7 @@ public class OAuth2AuthorizationRequestRedirectProvider { String body = responseEntity.getBody(); if (Objects.nonNull(body)) { - return objectMapper.readValue(body, ObjectNode.class); + return objectMapper.readValue(body, OAuth2Exchange.class); } throw new PayException("Wechat OAuth2 Authorization failed"); } diff --git a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/oauth2/OAuth2Exchange.java b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/oauth2/OAuth2Exchange.java new file mode 100644 index 0000000..6eff506 --- /dev/null +++ b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/oauth2/OAuth2Exchange.java @@ -0,0 +1,17 @@ +package com.enongm.dianji.payment.wechat.oauth2; + +import lombok.Data; + +/** + * OAuth2 Exchange + * @author Dax + * @since 13:14 + */ +@Data +public class OAuth2Exchange { + private String accessToken; + private String refreshToken; + private Long expiresIn; + private String openid; + private String scope; +} diff --git a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatMarketingApi.java b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatMarketingApi.java index 7e2c5f7..1b723d4 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatMarketingApi.java +++ b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/WechatMarketingApi.java @@ -218,7 +218,7 @@ public class WechatMarketingApi extends AbstractApi { * @param params the params * @return the wechat response entity */ - public WechatResponseEntity sendStocks(StocksSendParams params) { + public WechatResponseEntity sendStock(StocksSendParams params) { WechatResponseEntity wechatResponseEntity = new WechatResponseEntity<>(); this.getWechatPayClient().withType(WechatPayV3Type.MARKETING_FAVOR_USERS_COUPONS, params) .function(this::sendStocksFunction)