Files
jsowell-charger-web/jsowell-pile/src/main/java/com/jsowell/adapay/config/InitializeAdapayConfig.java
2023-08-15 15:29:05 +08:00

125 lines
3.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.jsowell.adapay.config;
import com.alibaba.fastjson2.JSON;
import com.huifu.adapay.Adapay;
import com.huifu.adapay.model.MerConfig;
import com.jsowell.adapay.factory.AdapayConfigFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Order(value = 3)
@Component
public class InitializeAdapayConfig implements CommandLineRunner {
private Logger logger = LoggerFactory.getLogger(InitializeAdapayConfig.class);
@Value("${adapay.debugFlag}")
private boolean DEBUG_FLAG;
@Value("${adapay.prodMode}")
private boolean PROD_MODE;
@Value("${adapay.jsowell.apiKey}")
private String JSOWELL_API_KEY;
@Value("${adapay.jsowell.mockApiKey}")
private String JSOWELL_MOCK_API_KEY;
@Value("${adapay.jsowell.rsaPrivateKey}")
private String JSOWELL_RSA_PRIVATE_KEY;
@Override
public void run(String... args) throws Exception {
/*
单商户
*/
// singleMerchant();
/*
多商户
目前有jsowell和xixiao
*/
multiMerchant();
}
/**
* 单商户配置
*/
public void singleMerchant() throws Exception {
/**
* debug 模式,开启后有详细的日志
*/
Adapay.debug = DEBUG_FLAG;
/**
* prodMode 模式默认为生产模式false可以使用mock模式
*/
Adapay.prodMode = PROD_MODE;
/**
* 初始化商户配置,服务器启动前,必须通过该方式初始化商户配置完成
* apiKey为prod模式的API KEY
* mockApiKey为mock模式的API KEY
* rsaPrivateKey为商户发起请求时用于请求参数加签所需要的RSA私钥
*/
String apiKey = JSOWELL_API_KEY;
String mockApiKey = JSOWELL_MOCK_API_KEY;
String rsaPrivateKey = JSOWELL_RSA_PRIVATE_KEY;
MerConfig merConfig = new MerConfig();
merConfig.setApiKey(apiKey);
merConfig.setApiMockKey(mockApiKey);
merConfig.setRSAPrivateKey(rsaPrivateKey);
Adapay.initWithMerConfig(merConfig);
logger.info("汇付配置初始化成功debug:{}, prodMode:{}, apiKey:{}, mockApiKey:{}, rsaPrivateKey:{}"
, DEBUG_FLAG, PROD_MODE, JSOWELL_API_KEY, JSOWELL_MOCK_API_KEY, JSOWELL_RSA_PRIVATE_KEY);
}
/**
* 多商户配置
* @throws Exception
*/
public void multiMerchant() throws Exception {
/**
* debug 模式,开启后与详细的日志
*/
Adapay.debug = true;
/**
* prodMode 模式默认为生产模式false可以使用mock模式
*/
Adapay.prodMode = true;
/**
* 初始化每个商户配置,服务器启动前,必须通过该方式初始化商户配置完成
* apiKey为prod模式的API KEY
* mockApiKey为mock模式的API KEY
* rsaPrivateKey为商户发起请求时用于请求参数加签所需要的RSA私钥
*/
Map<String, MerConfig> configPathMap = new HashMap<>();
List<AbstractAdapayConfig> allConfig = AdapayConfigFactory.getAllConfig();
for (AbstractAdapayConfig config : allConfig) {
String apiKey = config.getApiKey();
String mockApiKey = config.getMockApiKey();
String rsaPrivateKey = config.getRsaPrivateKey();
MerConfig merConfig = new MerConfig();
merConfig.setApiKey(apiKey);
merConfig.setApiMockKey(mockApiKey);
merConfig.setRSAPrivateKey(rsaPrivateKey);
// 定义一个商户的B的唯一标识交易发起时用于定位是哪个商户发起交易
configPathMap.put(config.getWechatAppId(), merConfig);
}
// 将商户A和B的商户配置放入本地缓存
Adapay.initWithMerConfigs(configPathMap);
logger.info("汇付多商户配置初始化成功configPathMap:{}", JSON.toJSONString(configPathMap));
}
}