|
|
|
|
@ -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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|