|
@@ -0,0 +1,211 @@
|
|
|
|
+package com.zanxiang.sdk.service.Impl;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
+import com.zanxiang.common.domain.MiPayConfig;
|
|
|
|
+import com.zanxiang.common.enums.HttpStatusEnum;
|
|
|
|
+import com.zanxiang.common.enums.StatusEnum;
|
|
|
|
+import com.zanxiang.common.exception.CustomException;
|
|
|
|
+import com.zanxiang.common.utils.StringUtils;
|
|
|
|
+import com.zanxiang.mybatis.entity.GamePayWay;
|
|
|
|
+import com.zanxiang.mybatis.entity.PayApplication;
|
|
|
|
+import com.zanxiang.mybatis.entity.PayBox;
|
|
|
|
+import com.zanxiang.mybatis.mapper.PayApplicationMapper;
|
|
|
|
+import com.zanxiang.mybatis.mapper.PayBoxMapper;
|
|
|
|
+import com.zanxiang.sdk.common.constant.RedisKeyConstant;
|
|
|
|
+import com.zanxiang.sdk.common.miPay.MiPayClient;
|
|
|
|
+import com.zanxiang.sdk.common.miPay.RequestParam;
|
|
|
|
+import com.zanxiang.sdk.common.miPay.UrlConstants;
|
|
|
|
+import com.zanxiang.sdk.common.util.RedisUtil;
|
|
|
|
+import com.zanxiang.sdk.domain.dto.PlatformOrderDTO;
|
|
|
|
+import com.zanxiang.sdk.service.GamePayWayService;
|
|
|
|
+import com.zanxiang.sdk.service.MiPayService;
|
|
|
|
+import com.zanxiang.sdk.service.PlatformOrderService;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Objects;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 米大师服务实现
|
|
|
|
+ *
|
|
|
|
+ * @author xufeng
|
|
|
|
+ * @date 2022/7/14 16:33
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+@Slf4j
|
|
|
|
+public class MiPayServiceImpl implements MiPayService {
|
|
|
|
+ protected final Logger logger = LoggerFactory.getLogger(MiPayServiceImpl.class);
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisUtil<String> redisUtil;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private PlatformOrderService platformOrderService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private GamePayWayService gamePayWayService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private MiPayClient miPayClient;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private PayApplicationMapper payApplicationMapper;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private PayBoxMapper payBoxMapper;
|
|
|
|
+
|
|
|
|
+ @Value("${spring.profiles.active}")
|
|
|
|
+ private String springActive;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Boolean midasPay(Long userId, String orderId) {
|
|
|
|
+ PlatformOrderDTO orderInfo = platformOrderService.info(orderId);
|
|
|
|
+ if (Objects.isNull(orderInfo)) {
|
|
|
|
+ throw new CustomException(HttpStatusEnum.ORDER_NO_FIND);
|
|
|
|
+ }
|
|
|
|
+ if (orderInfo.getGameId() == null || orderInfo.getGameId() == 0) {
|
|
|
|
+ throw new CustomException(HttpStatusEnum.ORDER_GAME_ID_IS_NULL);
|
|
|
|
+ }
|
|
|
|
+ RequestParam commonParam = getCommonParam(orderInfo.getGameId(), orderInfo.getUserId());
|
|
|
|
+ commonParam.setBill_no(orderId);
|
|
|
|
+ HashMap<String, String> result = miPayClient.api(UrlConstants.PAY_URL, commonParam, null, null, null);
|
|
|
|
+ if (Objects.equals(HttpStatusEnum.SUCCESS.getCode().toString(), result.get("code"))) {
|
|
|
|
+ //成功
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Boolean midasCancelPay(Long userId, String orderId) {
|
|
|
|
+ PlatformOrderDTO orderInfo = platformOrderService.info(orderId);
|
|
|
|
+ if (Objects.isNull(orderInfo)) {
|
|
|
|
+ throw new CustomException(HttpStatusEnum.ORDER_NO_FIND);
|
|
|
|
+ }
|
|
|
|
+ if (orderInfo.getGameId() == null || orderInfo.getGameId() == 0) {
|
|
|
|
+ throw new CustomException(HttpStatusEnum.ORDER_GAME_ID_IS_NULL);
|
|
|
|
+ }
|
|
|
|
+ RequestParam commonParam = getCommonParam(orderInfo.getGameId(), orderInfo.getUserId());
|
|
|
|
+ commonParam.setBill_no(orderId);
|
|
|
|
+ HashMap<String, String> result = miPayClient.api(UrlConstants.CANCEL_URL, commonParam, null, null, null);
|
|
|
|
+ if (Objects.equals(HttpStatusEnum.SUCCESS.getCode().toString(), result.get("code"))) {
|
|
|
|
+ //成功
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Boolean midasGetBalance(Long userId, String orderId) {
|
|
|
|
+ PlatformOrderDTO orderInfo = platformOrderService.info(orderId);
|
|
|
|
+ if (Objects.isNull(orderInfo)) {
|
|
|
|
+ throw new CustomException(HttpStatusEnum.ORDER_NO_FIND);
|
|
|
|
+ }
|
|
|
|
+ if (orderInfo.getGameId() == null || orderInfo.getGameId() == 0) {
|
|
|
|
+ throw new CustomException(HttpStatusEnum.ORDER_GAME_ID_IS_NULL);
|
|
|
|
+ }
|
|
|
|
+ RequestParam commonParam = getCommonParam(orderInfo.getGameId(), orderInfo.getUserId());
|
|
|
|
+ commonParam.setBill_no(orderId);
|
|
|
|
+ commonParam.setPresent_counts(orderInfo.getProductCnt());
|
|
|
|
+ HashMap<String, String> result = miPayClient.api(UrlConstants.BALANCE_URL, commonParam, null, null, null);
|
|
|
|
+ if (Objects.equals(HttpStatusEnum.SUCCESS.getCode().toString(), result.get("code"))) {
|
|
|
|
+ //成功
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Boolean midasPresent(Long userId, String orderId, Integer presentCounts) {
|
|
|
|
+ PlatformOrderDTO orderInfo = platformOrderService.info(orderId);
|
|
|
|
+ if (Objects.isNull(orderInfo)) {
|
|
|
|
+ throw new CustomException(HttpStatusEnum.ORDER_NO_FIND);
|
|
|
|
+ }
|
|
|
|
+ if (orderInfo.getGameId() == null || orderInfo.getGameId() == 0) {
|
|
|
|
+ throw new CustomException(HttpStatusEnum.ORDER_GAME_ID_IS_NULL);
|
|
|
|
+ }
|
|
|
|
+ RequestParam commonParam = getCommonParam(orderInfo.getGameId(), orderInfo.getUserId());
|
|
|
|
+ commonParam.setBill_no(orderId);
|
|
|
|
+ commonParam.setPresent_counts(orderInfo.getProductCnt());
|
|
|
|
+ HashMap<String, String> result = miPayClient.api(UrlConstants.PRESENT_URL, commonParam, null, null, null);
|
|
|
|
+ if (Objects.equals(HttpStatusEnum.SUCCESS.getCode().toString(), result.get("code"))) {
|
|
|
|
+ //成功
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public MiPayConfig midasConfig(Long gameId) {
|
|
|
|
+ GamePayWay one = gamePayWayService.getOne(new LambdaQueryWrapper<GamePayWay>()
|
|
|
|
+ .eq(GamePayWay::getGameId, gameId)
|
|
|
|
+ .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);
|
|
|
|
+ }
|
|
|
|
+ return JSONObject.parseObject(one.getPayConfig(), MiPayConfig.class);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 请求通用参数生成
|
|
|
|
+ *
|
|
|
|
+ * @param gameId 游戏id
|
|
|
|
+ * @param userId 玩家
|
|
|
|
+ * @return RequestParam 请求参数
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public RequestParam getCommonParam(Long gameId, String userId) {
|
|
|
|
+ GamePayWay one = gamePayWayService.getOne(new LambdaQueryWrapper<GamePayWay>()
|
|
|
|
+ .eq(GamePayWay::getGameId, gameId)
|
|
|
|
+ .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);
|
|
|
|
+ PayBox payBoxInfo = payBoxMapper.selectById(one.getPayBoxId());
|
|
|
|
+ if (Objects.isNull(payBoxInfo) || payBoxInfo.getPayApplicationId() == null || payBoxInfo.getPayApplicationId() <= 0) {
|
|
|
|
+ throw new CustomException(HttpStatusEnum.GET_PAY_BOX_ERROR);
|
|
|
|
+ }
|
|
|
|
+ PayApplication payApplicationInfo = payApplicationMapper.selectById(payBoxInfo.getPayApplicationId());
|
|
|
|
+ if (payApplicationInfo.getStatus() == StatusEnum.NO.getCode()) {
|
|
|
|
+ throw new CustomException(HttpStatusEnum.GET_PAY_BOX_ERROR);
|
|
|
|
+ }
|
|
|
|
+ String openId = redisUtil.getCache(RedisKeyConstant.WEIXIN_OPEN_ID + userId + "_" + gameId);
|
|
|
|
+ if (StringUtils.isEmpty(openId)) {
|
|
|
|
+ //todo 无openId情况
|
|
|
|
+ if (springActive.equals("dev")) {
|
|
|
|
+ openId = "1";
|
|
|
|
+ } else {
|
|
|
|
+ throw new CustomException(HttpStatusEnum.WEIXIN_OPEN_ID_NULL);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ RequestParam requestParam = new RequestParam();
|
|
|
|
+ requestParam.setOpenid(openId);
|
|
|
|
+ requestParam.setAppid(payApplicationInfo.getAppId());
|
|
|
|
+ requestParam.setOffer_id(miPayConfig.getAppId());//实际上是米大师应用id
|
|
|
|
+ requestParam.setApp_key(miPayConfig.getAppKey());
|
|
|
|
+ //沙箱时使用沙箱key
|
|
|
|
+ if (requestParam.getIs_sand() == 1) {
|
|
|
|
+ requestParam.setApp_key(miPayConfig.getAppKeyDev());
|
|
|
|
+ }
|
|
|
|
+ return requestParam;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|