Преглед изворни кода

Merge branch 'mfyx_2.0' of http://8.136.199.33:3000/hongze/hongze_cygx into debug

xingzai пре 9 месеци
родитељ
комит
b94e47427f
2 измењених фајлова са 95 додато и 43 уклоњено
  1. 41 43
      services/wx_pay.go
  2. 54 0
      utils/wechat_cert.go

+ 41 - 43
services/wx_pay.go

@@ -5,48 +5,45 @@ import (
 	"encoding/json"
 	"fmt"
 	"github.com/wechatpay-apiv3/wechatpay-go/core"
-	"github.com/wechatpay-apiv3/wechatpay-go/core/option"
 	"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
-	payUtils "github.com/wechatpay-apiv3/wechatpay-go/utils"
 	"hongze/hongze_cygx/utils"
-	"log"
 	"time"
 )
 
-const (
-	MchPKFileName              = "../hongze_mfyx/utils/cert/apiclient_key.pem"
-	Mchid                      = "1624495680"
-	MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
-	MchAPIv3Key                = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
-)
+//const (
+//	MchPKFileName              = "../hongze_mfyx/utils/cert/apiclient_key.pem"
+//	Mchid                      = "1624495680"
+//	MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
+//	MchAPIv3Key                = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
+//)
 
-// 微信商户建立连接
-func getWechatClient() (context.Context, *core.Client, error) {
-	var err error
-	defer func() {
-		if err != nil {
-			fmt.Println("err", err)
-			go utils.SendAlarmMsg(fmt.Sprint("微信商户建立连接失败 getWechatClient, err:", err.Error()), 2)
-		}
-	}()
-	// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
-	mchPrivateKey, err := payUtils.LoadPrivateKeyWithPath(MchPKFileName)
-	if err != nil {
-		log.Print("load merchant private key error")
-		return nil, nil, err
-	}
-	ctx := context.Background()
-	// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
-	opts := []core.ClientOption{
-		option.WithWechatPayAutoAuthCipher(Mchid, MchCertificateSerialNumber, mchPrivateKey, MchAPIv3Key),
-	}
-	client, err := core.NewClient(ctx, opts...)
-	if err != nil {
-		log.Printf("new wechat pay client err:%s", err)
-		return nil, nil, err
-	}
-	return ctx, client, nil
-}
+//// 微信商户建立连接
+//func getWechatClient() (context.Context, *core.Client, error) {
+//	var err error
+//	defer func() {
+//		if err != nil {
+//			fmt.Println("err", err)
+//			go utils.SendAlarmMsg(fmt.Sprint("微信商户建立连接失败 getWechatClient, err:", err.Error()), 2)
+//		}
+//	}()
+//	// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
+//	mchPrivateKey, err := payUtils.LoadPrivateKeyWithPath(MchPKFileName)
+//	if err != nil {
+//		log.Print("load merchant private key error")
+//		return nil, nil, err
+//	}
+//	ctx := context.Background()
+//	// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
+//	opts := []core.ClientOption{
+//		option.WithWechatPayAutoAuthCipher(Mchid, MchCertificateSerialNumber, mchPrivateKey, MchAPIv3Key),
+//	}
+//	client, err := core.NewClient(ctx, opts...)
+//	if err != nil {
+//		log.Printf("new wechat pay client err:%s", err)
+//		return nil, nil, err
+//	}
+//	return ctx, client, nil
+//}
 
 //func init() {
 //	GetQueryOrderByOutTradeNo("OD202404081631545174")
@@ -61,16 +58,17 @@ func GetQueryOrderByOutTradeNo(outTradeNo string) (tradeState string, statusCode
 			go utils.SendAlarmMsg(fmt.Sprint("根据订单号查询订单状态失败 GetQueryOrderByOutTradeNo, err:", err.Error(), "outTradeNo", outTradeNo), 2)
 		}
 	}()
-	ctx, client, err := getWechatClient()
-	if err != nil {
-		log.Printf("getWechatClientt err:%s", err)
-		return
-	}
-	svc := jsapi.JsapiApiService{Client: client}
+	//ctx, client, err := getWechatClient()
+	//if err != nil {
+	//	log.Printf("getWechatClientt err:%s", err)
+	//	return
+	//}
+	ctx := context.Background()
+	svc := jsapi.JsapiApiService{Client: utils.WechatCertClient}
 	resp, result, err := svc.QueryOrderByOutTradeNo(ctx,
 		jsapi.QueryOrderByOutTradeNoRequest{
 			OutTradeNo: core.String(outTradeNo),
-			Mchid:      core.String(Mchid),
+			Mchid:      core.String(utils.Mchid),
 		},
 	)
 	statusCode = result.Response.StatusCode

+ 54 - 0
utils/wechat_cert.go

@@ -0,0 +1,54 @@
+package utils
+
+import (
+	"context"
+	"fmt"
+	"github.com/wechatpay-apiv3/wechatpay-go/core"
+	"github.com/wechatpay-apiv3/wechatpay-go/core/option"
+	payUtils "github.com/wechatpay-apiv3/wechatpay-go/utils"
+	"os"
+)
+
+var WechatCertClient *core.Client
+
+const (
+	//MchPKFileName = "../hongze_mfyx/utils/cert/apiclient_key.pem"
+	MchPKFileName              = "/home/code/wechat_cert/cygx/apiclient_key.pem"
+	Mchid                      = "1624495680"
+	MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
+	MchAPIv3Key                = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
+)
+
+// 微信商户建立连接
+func init() {
+	if RunMode != "release" {
+		_, err := os.Stat(MchPKFileName) // 判断证书路径是否存在,非生产环境不做判断。
+		if err != nil {
+			//fmt.Println(err)
+			go SendAlarmMsg("微信商户链接失败 os.Stat"+err.Error(), 2)
+			FileLog.Info("WechatCertClient err:" + err.Error())
+			err = nil
+			return
+		}
+	}
+
+	// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
+	mchPrivateKey, err := payUtils.LoadPrivateKeyWithPath(MchPKFileName)
+	if err != nil {
+		fmt.Println(err)
+		go SendAlarmMsg("微信商户链接失败 LoadPrivateKeyWithPath"+err.Error(), 2)
+	}
+	ctx := context.Background()
+	// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
+	opts := []core.ClientOption{
+		option.WithWechatPayAutoAuthCipher(Mchid, MchCertificateSerialNumber, mchPrivateKey, MchAPIv3Key),
+	}
+
+	client, err := core.NewClient(ctx, opts...)
+	if err != nil {
+		fmt.Println(err)
+		go SendAlarmMsg("微信商户链接失败 NewClient"+err.Error(), 2)
+	}
+	WechatCertClient = client
+	return
+}