|
@@ -1,14 +1,13 @@
|
|
|
package com.zanxiang.sdk.service.pay;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
import com.zanxiang.common.domain.ResultMap;
|
|
|
import com.zanxiang.common.enums.HttpStatusEnum;
|
|
|
import com.zanxiang.common.exception.BaseException;
|
|
|
import com.zanxiang.common.exception.CustomException;
|
|
|
import com.zanxiang.common.utils.StringUtils;
|
|
|
-import com.zanxiang.common.utils.URIUtil;
|
|
|
import com.zanxiang.module.util.JsonUtil;
|
|
|
import com.zanxiang.sdk.constant.MiPayConstants;
|
|
|
+import com.zanxiang.sdk.constant.RedisKeyConstant;
|
|
|
import com.zanxiang.sdk.domain.bo.ProductPayParamBO;
|
|
|
import com.zanxiang.sdk.domain.dto.GameAppletDTO;
|
|
|
import com.zanxiang.sdk.domain.dto.MiPayConfigDTO;
|
|
@@ -17,15 +16,19 @@ import com.zanxiang.sdk.domain.dto.UserDTO;
|
|
|
import com.zanxiang.sdk.service.GameAppletService;
|
|
|
import com.zanxiang.sdk.service.OrderService;
|
|
|
import com.zanxiang.sdk.service.UserService;
|
|
|
+import com.zanxiang.sdk.util.RedisUtil;
|
|
|
import com.zanxiang.sdk.util.WxPayUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.logging.log4j.util.Strings;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.net.URI;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
@@ -46,21 +49,19 @@ public class MiPayService extends PayBaseService {
|
|
|
@Autowired
|
|
|
private GameAppletService gameAppletService;
|
|
|
|
|
|
- @Value("${spring.profiles.active}")
|
|
|
- private String springActive;
|
|
|
-
|
|
|
@Autowired
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
@Autowired
|
|
|
private UserService userService;
|
|
|
|
|
|
- /**
|
|
|
- * 米大师支付配置
|
|
|
- */
|
|
|
- private MiPayConfigDTO config;
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil<String> redisUtil;
|
|
|
+
|
|
|
+ @Value("${payConfig.miPay.isSand}")
|
|
|
+ private Integer isSand;
|
|
|
|
|
|
- private int isSand = 1;
|
|
|
+ private MiPayConfigDTO config;
|
|
|
|
|
|
@Override
|
|
|
public ResultMap create(ProductPayParamBO product) {
|
|
@@ -72,7 +73,6 @@ public class MiPayService extends PayBaseService {
|
|
|
if (orderInfo.getGameId() == null || orderInfo.getGameId() == 0) {
|
|
|
throw new CustomException(HttpStatusEnum.ORDER_GAME_ID_IS_NULL);
|
|
|
}
|
|
|
- this.isSand = Objects.equals(this.springActive, "dev") ? 1 : 0;
|
|
|
this.configInit(product.getGameId());
|
|
|
//获取用户openId
|
|
|
UserDTO userDTO = userService.getUserByUserId(product.getUserId());
|
|
@@ -110,16 +110,7 @@ public class MiPayService extends PayBaseService {
|
|
|
paramMap.put("ts", String.valueOf(System.currentTimeMillis() / 1000));
|
|
|
paramMap.put("zone_id", "1");
|
|
|
paramMap.put("pf", "android");
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- String urlPath = this.isSand == 1 ? "/cgi-bin/midas/sandbox/getbalance" : "/cgi-bin/midas/getbalance";
|
|
|
- String appKey = this.isSand == 1 ? config.getAppKeyDev() : config.getAppKey();
|
|
|
- String sig = WxPayUtil.miPaySin("POST", urlPath, paramMap, appKey);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- paramMap.put("sig", this.sign("POST", paramMap));
|
|
|
+ paramMap.put("sig", this.sign(MiPayConstants.BALANCE_URL, paramMap));
|
|
|
Map<String, String> result = this.miPayApi(MiPayConstants.BALANCE_URL, paramMap);
|
|
|
return Long.valueOf(result.get("balance"));
|
|
|
}
|
|
@@ -134,74 +125,80 @@ public class MiPayService extends PayBaseService {
|
|
|
paramMap.put("zone_id", "1");
|
|
|
paramMap.put("amt", orderInfo.getAmount().toString());
|
|
|
paramMap.put("bill_no", orderInfo.getOrderId());
|
|
|
- paramMap.put("sig", this.sign("POST", paramMap));
|
|
|
+ paramMap.put("sig", this.sign(MiPayConstants.PAY_URL, paramMap));
|
|
|
Map<String, String> result = this.miPayApi(MiPayConstants.PAY_URL, paramMap);
|
|
|
return result.get("bill_no");
|
|
|
}
|
|
|
|
|
|
- private String getAccessToken(String appId, String appSecret) {
|
|
|
-// String redisKey = RedisKeyConstant.WEIXIN_ACCESS_TOKEN + appId;
|
|
|
-// String cache = redisUtil.getCache(redisKey);
|
|
|
-// if (StringUtils.isNotEmpty(cache)) {
|
|
|
-// return cache;
|
|
|
-// }
|
|
|
- HashMap<String, String> param = new HashMap<>();
|
|
|
- param.put("grant_type", "client_credential");
|
|
|
- param.put("appid", "wxa8ede4e6c65fad30");
|
|
|
- param.put("secret", "a8df1c1146f89f06d07d1e494f845ede");
|
|
|
-
|
|
|
- String url = URIUtil.fillUrlParams(MiPayConstants.TOKEN_URL, param, Boolean.FALSE);
|
|
|
-
|
|
|
- String result = restTemplate.getForObject(url, String.class);
|
|
|
-
|
|
|
-
|
|
|
- if (StringUtils.isEmpty(result)) {
|
|
|
- return "";
|
|
|
+ private String getAccessToken(String appId, String appKey) {
|
|
|
+ String redisKey = RedisKeyConstant.MI_PAY_TOKEN + appId;
|
|
|
+ String cache = redisUtil.getCache(redisKey);
|
|
|
+ if (StringUtils.isNotEmpty(cache)) {
|
|
|
+ return cache;
|
|
|
}
|
|
|
- JSONObject data = JSONObject.parseObject(result);
|
|
|
- if (Objects.isNull(data)) {
|
|
|
- return "";
|
|
|
+ //请求url
|
|
|
+ URI uri = UriComponentsBuilder.fromHttpUrl(MiPayConstants.MI_PAY_HOST + MiPayConstants.TOKEN_URL)
|
|
|
+ .queryParam("appid", appId)
|
|
|
+ .queryParam("secret", appKey)
|
|
|
+ .queryParam("grant_type", "client_credential")
|
|
|
+ .build().toUri();
|
|
|
+ String result;
|
|
|
+ try {
|
|
|
+ result = restTemplate.getForObject(uri, String.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取米大师token异常, appId : {}, appKey : {}, e : {}", appId, appKey, e.getMessage());
|
|
|
+ throw new BaseException("获取米大师token异常");
|
|
|
}
|
|
|
- Object accessToken = data.get("access_token");
|
|
|
- if (Objects.isNull(accessToken)) {
|
|
|
- return "";
|
|
|
+ if (Strings.isEmpty(result)) {
|
|
|
+ log.error("获取米大师token失败, 返回结果为空, appId : {}, appKey : {}", appId, appKey);
|
|
|
+ throw new BaseException("获取米大师token失败, 返回结果为空");
|
|
|
}
|
|
|
- String token = accessToken.toString();
|
|
|
-// redisUtil.setCache(redisKey, token, Long.valueOf(data.get("expires_in").toString()));
|
|
|
- return token;
|
|
|
+ Map<String, String> data = JsonUtil.toMap(result, Map.class, String.class, String.class);
|
|
|
+ String accessToken = data.get("access_token");
|
|
|
+ String expiresIn = data.get("expires_in");
|
|
|
+ if (Strings.isBlank(accessToken) || Strings.isBlank(expiresIn)) {
|
|
|
+ log.error("获取米大师token失败, 返回结果参数不全, appId : {}, appKey : {}, data {}", appId, appKey, data);
|
|
|
+ throw new BaseException("获取米大师token失败, 返回结果参数不全");
|
|
|
+ }
|
|
|
+ redisUtil.setCache(redisKey, accessToken, Long.valueOf(data.get("expires_in")));
|
|
|
+ return accessToken;
|
|
|
}
|
|
|
|
|
|
private Map<String, String> miPayApi(String url, Map<String, String> paramMap) {
|
|
|
+ //使用沙箱时,沙箱地址替换
|
|
|
+ if (this.isSand == 1) {
|
|
|
+ url = url.replace("/cgi-bin/midas/", "/cgi-bin/midas/sandbox/");
|
|
|
+ }
|
|
|
+ String appKey = this.isSand == 1 ? config.getAppKeyDev() : config.getAppKey();
|
|
|
+ //接口凭证
|
|
|
+ String accessToken = getAccessToken(config.getAppId(), appKey);
|
|
|
+ if (StringUtils.isEmpty(accessToken)) {
|
|
|
+ throw new CustomException(HttpStatusEnum.ACCESS_TOKEN_CREATE_ERROR);
|
|
|
+ }
|
|
|
+ //接口url
|
|
|
+ URI uri = UriComponentsBuilder.fromHttpUrl(MiPayConstants.MI_PAY_HOST + url)
|
|
|
+ .queryParam("access_token", accessToken)
|
|
|
+ .build().toUri();
|
|
|
+ String result;
|
|
|
try {
|
|
|
- //使用沙箱时,沙箱地址替换
|
|
|
- String appKey = config.getAppKey();
|
|
|
- if (this.isSand == 1) {
|
|
|
- url = url.replace("/cgi-bin/midas/", "/cgi-bin/midas/sandbox/");
|
|
|
- appKey = config.getAppKeyDev();
|
|
|
- }
|
|
|
- String accessToken = getAccessToken(config.getAppId(), appKey);
|
|
|
- if (StringUtils.isEmpty(accessToken)) {
|
|
|
- throw new CustomException(HttpStatusEnum.ACCESS_TOKEN_CREATE_ERROR);
|
|
|
- }
|
|
|
-
|
|
|
- url += "?access_token=" + accessToken;
|
|
|
- String result = restTemplate.postForObject(url, paramMap, String.class);
|
|
|
- if (StringUtils.isEmpty(result)) {
|
|
|
- throw new CustomException(HttpStatusEnum.MDS_CONNECT_ERROR);
|
|
|
- }
|
|
|
- Map<String, String> data = JsonUtil.toMap(result, Map.class, String.class, String.class);
|
|
|
- if (Objects.isNull(data)) {
|
|
|
- throw new CustomException(HttpStatusEnum.MDS_CONNECT_ERROR);
|
|
|
- }
|
|
|
- if (!data.get("errcode").equals("0")) {
|
|
|
- String errmsg = data.get("errmsg");
|
|
|
- Integer errcode = Integer.valueOf(data.get("errcode"));
|
|
|
- throw new CustomException(errmsg, errcode);
|
|
|
- }
|
|
|
- return data;
|
|
|
+ result = restTemplate.postForObject(uri, paramMap, String.class);
|
|
|
} catch (Exception e) {
|
|
|
+ log.error("米大师接口调用失败, url : {}, paramMap : {}, e : {}", url, JsonUtil.toString(paramMap), e.getMessage());
|
|
|
throw new BaseException("米大师接口调用失败");
|
|
|
}
|
|
|
+ if (StringUtils.isEmpty(result)) {
|
|
|
+ log.error("米大师接口调用失败, 返回结果为空, url : {}, paramMap : {}", url, JsonUtil.toString(paramMap));
|
|
|
+ throw new BaseException("米大师接口调用失败, 返回结果为空");
|
|
|
+ }
|
|
|
+ Map<String, String> data = JsonUtil.toMap(result, Map.class, String.class, String.class);
|
|
|
+ //返回结果成功
|
|
|
+ if (Objects.equals("0", data.get("errcode"))) {
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+ //返回结果失败
|
|
|
+ log.error("米大师接口调用返回结果失败, url : {}, paramMap : {}, errcode : {}, errmsg : {}", url, paramMap,
|
|
|
+ data.get("errcode"), data.get("errmsg"));
|
|
|
+ throw new BaseException("米大师接口调用返回结果失败");
|
|
|
}
|
|
|
|
|
|
private void configInit(Long gameId) {
|
|
@@ -216,10 +213,14 @@ public class MiPayService extends PayBaseService {
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
- private String sign(String method, Map<String, String> paramMap) {
|
|
|
- String urlPath = this.isSand == 1 ? "/cgi-bin/midas/sandbox/getbalance" : "/cgi-bin/midas/getbalance";
|
|
|
+ private String sign(String url, Map<String, String> paramMap) {
|
|
|
+ //沙箱环境地址替换
|
|
|
+ if (this.isSand == 1) {
|
|
|
+ url = url.replace("/cgi-bin/midas/", "/cgi-bin/midas/sandbox/");
|
|
|
+ }
|
|
|
String appKey = this.isSand == 1 ? config.getAppKeyDev() : config.getAppKey();
|
|
|
- return WxPayUtil.miPaySin(method, urlPath, paramMap, appKey);
|
|
|
+ //签名
|
|
|
+ return WxPayUtil.miPaySin("POST", url, paramMap, appKey);
|
|
|
}
|
|
|
|
|
|
@Override
|