mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-29 17:28:42 +08:00 
			
		
		
		
	优化渠道 config 校验和逻辑转换问题
This commit is contained in:
		| @@ -80,7 +80,7 @@ public class PayMerchantController { | ||||
|     @ApiImplicitParam(name = "name", value = "商户名称", required = true, example = "芋道", dataTypeClass = Long.class) | ||||
|     @PreAuthorize("@ss.hasPermission('pay:merchant:query')") | ||||
|     public CommonResult<List<PayMerchantRespVO>> getMerchantListByName(@RequestParam("name") String name) { | ||||
|         List<PayMerchantDO> merchantListDO = merchantService.getMerchantListByNameLimit(name); | ||||
|         List<PayMerchantDO> merchantListDO = merchantService.getMerchantListByName(name); | ||||
|         return success(PayMerchantConvert.INSTANCE.convertList(merchantListDO)); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -6,6 +6,7 @@ import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.merchant.PayMerch | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | ||||
| import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; | ||||
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||||
| import org.apache.ibatis.annotations.Mapper; | ||||
|  | ||||
| @@ -48,8 +49,6 @@ public interface PayMerchantMapper extends BaseMapperX<PayMerchantDO> { | ||||
|      * @return 商户集合 | ||||
|      */ | ||||
|     default List<PayMerchantDO> getMerchantListByName(String merchantName) { | ||||
|         // TODO @aquan:全模糊匹配,暂时不考虑索引的事;另外,可以直接 new Lambada 的 QueryWrapper 实现类呀 | ||||
|         return this.selectList(new QueryWrapper<PayMerchantDO>() | ||||
|                 .lambda().likeRight(PayMerchantDO::getName, merchantName)); | ||||
|         return this.selectList(new LambdaQueryWrapper<PayMerchantDO>().like(PayMerchantDO::getName, merchantName)); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -22,7 +22,7 @@ import org.springframework.validation.annotation.Validated; | ||||
| import javax.annotation.Resource; | ||||
| import java.util.*; | ||||
|  | ||||
| import static cn.iocoder.yudao.coreservice.modules.pay.enums.PayErrorCodeCoreConstants.APP_NOT_EXISTS; | ||||
| import static cn.iocoder.yudao.coreservice.modules.pay.enums.PayErrorCodeCoreConstants.*; | ||||
| import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||||
| import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | ||||
|  | ||||
| @@ -72,7 +72,7 @@ public class PayAppServiceImpl implements PayAppService { | ||||
|  | ||||
|     private void validateAppExists(Long id) { | ||||
|         if (appMapper.selectById(id) == null) { | ||||
|             throw exception(APP_NOT_EXISTS); | ||||
|             throw exception(PAY_APP_NOT_FOUND); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @@ -148,7 +148,7 @@ public class PayAppServiceImpl implements PayAppService { | ||||
|         } | ||||
|         PayAppDO payApp = appMapper.selectById(id); | ||||
|         if (payApp == null) { | ||||
|             throw exception(APP_NOT_EXISTS); | ||||
|             throw exception(PAY_APP_NOT_FOUND); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| package cn.iocoder.yudao.adminserver.modules.pay.service.channel.impl; | ||||
|  | ||||
| import cn.hutool.json.JSONUtil; | ||||
| import cn.iocoder.yudao.adminserver.modules.pay.controller.channel.vo.PayChannelCreateReqVO; | ||||
| import cn.iocoder.yudao.adminserver.modules.pay.controller.channel.vo.PayChannelExportReqVO; | ||||
| import cn.iocoder.yudao.adminserver.modules.pay.controller.channel.vo.PayChannelPageReqVO; | ||||
| @@ -9,24 +10,20 @@ import cn.iocoder.yudao.adminserver.modules.pay.dal.mysql.channel.PayChannelMapp | ||||
| import cn.iocoder.yudao.adminserver.modules.pay.service.channel.PayChannelService; | ||||
| import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.merchant.PayChannelDO; | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayPayClientConfig; | ||||
| import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; | ||||
| import cn.iocoder.yudao.framework.pay.core.client.impl.wx.WXPayClientConfig; | ||||
| import cn.iocoder.yudao.framework.pay.core.enums.PayChannelEnum; | ||||
| import com.alibaba.fastjson.JSON; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.beans.factory.annotation.Qualifier; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.util.Assert; | ||||
| import org.springframework.validation.annotation.Validated; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| import javax.validation.ConstraintViolation; | ||||
| import javax.validation.Validation; | ||||
| import javax.validation.Validator; | ||||
| import javax.validation.ValidatorFactory; | ||||
| import java.util.Collection; | ||||
| import java.util.List; | ||||
| import java.util.Set; | ||||
| import java.util.stream.Collectors; | ||||
|  | ||||
| import static cn.iocoder.yudao.coreservice.modules.pay.enums.PayErrorCodeCoreConstants.*; | ||||
| import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||||
| @@ -44,14 +41,15 @@ public class PayChannelServiceImpl implements PayChannelService { | ||||
|     @Resource | ||||
|     private PayChannelMapper channelMapper; | ||||
|  | ||||
|     @Resource | ||||
|     private Validator validator; | ||||
|  | ||||
|     @Override | ||||
|     public Long createChannel(PayChannelCreateReqVO reqVO) { | ||||
|         // TODO @aquan:感觉获得那一条比较合适。因为是有唯一性的。注释有错别字哈。 | ||||
|         // 判断是否有重复的有责无法新增 | ||||
|         Integer channelCount = this.getChannelCountByConditions(reqVO.getMerchantId(), reqVO.getAppId(), reqVO.getCode()); | ||||
|         if (channelCount > 0) { | ||||
|             throw exception(CHANNEL_EXIST_SAME_CHANNEL_ERROR); | ||||
|         } | ||||
|  | ||||
|         // 断言是否有重复的 | ||||
|         PayChannelDO channelDO = this.getChannelByConditions(reqVO.getMerchantId(), reqVO.getAppId(), reqVO.getCode()); | ||||
|         Assert.isNull(channelDO, CHANNEL_EXIST_SAME_CHANNEL_ERROR.getMsg()); | ||||
|  | ||||
|         // 新增渠道 | ||||
|         PayChannelDO channel = PayChannelConvert.INSTANCE.convert(reqVO); | ||||
| @@ -142,24 +140,6 @@ public class PayChannelServiceImpl implements PayChannelService { | ||||
|         return this.channelMapper.getChannelByConditions(merchantId, appid, code); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 检测微信秘钥参数 | ||||
|      * | ||||
|      * @param config 信秘钥参数 | ||||
|      */ | ||||
|     private void wechatParamCheck(WXPayClientConfig config) { | ||||
|         // 针对于 V2 或者 V3 版本的参数校验 | ||||
|         if (WXPayClientConfig.API_VERSION_V2.equals(config.getApiVersion())) { | ||||
|             Assert.notNull(config.getMchKey(), CHANNEL_WECHAT_VERSION_2_MCH_KEY_IS_NULL.getMsg()); | ||||
|         } | ||||
|         if (WXPayClientConfig.API_VERSION_V3.equals(config.getApiVersion())) { | ||||
|             Assert.notNull(config.getPrivateKeyContent(), CHANNEL_WECHAT_VERSION_3_PRIVATE_KEY_IS_NULL.getMsg()); | ||||
|             Assert.notNull(config.getPrivateCertContent(), CHANNEL_WECHAT_VERSION_3_CERT_KEY_IS_NULL.getMsg()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * 设置渠道配置以及参数校验 | ||||
|      * | ||||
| @@ -168,48 +148,11 @@ public class PayChannelServiceImpl implements PayChannelService { | ||||
|      */ | ||||
|     private void settingConfigAndCheckParam(PayChannelDO channel, String configStr) { | ||||
|         // 得到这个渠道是微信的还是支付宝的 | ||||
|         String channelType = PayChannelEnum.verifyWechatOrAliPay(channel.getCode()); | ||||
|         Assert.notNull(channelType, CHANNEL_NOT_EXISTS.getMsg()); | ||||
|  | ||||
|         // 进行验证 | ||||
|         // TODO @阿全:Spring 可以注入 Validator 哈 | ||||
|         ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); | ||||
|         Validator validator = validatorFactory.getValidator(); | ||||
|  | ||||
|         // 微信的验证 | ||||
|         // TODO @aquan:这么实现,可扩性不好。@AssertTrue 注解。 | ||||
|         if (PayChannelEnum.WECHAT.equals(channelType)) { | ||||
|  | ||||
|             WXPayClientConfig config = JSON.parseObject(configStr, WXPayClientConfig.class); | ||||
|             // 判断是V2 版本还是 V3 版本 | ||||
|             Class clazz = config.getApiVersion().equals(WXPayClientConfig.API_VERSION_V2) | ||||
|                     ? WXPayClientConfig.V2.class : WXPayClientConfig.V3.class; | ||||
|             // 手动调用validate进行验证 | ||||
|             Set<ConstraintViolation<WXPayClientConfig>> validate = validator.validate(config,clazz); | ||||
|  | ||||
|             // 断言没有异常 | ||||
|             Assert.isTrue(validate.isEmpty(), validate.stream().map(ConstraintViolation::getMessage) | ||||
|                     .collect(Collectors.joining(","))); | ||||
|  | ||||
|             channel.setConfig(config); | ||||
|         } | ||||
|  | ||||
|         // 支付宝验证 | ||||
|         if (PayChannelEnum.ALIPAY.equals(channelType)) { | ||||
|  | ||||
|             AlipayPayClientConfig config = JSON.parseObject(configStr, AlipayPayClientConfig.class); | ||||
|  | ||||
|             // 判断是V2 版本还是 V3 版本 | ||||
|             Class clazz = config.getMode().equals(AlipayPayClientConfig.MODE_PUBLIC_KEY) | ||||
|                     ? AlipayPayClientConfig.ModePublicKey.class : AlipayPayClientConfig.ModeCertificate.class; | ||||
|             // 手动调用validate进行验证 | ||||
|             Set<ConstraintViolation<AlipayPayClientConfig>> validate = validator.validate(config,clazz); | ||||
|  | ||||
|             // 断言没有异常 | ||||
|             Assert.isTrue(validate.isEmpty(), validate.stream().map(ConstraintViolation::getMessage) | ||||
|                     .collect(Collectors.joining(","))); | ||||
|             channel.setConfig(config); | ||||
|         } | ||||
|  | ||||
|         Class<? extends PayClientConfig> payClass = PayChannelEnum.findByCodeGetClass(channel.getCode()); | ||||
|         Assert.notNull(payClass, CHANNEL_NOT_EXISTS.getMsg()); | ||||
|         PayClientConfig config = JSONUtil.toBean(configStr, payClass); | ||||
|         // 验证参数 | ||||
|         config.verifyParam(validator); | ||||
|         channel.setConfig(config); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -92,14 +92,6 @@ public interface PayMerchantService { | ||||
|      */ | ||||
|     List<PayMerchantDO> getMerchantListByName(String merchantName); | ||||
|  | ||||
|     // TODO aquan:暂时不用提供这样的检索。商户不多的。 | ||||
|     /** | ||||
|      * 根据商户名称模糊查询一定数量的商户集合 | ||||
|      * @param merchantName 商户名称 | ||||
|      * @return 商户集合 | ||||
|      */ | ||||
|     List<PayMerchantDO> getMerchantListByNameLimit(String merchantName); | ||||
|  | ||||
|     /** | ||||
|      * 获得指定编号的商户 Map | ||||
|      * | ||||
|   | ||||
| @@ -10,8 +10,6 @@ import cn.iocoder.yudao.adminserver.modules.pay.dal.mysql.merchant.PayMerchantMa | ||||
| import cn.iocoder.yudao.adminserver.modules.pay.service.merchant.PayMerchantService; | ||||
| import cn.iocoder.yudao.coreservice.modules.pay.dal.dataobject.merchant.PayMerchantDO; | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||||
| import com.google.common.annotations.VisibleForTesting; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.validation.annotation.Validated; | ||||
| @@ -21,7 +19,7 @@ import java.time.LocalDateTime; | ||||
| import java.util.Collection; | ||||
| import java.util.List; | ||||
|  | ||||
| import static cn.iocoder.yudao.coreservice.modules.pay.enums.PayErrorCodeCoreConstants.MERCHANT_NOT_EXISTS; | ||||
| import static cn.iocoder.yudao.coreservice.modules.pay.enums.PayErrorCodeCoreConstants.PAY_MERCHANT_NOT_EXISTS; | ||||
| import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||||
| /** | ||||
|  * 支付商户信息 Service 实现类 | ||||
| @@ -64,7 +62,7 @@ public class PayMerchantServiceImpl implements PayMerchantService { | ||||
|  | ||||
|     private void validateMerchantExists(Long id) { | ||||
|         if (merchantMapper.selectById(id) == null) { | ||||
|             throw exception(MERCHANT_NOT_EXISTS); | ||||
|             throw exception(PAY_MERCHANT_NOT_EXISTS); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @@ -116,22 +114,6 @@ public class PayMerchantServiceImpl implements PayMerchantService { | ||||
|         return this.merchantMapper.getMerchantListByName(merchantName); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 根据商户名称模糊查询一定数量的商户集合 | ||||
|      * | ||||
|      * @param merchantName 商户名称 | ||||
|      * @return 商户集合 | ||||
|      */ | ||||
|     @Override | ||||
|     public List<PayMerchantDO> getMerchantListByNameLimit(String merchantName) { | ||||
|         // TODO @aquan:mybatis plus 哈 | ||||
|         LambdaQueryWrapper<PayMerchantDO> queryWrapper = new QueryWrapper<PayMerchantDO>().lambda() | ||||
|                 .select(PayMerchantDO::getId, PayMerchantDO::getName) | ||||
|                 .likeRight(PayMerchantDO::getName, merchantName) | ||||
|                 .last("limit 200"); | ||||
|  | ||||
|         return this.merchantMapper.selectList(queryWrapper); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 检查商户是否存在 | ||||
| @@ -144,7 +126,7 @@ public class PayMerchantServiceImpl implements PayMerchantService { | ||||
|         } | ||||
|         PayMerchantDO merchant = merchantMapper.selectById(id); | ||||
|         if (merchant == null) { | ||||
|             throw exception(MERCHANT_NOT_EXISTS); | ||||
|             throw exception(PAY_MERCHANT_NOT_EXISTS); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -92,7 +92,7 @@ public class PayAppServiceTest extends BaseDbUnitTest { | ||||
|         PayAppUpdateReqVO reqVO = randomPojo(PayAppUpdateReqVO.class, o -> | ||||
|                 o.setStatus((RandomUtil.randomEle(CommonStatusEnum.values()).getStatus()))); | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> appService.updateApp(reqVO), APP_NOT_EXISTS); | ||||
|         assertServiceException(() -> appService.updateApp(reqVO), PAY_APP_NOT_FOUND); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -116,7 +116,7 @@ public class PayAppServiceTest extends BaseDbUnitTest { | ||||
|         Long id = randomLongId(); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> appService.deleteApp(id), APP_NOT_EXISTS); | ||||
|         assertServiceException(() -> appService.deleteApp(id), PAY_APP_NOT_FOUND); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|   | ||||
| @@ -0,0 +1,20 @@ | ||||
| package cn.iocoder.yudao.adminserver.modules.pay.service.channel; | ||||
|  | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Configuration; | ||||
|  | ||||
| import javax.validation.Validation; | ||||
| import javax.validation.Validator; | ||||
|  | ||||
| /** | ||||
|  * 用于初始化 validator Bean 对象 | ||||
|  * @author aquan | ||||
|  */ | ||||
| @Configuration | ||||
| public class PayChannelConfig { | ||||
|  | ||||
|     @Bean | ||||
|     public Validator validator(){ | ||||
|         return Validation.buildDefaultValidatorFactory().getValidator(); | ||||
|     } | ||||
| } | ||||
| @@ -34,7 +34,10 @@ import static org.junit.jupiter.api.Assertions.*; | ||||
|  * | ||||
|  * @author 芋艿 | ||||
|  */ | ||||
| @Import(PayChannelServiceImpl.class) | ||||
| @Import({ | ||||
|         PayChannelServiceImpl.class, | ||||
|         PayChannelConfig.class | ||||
| }) | ||||
| public class PayChannelServiceTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @Resource | ||||
| @@ -43,7 +46,6 @@ public class PayChannelServiceTest extends BaseDbUnitTest { | ||||
|     @Resource | ||||
|     private PayChannelMapper channelMapper; | ||||
|  | ||||
|  | ||||
|     @Test | ||||
|     public void testCreateWechatVersion2Channel_success() { | ||||
|         // 准备参数 | ||||
| @@ -69,8 +71,7 @@ public class PayChannelServiceTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @Test | ||||
|     public void testCreateWechatVersion3Channel_success() { | ||||
|         // 准备参数 TODO @aquan:多余的空行去掉哈。例如说 74 行。 | ||||
|  | ||||
|         // 准备参数 | ||||
|         WXPayClientConfig v3Config = getV3Config(); | ||||
|         PayChannelCreateReqVO reqVO = randomPojo(PayChannelCreateReqVO.class, o -> { | ||||
|             o.setCode(PayChannelEnum.WX_PUB.getCode()); | ||||
|   | ||||
| @@ -18,7 +18,7 @@ import org.springframework.context.annotation.Import; | ||||
| import javax.annotation.Resource; | ||||
| import java.util.List; | ||||
|  | ||||
| import static cn.iocoder.yudao.coreservice.modules.pay.enums.PayErrorCodeCoreConstants.MERCHANT_NOT_EXISTS; | ||||
| import static cn.iocoder.yudao.coreservice.modules.pay.enums.PayErrorCodeCoreConstants.PAY_MERCHANT_NOT_EXISTS; | ||||
| import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; | ||||
| import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | ||||
| import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime; | ||||
| @@ -79,7 +79,7 @@ public class PayMerchantServiceTest extends BaseDbUnitTest { | ||||
|         PayMerchantUpdateReqVO reqVO = randomPojo(PayMerchantUpdateReqVO.class); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> merchantService.updateMerchant(reqVO), MERCHANT_NOT_EXISTS); | ||||
|         assertServiceException(() -> merchantService.updateMerchant(reqVO), PAY_MERCHANT_NOT_EXISTS); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -103,7 +103,7 @@ public class PayMerchantServiceTest extends BaseDbUnitTest { | ||||
|         Long id = randomLongId(); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> merchantService.deleteMerchant(id), MERCHANT_NOT_EXISTS); | ||||
|         assertServiceException(() -> merchantService.deleteMerchant(id), PAY_MERCHANT_NOT_EXISTS); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 aquan
					aquan