異常信息
java.lang.IllegalStateException: The corresponding provider for the merchant already exists.
原因
這個(gè)錯(cuò)誤是微信SDK拋出的,這是因?yàn)槲⑿胖Ц禷piV3的RSAConfig重復(fù)build導(dǎo)致,即RSAConfig要保證是單例才不會導(dǎo)致報(bào)錯(cuò)。
// 要保證這個(gè)Config在服務(wù)中單例
private RSAAutoCertificateConfig config;
@Autowired
public void setConfig(){
config = new RSAAutoCertificateConfig.Builder()
.merchantId(mchId)
.privateKey(privateKey)
.merchantSerialNumber(mchSerialNo)
.apiV3Key(apiV3Key)
.build();
}
public Config getConfig(){
return this.config;
}
public NotificationConfig getNotificationConfig(){
return this.config;
}
參數(shù)說明
- mchId:商戶號
- privateKey:商戶號密鑰
- mchSerialNo:商戶證書號
- apiV3Key:apiV3密鑰
建議
可以把商戶配置參數(shù)使用數(shù)據(jù)庫保存,服務(wù)啟動(dòng)的時(shí)候加載在緩存里面,緩存的key為mchId,value為config對象,這樣可以實(shí)現(xiàn)一個(gè)服務(wù)多個(gè)直連商戶號共存。文章來源:http://www.zghlxwxcb.cn/news/detail-511964.html
package com.swkj.payment.test;
import com.swkj.common.base.enums.PaymentMchType;
import com.swkj.common.base.util.CollectionUtils;
import com.swkj.common.charge.model.po.paymentmch.PaymentMchPo;
import com.swkj.common.charge.service.paymentmch.PaymentMchService;
import com.swkj.common.core.wrapper.LambdaQueryWrapperX;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* @Description:
* @Author: GE LIANG
* @Date: 2023/3/14 8:49
*/
public class PaymentServiceImpl {
// 本地緩存
private Map<String, RSAAutoCertificateConfig> configMap;
@Resource
PaymentMchService paymentMchService;
@PostConstruct
public void initConfig() {
LambdaQueryWrapperX<PaymentMchPo> wrapperX = new LambdaQueryWrapperX<>();
wrapperX.eq(PaymentMchPo::getMchTypeId, PaymentMchType.WECHAT.getValue());
List<PaymentMchPo> list = paymentMchService.list(wrapperX);
if (CollectionUtils.isEmpty(list)) {
return;
}
for (PaymentMchPo paymentMchPo : list) {
RSAAutoCertificateConfig config =
new RSAAutoCertificateConfig.Builder()
.merchantId(paymentMchPo.getMchId())
.privateKey(paymentMchPo.getPrivateKey())
// .privateKeyFromPath(wechatPaymentConfig.getPrivateKeyPath())
.merchantSerialNumber(paymentMchPo.getMchSerialNo())
.apiV3Key(paymentMchPo.getApiV3Key())
.build();
configMap.put(paymentMchPo.getAppId(), config);
}
}
/**
* @param appId 根據(jù)AppId獲取Config
* @return
*/
public Config getWechatConfig(String appId) {
return configMap.get(appId);
}
/**
* @param appId 根據(jù)appId獲取NotificationConfig
* @return
*/
public NotificationConfig getNotificationConfig(String appId) {
return configMap.get(appId);
}
}
PaymentMchPo 對應(yīng)的sql結(jié)構(gòu)文章來源地址http://www.zghlxwxcb.cn/news/detail-511964.html
DROP TABLE IF EXISTS "public"."charge_payment_mch";
CREATE TABLE "public"."charge_payment_mch" (
"payment_mch_id" int8 NOT NULL,
"revision" int4 NOT NULL,
"created_by" int8 NOT NULL,
"created_time" timestamp(0) NOT NULL,
"updated_by" int8,
"updated_time" timestamp(0),
"mch_id" varchar(50) COLLATE "pg_catalog"."default",
"private_key" text COLLATE "pg_catalog"."default",
"mch_serial_no" varchar(256) COLLATE "pg_catalog"."default",
"api_v3_key" varchar(256) COLLATE "pg_catalog"."default",
"app_id" varchar(50) COLLATE "pg_catalog"."default"
)
;
到了這里,關(guān)于微信支付apiV3異常:The corresponding provider for the merchant already exists的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!