From ea4d8b17b04ce905a341c4f82496d19d08d2503c Mon Sep 17 00:00:00 2001 From: xiafang Date: Tue, 1 Dec 2020 11:58:55 +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 --- payment-spring-boot-autoconfigure/pom.xml | 4 ++-- ...2AuthorizationRequestRedirectProvider.java | 24 +++++++++++-------- .../dianji/payment/wechat/v3/AbstractApi.java | 3 +-- payment-spring-boot-starter/pom.xml | 4 ++-- pom.xml | 2 +- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/payment-spring-boot-autoconfigure/pom.xml b/payment-spring-boot-autoconfigure/pom.xml index 6273bb4..d474ff0 100644 --- a/payment-spring-boot-autoconfigure/pom.xml +++ b/payment-spring-boot-autoconfigure/pom.xml @@ -5,7 +5,7 @@ com.enongm.dianji payment-spring-boot-autoconfigure - 1.0.1.RELEASE + 1.0.0.RELEASE @@ -38,7 +38,7 @@ com.enongm.dianji payment-spring-boot - 1.0.1.RELEASE + 1.0.0.RELEASE pom import 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 ee015f6..212d713 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,7 +2,7 @@ package com.enongm.dianji.payment.wechat.oauth2; import com.enongm.dianji.payment.PayException; -import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import lombok.SneakyThrows; import org.springframework.http.RequestEntity; @@ -29,6 +29,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 String appId; private final String secret; @@ -46,12 +47,12 @@ public class OAuth2AuthorizationRequestRedirectProvider { /** * 拼接微信公众号授权的url,用以触发用户点击跳转微信授权. * - * @param phoneNumber the phone number + * @param state the state * @param redirectUri the redirect uri * @return uri components */ @SneakyThrows - public String redirect(String phoneNumber, String redirectUri) { + public String redirect(String state, String redirectUri) { Assert.hasText(redirectUri, "redirectUri is required"); String encode = URLEncoder.encode(redirectUri, StandardCharsets.UTF_8.name()); MultiValueMap queryParams = new LinkedMultiValueMap<>(); @@ -59,7 +60,7 @@ public class OAuth2AuthorizationRequestRedirectProvider { queryParams.add("redirect_uri", encode); queryParams.add("response_type", "code"); queryParams.add("scope", "snsapi_base"); - queryParams.add("state", phoneNumber); + queryParams.add("state", state); return UriComponentsBuilder.fromHttpUrl(AUTHORIZATION_URI).queryParams(queryParams).build().toUriString() + "#wechat_redirect"; } @@ -70,12 +71,15 @@ public class OAuth2AuthorizationRequestRedirectProvider { * @param code the code * @return the string */ - public String openId(String code) { + @SneakyThrows + public ObjectNode openId(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<>(); queryParams.add("appid", appId); queryParams.add("secret", secret); queryParams.add("code", code); + queryParams.add("state", state); queryParams.add("grant_type", "authorization_code"); URI uri = UriComponentsBuilder.fromHttpUrl(TOKEN_URI) .queryParams(queryParams) @@ -83,13 +87,13 @@ public class OAuth2AuthorizationRequestRedirectProvider { .toUri(); RequestEntity requestEntity = RequestEntity.get(uri).build(); - ResponseEntity responseEntity = restOperations.exchange(requestEntity, ObjectNode.class); - ObjectNode body = responseEntity.getBody(); + ResponseEntity responseEntity = restOperations.exchange(requestEntity, String.class); + + String body = responseEntity.getBody(); if (Objects.nonNull(body)) { - JsonNode openid = body.get("openid"); - return openid.asText(); + return objectMapper.readValue(body, ObjectNode.class); } - throw new PayException("OpenId Not Available"); + throw new PayException("Wechat OAuth2 Authorization failed"); } diff --git a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/AbstractApi.java b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/AbstractApi.java index 4e9d9ea..9d55044 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/AbstractApi.java +++ b/payment-spring-boot-autoconfigure/src/main/java/com/enongm/dianji/payment/wechat/v3/AbstractApi.java @@ -44,8 +44,7 @@ public abstract class AbstractApi { return RequestEntity.post(uri) .body(mapper.writeValueAsString(params)); } catch (JsonProcessingException e) { - e.printStackTrace(); + throw new PayException("wechat app pay json failed"); } - throw new PayException("wechat app pay json failed"); } } diff --git a/payment-spring-boot-starter/pom.xml b/payment-spring-boot-starter/pom.xml index 662ae10..29453fc 100644 --- a/payment-spring-boot-starter/pom.xml +++ b/payment-spring-boot-starter/pom.xml @@ -5,7 +5,7 @@ com.enongm.dianji payment-spring-boot-starter - 1.0.1.RELEASE + 1.0.0.RELEASE jar 4.0.0 @@ -26,7 +26,7 @@ com.enongm.dianji payment-spring-boot - 1.0.1.RELEASE + 1.0.0.RELEASE pom import diff --git a/pom.xml b/pom.xml index e7bd5de..838249e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> com.enongm.dianji payment-spring-boot - 1.0.1.RELEASE + 1.0.0.RELEASE pom 4.0.0