|
@@ -1,23 +1,22 @@
|
|
|
package com.zanxiang.sdk.service.pay;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import com.zanxiang.common.domain.MiPayConfig;
|
|
|
import com.zanxiang.common.domain.ResultMap;
|
|
|
import com.zanxiang.common.enums.HttpStatusEnum;
|
|
|
-import com.zanxiang.common.enums.StatusEnum;
|
|
|
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.mybatis.entity.GamePayWay;
|
|
|
import com.zanxiang.sdk.constant.MiPayConstants;
|
|
|
import com.zanxiang.sdk.domain.bo.ProductPayParamBO;
|
|
|
+import com.zanxiang.sdk.domain.dto.GameAppletDTO;
|
|
|
+import com.zanxiang.sdk.domain.dto.MiPayConfigDTO;
|
|
|
import com.zanxiang.sdk.domain.dto.PlatformOrderDTO;
|
|
|
-import com.zanxiang.sdk.service.GamePayWayService;
|
|
|
+import com.zanxiang.sdk.domain.dto.UserDTO;
|
|
|
+import com.zanxiang.sdk.service.GameAppletService;
|
|
|
import com.zanxiang.sdk.service.OrderService;
|
|
|
-import com.zanxiang.sdk.service.api.WxApiService;
|
|
|
+import com.zanxiang.sdk.service.UserService;
|
|
|
import com.zanxiang.sdk.util.WxPayUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -41,14 +40,11 @@ import java.util.Objects;
|
|
|
@Service
|
|
|
public class MiPayService extends PayBaseService {
|
|
|
|
|
|
- @Autowired
|
|
|
- private WxApiService wxApiService;
|
|
|
-
|
|
|
@Autowired
|
|
|
private OrderService orderService;
|
|
|
|
|
|
@Autowired
|
|
|
- private GamePayWayService gamePayWayService;
|
|
|
+ private GameAppletService gameAppletService;
|
|
|
|
|
|
@Value("${spring.profiles.active}")
|
|
|
private String springActive;
|
|
@@ -56,13 +52,18 @@ public class MiPayService extends PayBaseService {
|
|
|
@Autowired
|
|
|
private RestTemplate restTemplate;
|
|
|
|
|
|
- private String code;
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 米大师支付配置
|
|
|
+ */
|
|
|
+ private MiPayConfigDTO config;
|
|
|
|
|
|
private int isSand = 1;
|
|
|
|
|
|
@Override
|
|
|
public ResultMap create(ProductPayParamBO product) {
|
|
|
- this.code = product.getCode();
|
|
|
//查询订单
|
|
|
PlatformOrderDTO orderInfo = orderService.info(product.getOutTradeNo());
|
|
|
if (Objects.isNull(orderInfo)) {
|
|
@@ -72,27 +73,17 @@ public class MiPayService extends PayBaseService {
|
|
|
throw new CustomException(HttpStatusEnum.ORDER_GAME_ID_IS_NULL);
|
|
|
}
|
|
|
this.isSand = Objects.equals(this.springActive, "dev") ? 1 : 0;
|
|
|
- //查询游戏配置
|
|
|
- GamePayWay one = gamePayWayService.getOne(new LambdaQueryWrapper<GamePayWay>()
|
|
|
- .eq(GamePayWay::getGameId, orderInfo.getGameId())
|
|
|
- .eq(GamePayWay::getStatus, StatusEnum.YES.getCode())
|
|
|
- .gt(GamePayWay::getPayBoxId, 0)
|
|
|
- .last("limit 1")
|
|
|
- );
|
|
|
- if (Objects.isNull(one) || StringUtils.isEmpty(one.getPayConfig())) {
|
|
|
- throw new CustomException(HttpStatusEnum.GET_CONFIG_ERROR);
|
|
|
- }
|
|
|
- //米大师支付配置
|
|
|
- MiPayConfig miPayConfig = JSONObject.parseObject(one.getPayConfig(), MiPayConfig.class);
|
|
|
+ this.configInit(product.getGameId());
|
|
|
//获取用户openId
|
|
|
- String openId = wxApiService.getAppletOpenId(code, miPayConfig.getWxAppId(), "a8df1c1146f89f06d07d1e494f845ede");
|
|
|
+ UserDTO userDTO = userService.getUserByUserId(product.getUserId());
|
|
|
+ String openId = userDTO.getOpenId();
|
|
|
//获取米大师钱包余额
|
|
|
- Long balance = this.midasGetBalance(openId, miPayConfig);
|
|
|
+ Long balance = this.midasGetBalance(openId);
|
|
|
//返回参数
|
|
|
Map<String, String> result = new HashMap<>();
|
|
|
//余额充足, 直接扣除
|
|
|
if (balance >= orderInfo.getAmount().longValue()) {
|
|
|
- String billNo = this.midasPay(openId, miPayConfig, orderInfo);
|
|
|
+ String billNo = this.midasPay(openId, orderInfo);
|
|
|
result.put("status", "0");
|
|
|
result.put("billNo", billNo);
|
|
|
}
|
|
@@ -101,7 +92,7 @@ public class MiPayService extends PayBaseService {
|
|
|
result.put("currencyType", "CNY");
|
|
|
result.put("mode", "game");
|
|
|
result.put("zoneId", "1");
|
|
|
- result.put("offerId", miPayConfig.getAppId());
|
|
|
+ result.put("offerId", config.getAppId());
|
|
|
result.put("env", String.valueOf(this.isSand));
|
|
|
result.put("buyQuantity", orderInfo.getAmount().toString());
|
|
|
result.put("status", "1");
|
|
@@ -111,37 +102,40 @@ public class MiPayService extends PayBaseService {
|
|
|
return ResultMap.ok(result);
|
|
|
}
|
|
|
|
|
|
- private Long midasGetBalance(String openId, MiPayConfig miPayConfig) {
|
|
|
+ private Long midasGetBalance(String openId) {
|
|
|
Map<String, String> paramMap = new HashMap<>();
|
|
|
paramMap.put("openid", openId);
|
|
|
- paramMap.put("appid", miPayConfig.getWxAppId());
|
|
|
- paramMap.put("offer_id", miPayConfig.getAppId());
|
|
|
+ paramMap.put("appid", config.getWxAppId());
|
|
|
+ paramMap.put("offer_id", config.getAppId());
|
|
|
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 ? miPayConfig.getAppKeyDev() : miPayConfig.getAppKey();
|
|
|
+ String appKey = this.isSand == 1 ? config.getAppKeyDev() : config.getAppKey();
|
|
|
String sig = WxPayUtil.miPaySin("POST", urlPath, paramMap, appKey);
|
|
|
- paramMap.put("sig", sig);
|
|
|
- Map<String, String> result = this.miPayApi(MiPayConstants.BALANCE_URL, miPayConfig, paramMap);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ paramMap.put("sig", this.sign("POST", paramMap));
|
|
|
+ Map<String, String> result = this.miPayApi(MiPayConstants.BALANCE_URL, paramMap);
|
|
|
return Long.valueOf(result.get("balance"));
|
|
|
}
|
|
|
|
|
|
- private String midasPay(String openId, MiPayConfig miPayConfig, PlatformOrderDTO orderInfo) {
|
|
|
+ private String midasPay(String openId, PlatformOrderDTO orderInfo) {
|
|
|
Map<String, String> paramMap = new HashMap<>();
|
|
|
- paramMap.put("appid", miPayConfig.getWxAppId());
|
|
|
+ paramMap.put("appid", config.getWxAppId());
|
|
|
paramMap.put("openid", openId);
|
|
|
- paramMap.put("offer_id", miPayConfig.getAppId());
|
|
|
+ paramMap.put("offer_id", config.getAppId());
|
|
|
paramMap.put("ts", String.valueOf(System.currentTimeMillis() / 1000));
|
|
|
paramMap.put("pf", "android");
|
|
|
paramMap.put("zone_id", "1");
|
|
|
paramMap.put("amt", orderInfo.getAmount().toString());
|
|
|
paramMap.put("bill_no", orderInfo.getOrderId());
|
|
|
- String urlPath = this.isSand == 1 ? "/cgi-bin/midas/sandbox/getbalance" : "/cgi-bin/midas/getbalance";
|
|
|
- String appKey = this.isSand == 1 ? miPayConfig.getAppKeyDev() : miPayConfig.getAppKey();
|
|
|
- String sig = WxPayUtil.miPaySin("POST", urlPath, paramMap, appKey);
|
|
|
- paramMap.put("sig", sig);
|
|
|
- Map<String, String> result = this.miPayApi(MiPayConstants.PAY_URL, miPayConfig, paramMap);
|
|
|
+ paramMap.put("sig", this.sign("POST", paramMap));
|
|
|
+ Map<String, String> result = this.miPayApi(MiPayConstants.PAY_URL, paramMap);
|
|
|
return result.get("bill_no");
|
|
|
}
|
|
|
|
|
@@ -177,15 +171,15 @@ public class MiPayService extends PayBaseService {
|
|
|
return token;
|
|
|
}
|
|
|
|
|
|
- private Map<String, String> miPayApi(String url, MiPayConfig miPayConfig, Map<String, String> paramMap) {
|
|
|
+ private Map<String, String> miPayApi(String url, Map<String, String> paramMap) {
|
|
|
try {
|
|
|
//使用沙箱时,沙箱地址替换
|
|
|
- String appKey = miPayConfig.getAppKey();
|
|
|
+ String appKey = config.getAppKey();
|
|
|
if (this.isSand == 1) {
|
|
|
url = url.replace("/cgi-bin/midas/", "/cgi-bin/midas/sandbox/");
|
|
|
- appKey = miPayConfig.getAppKeyDev();
|
|
|
+ appKey = config.getAppKeyDev();
|
|
|
}
|
|
|
- String accessToken = getAccessToken(miPayConfig.getAppId(), appKey);
|
|
|
+ String accessToken = getAccessToken(config.getAppId(), appKey);
|
|
|
if (StringUtils.isEmpty(accessToken)) {
|
|
|
throw new CustomException(HttpStatusEnum.ACCESS_TOKEN_CREATE_ERROR);
|
|
|
}
|
|
@@ -210,6 +204,24 @@ public class MiPayService extends PayBaseService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void configInit(Long gameId) {
|
|
|
+ //查询游戏小程序信息
|
|
|
+ GameAppletDTO gameAppletDTO = gameAppletService.getByGameId(gameId);
|
|
|
+ //支付配置赋值
|
|
|
+ this.config = MiPayConfigDTO.builder()
|
|
|
+ .wxAppId(gameAppletDTO.getAppId())
|
|
|
+ .appId(gameAppletDTO.getMiPayAppId())
|
|
|
+ .appKey(gameAppletDTO.getMiPayAppKey())
|
|
|
+ .appKeyDev(gameAppletDTO.getMiPayAppKeyDev())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String sign(String method, Map<String, String> paramMap) {
|
|
|
+ String urlPath = this.isSand == 1 ? "/cgi-bin/midas/sandbox/getbalance" : "/cgi-bin/midas/getbalance";
|
|
|
+ String appKey = this.isSand == 1 ? config.getAppKeyDev() : config.getAppKey();
|
|
|
+ return WxPayUtil.miPaySin(method, urlPath, paramMap, appKey);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public String notify(HttpServletRequest request, HttpServletResponse response) {
|
|
|
throw new BaseException("米大师支付不存在异步回调");
|