All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
matrix.business.pay.config.PayAutoConfiguration Maven / Gradle / Ivy
package matrix.business.pay.config;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import matrix.boot.common.utils.AssertUtil;
import matrix.boot.jdbc.beans.DynamicDataSource;
import matrix.boot.jdbc.config.JdbcTemplateAutoConfiguration;
import matrix.business.common.utils.SqlUtil;
import matrix.business.pay.constants.AlipayConstant;
import matrix.business.pay.constants.PayConstant;
import matrix.business.pay.controller.ForwardPayController;
import matrix.business.pay.controller.PayNotifyController;
import matrix.business.pay.controller.WePayController;
import matrix.business.pay.properties.PayProperties;
import matrix.business.pay.selector.AlipaySelector;
import matrix.business.pay.selector.WxSelector;
import matrix.business.pay.service.impl.MatrixPayServiceImpl;
import matrix.business.pay.service.impl.MatrixRefundServiceImpl;
import matrix.business.pay.templates.AbstractTemplate;
import matrix.business.pay.templates.AlipayTemplate;
import matrix.business.pay.selector.PayTemplateSelector;
import matrix.business.pay.templates.WePayTemplate;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* 支付自动装配
*
* @author wangcheng
*/
@Configuration
@EnableConfigurationProperties({PayProperties.class, PayProperties.AlipayProperties.class, PayProperties.WePayProperties.class})
@AutoConfigureAfter(JdbcTemplateAutoConfiguration.class)
@ImportAutoConfiguration({MatrixPayServiceImpl.class, MatrixRefundServiceImpl.class, ForwardPayController.class, PayNotifyController.class})
@ConditionalOnProperty(value = {"matrix.business.pay.enabled"})
public class PayAutoConfiguration {
/**
* 支付配置参数
*/
private final PayProperties payProperties;
/**
* jdbc模板
*/
private final JdbcTemplate jdbcTemplate;
public PayAutoConfiguration(PayProperties payProperties, JdbcTemplate jdbcTemplate) {
this.payProperties = payProperties;
this.jdbcTemplate = jdbcTemplate;
}
/**
* 初始化支付表
*/
@PostConstruct
public void init() {
//设置动态数据源的db,如果不是动态数据源设置无效
DynamicDataSource.DynamicDataSourceHolder.setDataSourceKey(payProperties.getDb());
try {
//判断支付表是否存在
jdbcTemplate.execute(SqlUtil.getSql("exist_pay_table.sql"));
} catch (BadSqlGrammarException e) {
//创建支付表
jdbcTemplate.execute(SqlUtil.getSql("create_pay_table.sql"));
}
try {
//判断退款表是否存在
jdbcTemplate.execute(SqlUtil.getSql("exist_refund_table.sql"));
} catch (BadSqlGrammarException e) {
//新建数据表
jdbcTemplate.execute(SqlUtil.getSql("create_refund_table.sql"));
}
//清除动态数据源配置
DynamicDataSource.DynamicDataSourceHolder.clearDataSourceKey();
}
/**
* 支付宝自动装配
*/
@Configuration
@EnableConfigurationProperties(PayProperties.class)
@Slf4j
public static class AlipayAutoConfiguration {
/**
* 支付配置
*/
private final PayProperties payProperties;
public AlipayAutoConfiguration(PayProperties payProperties) {
this.payProperties = payProperties;
}
/**
* 支付宝支付选择器
*
* @return 支付宝支付选择器
*/
@Bean
public AlipaySelector alipaySelector() {
AssertUtil.notNullTip(payProperties.getNotifyUrl(), "matrix.business.pay.notify-url");
Map alipayPropertiesMap = payProperties.getAlipay();
//定义返回参数
AlipaySelector result = new AlipaySelector();
if (CollectionUtils.isEmpty(alipayPropertiesMap)) {
return result;
}
alipayPropertiesMap.forEach((typeCode, alipay) -> {
if (!alipay.isEnabled()) {
return;
}
AssertUtil.notNullTip(alipay.getRedirectUrl(), "matrix.business.pay.alipay.{typeCode}.redirect-url");
AssertUtil.notNullTip(alipay.getAppId(), "matrix.business.pay.alipay.{typeCode}.app-id");
AssertUtil.notNullTip(alipay.getSignType(), "matrix.business.pay.alipay.{typeCode}.sign-type");
AssertUtil.notNullTip(alipay.getPrivateKey(), "matrix.business.pay.alipay.{typeCode}.private-key");
AssertUtil.notNullTip(alipay.getPublicKey(), "matrix.business.pay.alipay.{typeCode}.public-key");
//放入配置
result.put(typeCode, alipay);
String gatewayUrl = alipay.isTest() ? AlipayConstant.TEST_GATEWAY_URL : AlipayConstant.GATEWAY_URL;
//创建客户端
AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, alipay.getAppId(), alipay.getPrivateKey(), AlipayConstant.FORMAT,
StandardCharsets.UTF_8.name(), alipay.getPublicKey(), alipay.getSignType());
result.put(typeCode, alipayClient);
});
return result;
}
/**
* 支付宝支付模板
*
* @return 支付宝支付模板
*/
@Bean
public AlipayTemplate alipayTemplate() {
AlipayTemplate result = new AlipayTemplate(alipaySelector());
log.info("Alipay Notify Pay Url:" + payProperties.getNotifyUrl() + PayConstant.NOTIFY_PAY_URL_PREFIX + result.getName());
return result;
}
}
/**
* 微信支付自动配置
*/
@Configuration
@ImportAutoConfiguration(WePayController.class)
@EnableConfigurationProperties(PayProperties.class)
@Slf4j
public static class WePayAutoConfiguration {
/**
* 支付配置
*/
private final PayProperties payProperties;
public WePayAutoConfiguration(PayProperties payProperties) {
this.payProperties = payProperties;
}
/**
* 微信对接选择器
*
* @return 微信对接选择器
*/
@Bean
public WxSelector wxSelector() {
AssertUtil.notNullTip(payProperties.getNotifyUrl(), "matrix.business.pay.notify-url");
Map wePayPropertiesMap = payProperties.getWePay();
//定义返回参数
WxSelector result = new WxSelector();
if (CollectionUtils.isEmpty(wePayPropertiesMap)) {
return result;
}
wePayPropertiesMap.forEach((typeCode, wePayProperties) -> {
if (!wePayProperties.isEnabled()) {
return;
}
AssertUtil.notNullTip(wePayProperties.getAppId(), "matrix.business.pay.we-pay.{typeCode}.app-id");
AssertUtil.notNullTip(wePayProperties.getSecret(), "matrix.business.pay.we-pay.{typeCode}.secret");
AssertUtil.notNullTip(wePayProperties.getMchId(), "matrix.business.pay.we-pay.{typeCode}.mch-id");
AssertUtil.notNullTip(wePayProperties.getMchKey(), "matrix.business.pay.we-pay.{typeCode}.mch-key");
AssertUtil.notNullTip(wePayProperties.getKeyPath(), "matrix.business.pay.we-pay.{typeCode}.key-path");
AssertUtil.notNullTip(wePayProperties.getRedirectUrl(), "matrix.business.pay.we-pay.{typeCode}.redirect-url");
//放入配置
result.put(typeCode, wePayProperties);
//微信支付配置
WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId(StringUtils.trimToNull(wePayProperties.getAppId()));
payConfig.setMchId(StringUtils.trimToNull(wePayProperties.getMchId()));
payConfig.setMchKey(StringUtils.trimToNull(wePayProperties.getMchKey()));
payConfig.setSubAppId(StringUtils.trimToNull(wePayProperties.getSubAppId()));
payConfig.setSubMchId(StringUtils.trimToNull(wePayProperties.getSubMchId()));
payConfig.setKeyPath(StringUtils.trimToNull(wePayProperties.getKeyPath()));
payConfig.setUseSandboxEnv(wePayProperties.isTest());
result.put(typeCode, payConfig);
//微信支付服务
WxPayService wxPayService = new WxPayServiceImpl();
wxPayService.setConfig(payConfig);
result.put(typeCode, wxPayService);
//微信公众号对接服务
WxMpDefaultConfigImpl wpConfig = new WxMpDefaultConfigImpl();
wpConfig.setAppId(StringUtils.trimToNull(wePayProperties.getAppId()));
wpConfig.setSecret(StringUtils.trimToNull(wePayProperties.getSecret()));
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wpConfig);
result.put(typeCode, wxMpService);
});
return result;
}
/**
* 微信支付模板方法
*
* @return 微信支付模板方法
*/
@Bean
public WePayTemplate wepayTemplate() {
AssertUtil.notNullTip(payProperties.getNotifyUrl(), "matrix.business.pay.notify-url");
WePayTemplate wepayTemplate = new WePayTemplate(wxSelector());
log.info("WePay Notify Pay Url:" + payProperties.getNotifyUrl() + PayConstant.NOTIFY_PAY_URL_PREFIX + wepayTemplate.getName());
log.info("WePay Notify Refund Url:" + payProperties.getNotifyUrl() + PayConstant.NOTIFY_RETURN_URL_PREFIX + wepayTemplate.getName());
return wepayTemplate;
}
}
/**
* 支付模板选择器
*
* @param objectProvider bean获取
* @return 支付模板选择器
*/
@Bean
public PayTemplateSelector payTemplateSelector(ObjectProvider objectProvider) {
Iterator iterator = objectProvider.stream().iterator();
Map templateMap = new HashMap<>();
while (iterator.hasNext()) {
AbstractTemplate template = iterator.next();
templateMap.put(template.getName(), template);
}
return new PayTemplateSelector(templateMap);
}
}