微信支付apiV3异常:The corresponding provider for the merchant already exists

这篇具有很好参考价值的文章主要介绍了微信支付apiV3异常:The corresponding provider for the merchant already exists。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

异常信息
java.lang.IllegalStateException: The corresponding provider for the merchant already exists.
原因

这个错误是微信SDK抛出的,这是因为微信支付apiV3的RSAConfig重复build导致,即RSAConfig要保证是单例才不会导致报错。

// 要保证这个Config在服务中单例
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;
}
参数说明
  • mchId:商户号
  • privateKey:商户号密钥
  • mchSerialNo:商户证书号
  • apiV3Key:apiV3密钥
建议

可以把商户配置参数使用数据库保存,服务启动的时候加载在缓存里面,缓存的key为mchId,value为config对象,这样可以实现一个服务多个直连商户号共存。

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 根据AppId获取Config
     * @return
     */
    public Config getWechatConfig(String appId) {
        return configMap.get(appId);
    }

    /**
     * @param appId 根据appId获取NotificationConfig
     * @return
     */
    public NotificationConfig getNotificationConfig(String appId) {
        return configMap.get(appId);
    }
}

PaymentMchPo 对应的sql结构文章来源地址https://www.toymoban.com/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"
)
;

到了这里,关于微信支付apiV3异常:The corresponding provider for the merchant already exists的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包