package config import ( "encoding/json" log "github.com/sirupsen/logrus" "github.com/smartwalle/alipay/v3" "io/ioutil" "sync" ) type AliPay struct { Name string `json:"name"` AppId string `json:"appId"` PublicKey string `json:"publicKey"` PrivateKey string `json:"privateKey"` //证书加密方式 IsCert bool `json:"isCert"` AppPublicKey string `json:"appPublicKey"` AlipayPublicKey string `json:"alipayPublicKey"` AlipayRootKey string `json:"alipayRootKey"` } var ( alipayMap map[string]*AliPay alipayClientMap map[string]*alipay.Client alipayMutex sync.RWMutex ) func LoadAliPay() { alipayClientMap = make(map[string]*alipay.Client) cfgBody, err := ioutil.ReadFile("conf/alipay/config.json") if err != nil { panic(err) } if err := json.Unmarshal(cfgBody, &alipayMap); err != nil { panic(err) } for k, v := range alipayMap { c, err := alipay.New(v.AppId, v.PrivateKey, true) if err != nil { log.Errorf("[alipay_notify]支付宝回调错误:%s", err.Error()) panic(err) } //证书模式 if v.IsCert { err = c.LoadAppCertPublicKeyFromFile(v.AppPublicKey) if err != nil { log.Errorf("LoadAppCertPublicKeyFromFile %s", err.Error()) panic(err) } err = c.LoadAlipayCertPublicKeyFromFile(v.AlipayPublicKey) if err != nil { log.Errorf("LoadAlipayCertPublicKeyFromFile %s", err.Error()) panic(err) } err = c.LoadAliPayRootCertFromFile(v.AlipayRootKey) if err != nil { log.Errorf("LoadAliPayRootCertFromFile %s", err.Error()) panic(err) } } else { err = c.LoadAliPayPublicKey(v.PublicKey) if err != nil { log.Errorf("LoadAliPayPublicKey 公钥加载失败 %s", err.Error()) panic(err) } } alipayClientMap[k] = c } //log.Infof("wxpay config : %s", goutil.EncodeJSON(alipayMap)) } func GetAliPay(channel string) *AliPay { alipayMutex.RLock() defer alipayMutex.RUnlock() return alipayMap[channel] } func GetAliPayClient(channel string) *alipay.Client { alipayMutex.RLock() defer alipayMutex.RUnlock() return alipayClientMap[channel] }