From c509323b3f29c289e7d4e07379113e18a0ddd204 Mon Sep 17 00:00:00 2001 From: HH Date: Thu, 31 Mar 2022 18:47:01 +0800 Subject: [PATCH] =?UTF-8?q?Adapay=E6=A0=B9=E6=8D=AE@ConditionalOnProperty(?= =?UTF-8?q?"adapay")=E8=87=AA=E5=8A=A8=E5=AE=8C=E6=88=90=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=92=8C=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapay/AdapayAutoConfiguration.java | 19 +++++++++++++++++- .../ghy/common/adapay/AdapayProperties.java | 20 +------------------ .../com/ghy/common/adapay/AdapayService.java | 3 ++- .../adapay/callback/model/WxLiteExpend.java | 2 +- .../main/resources/META-INF/spring.factories | 3 +++ 5 files changed, 25 insertions(+), 22 deletions(-) create mode 100644 ghy-common/src/main/resources/META-INF/spring.factories diff --git a/ghy-common/src/main/java/com/ghy/common/adapay/AdapayAutoConfiguration.java b/ghy-common/src/main/java/com/ghy/common/adapay/AdapayAutoConfiguration.java index 64c69c72..2c57c9f2 100644 --- a/ghy-common/src/main/java/com/ghy/common/adapay/AdapayAutoConfiguration.java +++ b/ghy-common/src/main/java/com/ghy/common/adapay/AdapayAutoConfiguration.java @@ -1,8 +1,12 @@ package com.ghy.common.adapay; +import com.huifu.adapay.Adapay; +import com.huifu.adapay.model.MerConfig; +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.util.Assert; /** * Adapay 自动装载 @@ -10,11 +14,24 @@ import org.springframework.context.annotation.Configuration; * @author HH 2022/3/25 */ @Configuration +@ConditionalOnProperty("adapay") @EnableConfigurationProperties(AdapayProperties.class) public class AdapayAutoConfiguration { @Bean - public AdapayService adapayService(AdapayProperties adapayProperties) { + public AdapayService adapayService(AdapayProperties adapayProperties) throws Exception { + + Assert.hasText(adapayProperties.getAppId(), "Adapay.appId is blank!"); + Assert.hasText(adapayProperties.getApiKey(), "Adapay.apiKey is blank!"); + Assert.hasText(adapayProperties.getMockApiKey(), "Adapay.mockApiKey is blank!"); + Assert.hasText(adapayProperties.getRsaPrivateKey(), "Adapay.rsaPrivateKey is blank!"); + + MerConfig merConfig = new MerConfig(); + merConfig.setApiKey(adapayProperties.getApiKey()); + merConfig.setApiMockKey(adapayProperties.getMockApiKey()); + merConfig.setRSAPrivateKey(adapayProperties.getRsaPrivateKey()); + Adapay.initWithMerConfig(merConfig); + AdapayService adapayService = new AdapayService(); adapayService.setAdapayProperties(adapayProperties); return adapayService; diff --git a/ghy-common/src/main/java/com/ghy/common/adapay/AdapayProperties.java b/ghy-common/src/main/java/com/ghy/common/adapay/AdapayProperties.java index 19583146..96b4b25e 100644 --- a/ghy-common/src/main/java/com/ghy/common/adapay/AdapayProperties.java +++ b/ghy-common/src/main/java/com/ghy/common/adapay/AdapayProperties.java @@ -1,12 +1,8 @@ package com.ghy.common.adapay; -import com.huifu.adapay.Adapay; -import com.huifu.adapay.model.MerConfig; import lombok.Data; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.util.Assert; /** * Adapay 配置类 @@ -16,7 +12,7 @@ import org.springframework.util.Assert; @Data @Slf4j @ConfigurationProperties("adapay") -public class AdapayProperties implements InitializingBean { +public class AdapayProperties { /** * 是否打印调用日志,默认不打印 @@ -41,18 +37,4 @@ public class AdapayProperties implements InitializingBean { private String mockApiKey; private String rsaPrivateKey; - @Override - public void afterPropertiesSet() throws Exception { - log.info("Adapay.debug={}, Adapay.prodMode={}", debug, prodMode); - Assert.hasText(appId, "Adapay.appId is blank!"); - Assert.hasText(apiKey, "Adapay.apiKey is blank!"); - Assert.hasText(mockApiKey, "Adapay.mockApiKey is blank!"); - Assert.hasText(rsaPrivateKey, "Adapay.rsaPrivateKey is blank!"); - - MerConfig merConfig = new MerConfig(); - merConfig.setApiKey(apiKey); - merConfig.setApiMockKey(mockApiKey); - merConfig.setRSAPrivateKey(rsaPrivateKey); - Adapay.initWithMerConfig(merConfig); - } } \ No newline at end of file diff --git a/ghy-common/src/main/java/com/ghy/common/adapay/AdapayService.java b/ghy-common/src/main/java/com/ghy/common/adapay/AdapayService.java index 669ce698..69955ae9 100644 --- a/ghy-common/src/main/java/com/ghy/common/adapay/AdapayService.java +++ b/ghy-common/src/main/java/com/ghy/common/adapay/AdapayService.java @@ -76,7 +76,7 @@ public class AdapayService { * @param paymentId [必填项]支付确认对象的id * @param refundOrderNo [必填项]订单号 * @param refundAmt [必填项]退款金额,若退款金额小于原交易金额,则认为是部分退款,必须大于0,保留两位小数点,如0.10、100.05等 - * @return 同步返回一个 退款对象 + * @return 同步返回一个 退款对象 https://docs.adapay.tech/api/trade.html#create-refund-params */ public Map refund(RefundCallback callback, String paymentId, String refundOrderNo, String refundAmt) throws BaseAdaPayException { Map refundParams = new HashMap<>(4); @@ -98,6 +98,7 @@ public class AdapayService { * @param paymentId [必填项]由 Adapay 生成的支付对象 id * @param reason 关单描述 * @param expend 扩展域 + * @return 关单的结果将通过一个 JSON 同步返回 https://docs.adapay.tech/api/trade.html#close-payment-response */ public Map close(String paymentId, String reason, String expend) throws BaseAdaPayException { Map paymentParams = new HashMap<>(4); diff --git a/ghy-common/src/main/java/com/ghy/common/adapay/callback/model/WxLiteExpend.java b/ghy-common/src/main/java/com/ghy/common/adapay/callback/model/WxLiteExpend.java index 5ff4edfe..80701afb 100644 --- a/ghy-common/src/main/java/com/ghy/common/adapay/callback/model/WxLiteExpend.java +++ b/ghy-common/src/main/java/com/ghy/common/adapay/callback/model/WxLiteExpend.java @@ -11,7 +11,7 @@ import lombok.EqualsAndHashCode; */ @Data @EqualsAndHashCode(callSuper = true) -public class WxLiteExpend extends Expend{ +public class WxLiteExpend extends Expend { /** * [必填]微信用户关注商家公众号的 openid diff --git a/ghy-common/src/main/resources/META-INF/spring.factories b/ghy-common/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..d4d5a584 --- /dev/null +++ b/ghy-common/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +# Auto Configure +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +com.ghy.common.adapay.AdapayAutoConfiguration