82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"lone-services/pkg/utils"
|
|
wxpay "lone-services/pkg/wechat/pay"
|
|
)
|
|
|
|
func NewWechatPayConfig(clientCode string) (wxpay.Config, error) {
|
|
if clientCode == utils.StringEmpty {
|
|
return wxpay.Config{}, fmt.Errorf("wechat pay client_code empty")
|
|
}
|
|
|
|
prefix := "wechat." + clientCode
|
|
alias := clientAppAlias(clientCode)
|
|
|
|
appID := firstNonEmpty(
|
|
utils.GetConfigString(prefix+".app_id"),
|
|
utils.GetConfigString(prefix+".appid"),
|
|
utils.GetConfigString("wechat."+alias+"_appid"),
|
|
utils.GetConfigString("wechat.appid"),
|
|
utils.GetConfigString("wechat.app_id"),
|
|
)
|
|
mchID := firstNonEmpty(
|
|
utils.GetConfigString(prefix+".pay_mchid"),
|
|
utils.GetConfigString("wechat.pay_mchid"),
|
|
)
|
|
serialNo := firstNonEmpty(
|
|
utils.GetConfigString(prefix+".pay_serial_no"),
|
|
utils.GetConfigString("wechat.pay_serial_no"),
|
|
)
|
|
privateKeyPath := firstNonEmpty(
|
|
utils.GetConfigString(prefix+".pay_private_key_path"),
|
|
utils.GetConfigString("wechat.pay_private_key_path"),
|
|
)
|
|
platformCertPath := firstNonEmpty(
|
|
utils.GetConfigString(prefix+".pay_platform_cert_path"),
|
|
utils.GetConfigString("wechat.pay_platform_cert_path"),
|
|
)
|
|
apiV3Key := firstNonEmpty(
|
|
utils.GetConfigString(prefix+".pay_apiv3_key"),
|
|
utils.GetConfigString("wechat.pay_apiv3_key"),
|
|
)
|
|
notifyURL := firstNonEmpty(
|
|
utils.GetConfigString(prefix+".pay_notify_url"),
|
|
utils.GetConfigString("wechat.pay_notify_url"),
|
|
)
|
|
|
|
if appID == utils.StringEmpty || mchID == utils.StringEmpty || serialNo == utils.StringEmpty ||
|
|
privateKeyPath == utils.StringEmpty || apiV3Key == utils.StringEmpty {
|
|
return wxpay.Config{}, fmt.Errorf("wechat pay config missing for client %s", clientCode)
|
|
}
|
|
|
|
return wxpay.Config{
|
|
AppID: appID,
|
|
MchID: mchID,
|
|
CertificateSerialNo: serialNo,
|
|
PrivateKeyPath: privateKeyPath,
|
|
PlatformCertPath: platformCertPath,
|
|
APIv3Key: apiV3Key,
|
|
NotifyURL: notifyURL,
|
|
}, nil
|
|
}
|
|
|
|
func clientAppAlias(clientCode string) string {
|
|
if i := strings.IndexByte(clientCode, '_'); i > 0 {
|
|
return clientCode[:i]
|
|
}
|
|
return clientCode
|
|
}
|
|
|
|
func firstNonEmpty(values ...string) string {
|
|
for _, v := range values {
|
|
if v != utils.StringEmpty {
|
|
return v
|
|
}
|
|
}
|
|
return utils.StringEmpty
|
|
}
|