diff --git a/app/third/paymentService/ali_service.go b/app/third/paymentService/ali_service.go index 48a74ad..76faa56 100644 --- a/app/third/paymentService/ali_service.go +++ b/app/third/paymentService/ali_service.go @@ -14,25 +14,16 @@ import ( "github.com/go-pay/gopay/alipay" "github.com/qit-team/snow-core/log/logger" "strconv" - "sync" -) - -var ( - aliClient *alipay.Client - aliClientErr error - aliOnce sync.Once ) // AliInitClient 使用提供的支付请求参数初始化支付宝客户端 -func AliInitClient(aliConfig AliPay) { +func AliInitClient(aliConfig AliPay) (*alipay.Client, error) { envConfig := config.GetConf() - aliOnce.Do(func() { - // 初始化支付宝客户端 - // appid:应用ID - // privateKey:应用私钥,支持PKCS1和PKCS8 - // isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。 - aliClient, aliClientErr = alipay.NewClient(aliConfig.AppId, aliConfig.PrivateKey, envConfig.PayService.IsProd) - }) + // 初始化支付宝客户端 + // appid:应用ID + // privateKey:应用私钥,支持PKCS1和PKCS8 + // isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。 + aliClient, aliClientErr := alipay.NewClient(aliConfig.AppId, aliConfig.PrivateKey, envConfig.PayService.IsProd) // 自定义配置http请求接收返回结果body大小,默认 10MB aliClient.SetBodySize(10) // 没有特殊需求,可忽略此配置 @@ -51,10 +42,6 @@ func AliInitClient(aliConfig AliPay) { // 证书内容 aliClientErr = aliClient.SetCertSnByContent([]byte(aliConfig.AppPublicCert), []byte(aliConfig.AlipayRootCert), []byte(aliConfig.AlipayPublicCert)) -} - -// GetAliClient 获取已经初始化的支付宝客户端 -func GetAliClient() (*alipay.Client, error) { if aliClient == nil { return nil, errors.New("client not initialized") } @@ -67,10 +54,7 @@ func GetAliClient() (*alipay.Client, error) { // ALiH5PayInfo 支付宝手机网站支付 func ALiH5PayInfo(c context.Context, payOrderRequest PayOrderRequest) (string, error) { // 初始化支付宝客户端 - AliInitClient(payOrderRequest.Ali) - - // 获取支付宝客户端 - aliClient, err := GetAliClient() + aliClient, err := AliInitClient(payOrderRequest.Ali) if err != nil { fmt.Println("Failed to get client:", err) return "", err @@ -147,14 +131,12 @@ func ALiCallBack(notifyReq gopay.BodyMap, aliConfig AliPay) error { // ALiOrderQuery 查询支付宝订单 func ALiOrderQuery(ctx context.Context, aliConfig AliPay, OrderNo string) (PayOrderQueryInfo, error) { // 初始化支付宝客户端 - AliInitClient(aliConfig) - - // 获取支付宝客户端 - aliClient, err := GetAliClient() + aliClient, err := AliInitClient(aliConfig) if err != nil { fmt.Println("Failed to get client:", err) return PayOrderQueryInfo{}, err } + // 请求参数 bm := make(gopay.BodyMap) bm.Set("out_trade_no", OrderNo) @@ -210,14 +192,12 @@ func ALiOrderQuery(ctx context.Context, aliConfig AliPay, OrderNo string) (PayOr // AliRefundOrder 支付宝退款申请 func AliRefundOrder(ctx context.Context, orderRefundRequest OrderRefundRequest) (OrderRefundInfo, error) { // 初始化支付宝客户端 - AliInitClient(orderRefundRequest.Ali) - - // 获取支付宝客户端 - aliClient, err := GetAliClient() + aliClient, err := AliInitClient(orderRefundRequest.Ali) if err != nil { fmt.Println("Failed to get client:", err) return OrderRefundInfo{}, err } + // 请求参数 refundAmount := float64(orderRefundRequest.RefundAmount) / 100.0 bm := make(gopay.BodyMap) @@ -251,10 +231,7 @@ func AliRefundOrder(ctx context.Context, orderRefundRequest OrderRefundRequest) // AliRefundOrderQuery 支付宝订单退款查询 func AliRefundOrderQuery(ctx context.Context, orderRefundQueryRequest OrderRefundQueryRequest) (OrderRefundInfo, error) { // 初始化支付宝客户端 - AliInitClient(orderRefundQueryRequest.Ali) - - // 获取支付宝客户端 - aliClient, err := GetAliClient() + aliClient, err := AliInitClient(orderRefundQueryRequest.Ali) if err != nil { fmt.Println("Failed to get client:", err) return OrderRefundInfo{}, err @@ -290,10 +267,7 @@ func AliRefundOrderQuery(ctx context.Context, orderRefundQueryRequest OrderRefun // AliCloseOrder 支付宝订单关闭 func AliCloseOrder(ctx context.Context, orderCloseRequest OrderCloseRequest) (OrderCloseInfo, error) { // 初始化支付宝客户端 - AliInitClient(orderCloseRequest.Ali) - - // 获取支付宝客户端 - aliClient, err := GetAliClient() + aliClient, err := AliInitClient(orderCloseRequest.Ali) if err != nil { fmt.Println("Failed to get client:", err) return OrderCloseInfo{}, err diff --git a/app/third/paymentService/wechat_service.go b/app/third/paymentService/wechat_service.go index 8cb27d0..1a33eba 100644 --- a/app/third/paymentService/wechat_service.go +++ b/app/third/paymentService/wechat_service.go @@ -14,62 +14,48 @@ import ( "github.com/go-pay/gopay/wechat/v3" "github.com/qit-team/snow-core/log/logger" "strconv" - "sync" "time" ) -var ( - wxClient *wechat.ClientV3 - clientErr error - once sync.Once -) - // InitClient 使用提供的支付请求参数初始化微信客户端 -func InitClient(wxConfig WxPay) { - once.Do(func() { - // NewClientV3 初始化微信客户端 v3 - // mchid:商户ID 或者服务商模式的 sp_mchid - // serialNo:商户证书的证书序列号 - // apiV3Key:apiV3Key,商户平台获取 - // privateKey:私钥 apiclient_key.pem 读取后的内容 - wxClient, clientErr = wechat.NewClientV3( - wxConfig.MchId, - wxConfig.SerialNo, - wxConfig.ApiV3Key, - wxConfig.PrivateKey, - ) - }) +func InitClient(wxConfig WxPay) (*wechat.ClientV3, error) { + // NewClientV3 初始化微信客户端 v3 + // mchid:商户ID 或者服务商模式的 sp_mchid + // serialNo:商户证书的证书序列号 + // apiV3Key:apiV3Key,商户平台获取 + // privateKey:私钥 apiclient_key.pem 读取后的内容 + wxClient, clientErr := wechat.NewClientV3( + wxConfig.MchId, + wxConfig.SerialNo, + wxConfig.ApiV3Key, + wxConfig.PrivateKey, + ) // 启用自动同步返回验签,并定时更新微信平台API证书(开启自动验签时,无需单独设置微信平台API证书和序列号) clientErr = wxClient.AutoVerifySign() - - // 自定义配置http请求接收返回结果body大小,默认 10MB - wxClient.SetBodySize(10) // 没有特殊需求,可忽略此配置 - - // 打开Debug开关,输出日志,默认是关闭的 - wxClient.DebugSwitch = gopay.DebugOn -} - -// GetClient 获取已经初始化的微信客户端 -func GetClient() (*wechat.ClientV3, error) { if wxClient == nil { return nil, errors.New("client not initialized") } if clientErr != nil { return nil, clientErr } + + // 自定义配置http请求接收返回结果body大小,默认 10MB + wxClient.SetBodySize(10) // 没有特殊需求,可忽略此配置 + + // 打开Debug开关,输出日志,默认是关闭的 + wxClient.DebugSwitch = gopay.DebugOn return wxClient, nil + } // WxH5PayInfo 微信H5支付 func WxH5PayInfo(c context.Context, payOrderRequest PayOrderRequest) (string, error) { // 初始化微信客户端 - InitClient(payOrderRequest.Wx) - - // 获取微信客户端 - wxClient, err := GetClient() + wxClient, err := InitClient(payOrderRequest.Wx) if err != nil { return "", err } + expire := time.Now().Add(10 * time.Minute).Format(time.RFC3339) // 初始化 BodyMap envConfig := config.GetConf() @@ -105,10 +91,7 @@ func WxH5PayInfo(c context.Context, payOrderRequest PayOrderRequest) (string, er // WxPayCallBack 微信支付回调 func WxPayCallBack(notifyReq *wechat.V3NotifyReq, wxConfig WxPay) error { // 初始化微信客户端 - InitClient(wxConfig) - - // 获取微信客户端 - wxClient, err := GetClient() + wxClient, err := InitClient(wxConfig) if err != nil { return err } @@ -181,10 +164,7 @@ func WxPayCallBack(notifyReq *wechat.V3NotifyReq, wxConfig WxPay) error { // WxOrderQuery 查询微信订单 func WxOrderQuery(ctx context.Context, wxConfig WxPay, orderNo string) (PayOrderQueryInfo, error) { // 初始化微信客户端 - InitClient(wxConfig) - - // 获取微信客户端 - wxClient, err := GetClient() + wxClient, err := InitClient(wxConfig) if err != nil { return PayOrderQueryInfo{}, err } @@ -228,13 +208,11 @@ func WxOrderQuery(ctx context.Context, wxConfig WxPay, orderNo string) (PayOrder // WxOrderRefund 微信退款申请 func WxOrderRefund(ctx context.Context, orderRefundRequest OrderRefundRequest) (OrderRefundInfo, error) { // 初始化微信客户端 - InitClient(orderRefundRequest.Wx) - - // 获取微信客户端 - wxClient, err := GetClient() + wxClient, err := InitClient(orderRefundRequest.Wx) if err != nil { return OrderRefundInfo{}, err } + // 初始化 BodyMap bm := make(gopay.BodyMap) bm.Set("out_trade_no", strconv.FormatInt(orderRefundRequest.OrderId, 10)). @@ -288,10 +266,7 @@ func WxOrderRefund(ctx context.Context, orderRefundRequest OrderRefundRequest) ( // WxOrderRefundQuery 微信订单退款查询 func WxOrderRefundQuery(ctx context.Context, orderRefundQueryRequest OrderRefundQueryRequest) (OrderRefundInfo, error) { // 初始化微信客户端 - InitClient(orderRefundQueryRequest.Wx) - - // 获取微信客户端 - wxClient, err := GetClient() + wxClient, err := InitClient(orderRefundQueryRequest.Wx) if err != nil { return OrderRefundInfo{}, err } @@ -329,13 +304,11 @@ func WxOrderRefundQuery(ctx context.Context, orderRefundQueryRequest OrderRefund // WxCloseOrder 微信订单关闭 func WxCloseOrder(ctx context.Context, orderCloseRequest OrderCloseRequest) (OrderCloseInfo, error) { // 初始化微信客户端 - InitClient(orderCloseRequest.Wx) - - // 获取微信客户端 - wxClient, err := GetClient() + wxClient, err := InitClient(orderCloseRequest.Wx) if err != nil { return OrderCloseInfo{}, err } + wxRsp, err := wxClient.V3TransactionCloseOrder(ctx, strconv.FormatInt(orderCloseRequest.OrderId, 10)) if err != nil { return OrderCloseInfo{}, err