From f46187aed03b08b97236dd5d92039425ba9bb89e Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Wed, 17 Apr 2019 23:40:50 +0800 Subject: [PATCH] =?UTF-8?q?#1007=20=E5=A2=9E=E5=8A=A0=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E6=94=AF=E4=BB=98=E7=9A=84=20spring=20boot=20starter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + starters/wx-java-pay-starter/pom.xml | 65 +++++++++++++++++++ .../pay/config/WxPayAutoConfiguration.java | 57 ++++++++++++++++ .../pay/properties/WxPayProperties.java | 46 +++++++++++++ .../main/resources/META-INF/spring.factories | 1 + 5 files changed, 170 insertions(+) create mode 100644 starters/wx-java-pay-starter/pom.xml create mode 100644 starters/wx-java-pay-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/config/WxPayAutoConfiguration.java create mode 100644 starters/wx-java-pay-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/properties/WxPayProperties.java create mode 100644 starters/wx-java-pay-starter/src/main/resources/META-INF/spring.factories diff --git a/pom.xml b/pom.xml index 19b454b06..7843d0444 100644 --- a/pom.xml +++ b/pom.xml @@ -105,6 +105,7 @@ weixin-java-pay weixin-java-miniapp weixin-java-open + starters/wx-java-pay-starter diff --git a/starters/wx-java-pay-starter/pom.xml b/starters/wx-java-pay-starter/pom.xml new file mode 100644 index 000000000..f61676f3c --- /dev/null +++ b/starters/wx-java-pay-starter/pom.xml @@ -0,0 +1,65 @@ + + + + wx-java + com.github.binarywang + 3.3.8.B + + 4.0.0 + + wx-java-pay-starter + + + 2.1.4.RELEASE + + + + + org.springframework.boot + spring-boot-autoconfigure + ${spring.boot.version} + + + org.springframework.boot + spring-boot-configuration-processor + ${spring.boot.version} + true + + + org.projectlombok + lombok + provided + + + com.github.binarywang + weixin-java-pay + ${project.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring.boot.version} + + + org.apache.maven.plugins + maven-source-plugin + 2.2.1 + + + attach-sources + + jar-no-fork + + + + + + + + diff --git a/starters/wx-java-pay-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/config/WxPayAutoConfiguration.java b/starters/wx-java-pay-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/config/WxPayAutoConfiguration.java new file mode 100644 index 000000000..43b2114e6 --- /dev/null +++ b/starters/wx-java-pay-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/config/WxPayAutoConfiguration.java @@ -0,0 +1,57 @@ +package com.binarywang.spring.starter.wxjava.pay.config; + +import com.binarywang.spring.starter.wxjava.pay.properties.WxPayProperties; +import com.github.binarywang.wxpay.config.WxPayConfig; +import com.github.binarywang.wxpay.service.WxPayService; +import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +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; + +/** + *
+ *  微信支付自动配置
+ *  Created by BinaryWang on 2019/4/17.
+ * 
+ * + * @author Binary Wang + */ +@Configuration +@EnableConfigurationProperties(WxPayProperties.class) +@ConditionalOnClass(WxPayService.class) +@ConditionalOnProperty(prefix = "wx.pay", value = "enabled", matchIfMissing = true) +public class WxPayAutoConfiguration { + private WxPayProperties properties; + + @Autowired + public WxPayAutoConfiguration(WxPayProperties properties) { + this.properties = properties; + } + + /** + * 构造微信支付服务对象. + * + * @return 微信支付service + */ + @Bean + @ConditionalOnMissingBean(WxPayService.class) + public WxPayService wxPayService() { + final WxPayServiceImpl wxPayService = new WxPayServiceImpl(); + WxPayConfig payConfig = new WxPayConfig(); + payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId())); + payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId())); + payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey())); + payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId())); + payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId())); + payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath())); + + wxPayService.setConfig(payConfig); + return wxPayService; + } + +} diff --git a/starters/wx-java-pay-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/properties/WxPayProperties.java b/starters/wx-java-pay-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/properties/WxPayProperties.java new file mode 100644 index 000000000..fe8a21565 --- /dev/null +++ b/starters/wx-java-pay-starter/src/main/java/com/binarywang/spring/starter/wxjava/pay/properties/WxPayProperties.java @@ -0,0 +1,46 @@ +package com.binarywang.spring.starter.wxjava.pay.properties; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + *
+ *  微信支付属性配置类
+ * Created by Binary Wang on 2019/4/17.
+ * 
+ * + * @author Binary Wang + */ +@Data +@ConfigurationProperties(prefix = "wx.pay") +public class WxPayProperties { + /** + * 设置微信公众号或者小程序等的appid. + */ + private String appId; + + /** + * 微信支付商户号. + */ + private String mchId; + + /** + * 微信支付商户密钥. + */ + private String mchKey; + + /** + * 服务商模式下的子商户公众账号ID,普通模式请不要配置,请在配置文件中将对应项删除. + */ + private String subAppId; + + /** + * 服务商模式下的子商户号,普通模式请不要配置,最好是请在配置文件中将对应项删除. + */ + private String subMchId; + + /** + * apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:开头指定. + */ + private String keyPath; +} diff --git a/starters/wx-java-pay-starter/src/main/resources/META-INF/spring.factories b/starters/wx-java-pay-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..37fe6c20e --- /dev/null +++ b/starters/wx-java-pay-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.binarywang.spring.starter.wxjava.pay.config.WxPayAutoConfiguration