|
@@ -13,6 +13,7 @@ import com.github.binarywang.wxpay.service.WxPayService;
|
|
|
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
|
|
|
import com.github.binarywang.wxpay.util.SignUtils;
|
|
|
import com.hys.app.framework.database.annotation.Id;
|
|
|
+import com.hys.app.framework.exception.ServiceException;
|
|
|
import com.hys.app.framework.util.JsonUtil;
|
|
|
import com.hys.app.pay.ali.PayOrder;
|
|
|
import org.springframework.http.HttpStatus;
|
|
@@ -22,13 +23,13 @@ import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
-import java.net.HttpURLConnection;
|
|
|
-import java.net.InetAddress;
|
|
|
-import java.net.URL;
|
|
|
-import java.net.UnknownHostException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Base64;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedHashMap;
|
|
@@ -57,6 +58,7 @@ public class WxPayServiceMy {
|
|
|
|
|
|
WxPayUnifiedOrderRequest orderRequest = WxPayUnifiedOrderRequest.newBuilder()
|
|
|
.body(payOrder.getSubject())
|
|
|
+ .openid(payOrder.getOpenId())
|
|
|
.outTradeNo(payOrder.getTraceNo())
|
|
|
.totalFee((int) Math.round(payOrder.getTotalAmount() * 100)) // 单位是分
|
|
|
.spbillCreateIp(InetAddress.getLocalHost().getHostAddress())
|
|
@@ -83,55 +85,35 @@ public class WxPayServiceMy {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static String getAccessToken(String appId, String secret) {
|
|
|
- String accessToken = "";
|
|
|
- try {
|
|
|
- String authString = appId + ":" + secret;
|
|
|
- byte[] authEncBytes = Base64.getEncoder().encode(authString.getBytes());
|
|
|
- String authStringEnc = new String(authEncBytes);
|
|
|
-
|
|
|
- URL url = new URL("https://api.weixin.qq.com/sns/oauth2/access_token");
|
|
|
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
- connection.setRequestMethod("POST");
|
|
|
- connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
|
|
|
-
|
|
|
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
- String inputLine;
|
|
|
- StringBuffer response = new StringBuffer();
|
|
|
-
|
|
|
- while ((inputLine = in.readLine()) != null) {
|
|
|
- response.append(inputLine);
|
|
|
- }
|
|
|
- in.close();
|
|
|
-
|
|
|
- accessToken = response.toString();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
+ public String getOpenIdCode(Long orderId) throws UnsupportedEncodingException {
|
|
|
+ return String.format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect",
|
|
|
+ wxConfig.getAppId(), URLEncoder.encode(wxConfig.getOAuth2Url(), "UTF-8"), "snsapi_base", orderId);
|
|
|
|
|
|
- return accessToken;
|
|
|
}
|
|
|
|
|
|
- public static String getOpenId(String accessToken) {
|
|
|
- String openId = "";
|
|
|
+ public String getOpenId(String code) {
|
|
|
+
|
|
|
+ String urlString = String.format(
|
|
|
+ "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
|
|
|
+ wxConfig.getAppId(), wxConfig.getSecret(), code);
|
|
|
try {
|
|
|
- URL url = new URL("https://api.weixin.qq.com/sns/oauth2/access_token"+accessToken);
|
|
|
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ URL url = new URL(urlString);
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
connection.setRequestMethod("GET");
|
|
|
|
|
|
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
|
+ StringBuilder response = new StringBuilder();
|
|
|
String inputLine;
|
|
|
- StringBuffer response = new StringBuffer();
|
|
|
|
|
|
while ((inputLine = in.readLine()) != null) {
|
|
|
response.append(inputLine);
|
|
|
}
|
|
|
in.close();
|
|
|
- LinkedHashMap<String, Object> map = JsonUtil.toMap(response.toString());
|
|
|
- openId = map.get("openid").toString();
|
|
|
+ Map<String, Object> map = JsonUtil.toMap(response.toString());
|
|
|
+ return map.get("openid").toString();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
+ return null; // 或者处理错误
|
|
|
}
|
|
|
- return openId;
|
|
|
}
|
|
|
}
|