Adapay根据@ConditionalOnProperty("adapay")自动完成配置和初始化

This commit is contained in:
HH 2022-03-31 18:47:01 +08:00
parent 70a3245630
commit c509323b3f
5 changed files with 25 additions and 22 deletions

View File

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

View File

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

View File

@ -76,7 +76,7 @@ public class AdapayService {
* @param paymentId [必填项]支付确认对象的id
* @param refundOrderNo [必填项]订单号
* @param refundAmt [必填项]退款金额若退款金额小于原交易金额则认为是部分退款必须大于0保留两位小数点如0.10100.05等
* @return 同步返回一个 退款对象
* @return 同步返回一个 退款对象 https://docs.adapay.tech/api/trade.html#create-refund-params
*/
public Map<String, Object> refund(RefundCallback callback, String paymentId, String refundOrderNo, String refundAmt) throws BaseAdaPayException {
Map<String, Object> 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<String, Object> close(String paymentId, String reason, String expend) throws BaseAdaPayException {
Map<String, Object> paymentParams = new HashMap<>(4);

View File

@ -11,7 +11,7 @@ import lombok.EqualsAndHashCode;
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class WxLiteExpend extends Expend{
public class WxLiteExpend extends Expend {
/**
* [必填]微信用户关注商家公众号的 openid

View File

@ -0,0 +1,3 @@
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ghy.common.adapay.AdapayAutoConfiguration