xingzai 9 bulan lalu
induk
melakukan
fde9987c54
2 mengubah file dengan 88 tambahan dan 43 penghapusan
  1. 34 43
      services/wx_pay.go
  2. 54 0
      utils/wechat_cert.go

+ 34 - 43
services/wx_pay.go

@@ -6,43 +6,41 @@ import (
 	"errors"
 	"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_mfyx/models"
 	"hongze/hongze_mfyx/models/order"
 	"hongze/hongze_mfyx/utils"
-	"log"
 	"time"
 )
 
-const (
-	MchPKFileName              = "./utils/cert/apiclient_key.pem"
-	Mchid                      = "1624495680"
-	MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
-	MchAPIv3Key                = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
-)
-
-// 微信商户建立连接
-func getWechatClient() (context.Context, *core.Client, error) {
-	// 使用 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
-}
+//const (
+//	MchPKFileName              = "./utils/cert/apiclient_key.pem"
+//	Mchid                      = "1624495680"
+//	MchCertificateSerialNumber = "5ED2719CFAE5205763034AD80BF4B8A33533C418"
+//	MchAPIv3Key                = "W1tbnzQrzQ7yRRNuQCIHjis8dgdasKVX"
+//)
+//
+//// 微信商户建立连接
+//func getWechatClient() (context.Context, *core.Client, error) {
+//	// 使用 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 ExampleJsapiApiServicePrepay(orderDetail *order.CygxOrder, unionId string) (JsapiApiResp order.PrepayWithRequestPaymentResponse, err error) {
@@ -59,22 +57,18 @@ func ExampleJsapiApiServicePrepay(orderDetail *order.CygxOrder, unionId string)
 		err = errors.New("GetUserRecordByUnionId, Err: " + e.Error())
 		return
 	}
-	ctx, client, err := getWechatClient()
-	if err != nil {
-		log.Printf("getWechatClientt err:%s", err)
-		return
-	}
-	svc := jsapi.JsapiApiService{Client: client}
+	ctx := context.Background()
+	svc := jsapi.JsapiApiService{Client: utils.WechatCertClient}
 	// 得到prepay_id,以及调起支付所需的参数和签名
 	resp, _, err := svc.PrepayWithRequestPayment(ctx,
 		jsapi.PrepayRequest{
 			Appid:       core.String(utils.WxAppId),
-			Mchid:       core.String(Mchid),
+			Mchid:       core.String(utils.Mchid),
 			Description: core.String(orderDetail.SourceTitle),
 			OutTradeNo:  core.String(orderDetail.OutTradeNo),
 			Attach:      core.String(""),
 			NotifyUrl:   core.String(utils.WxPayJsapiNotifyUrl),
-			TimeExpire:  core.Time(time.Now()),
+			TimeExpire:  core.Time(time.Now().Add(10 * time.Minute)),
 			Amount: &jsapi.Amount{
 				Total: core.Int64(int64(orderDetail.OrderMoney * 100)), // 分
 			},
@@ -83,8 +77,9 @@ func ExampleJsapiApiServicePrepay(orderDetail *order.CygxOrder, unionId string)
 			},
 		},
 	)
+
 	if err != nil {
-		log.Printf("PrepayWithRequestPayment err:%s", err)
+		//log.Printf("PrepayWithRequestPayment err:%s", err)
 		return
 	}
 	JsapiApiResp.PrepayId = *resp.PrepayId
@@ -137,11 +132,7 @@ func WxDecodeNotify(body []byte) (wechatPayCallback *WechatPayCallback) {
 	associatedData := prepaymap2["associated_data"].(string)
 	ciphertext := prepaymap2["ciphertext"].(string)
 
-	tx, e := payUtils.DecryptAES256GCM(MchAPIv3Key, associatedData, nonce, ciphertext)
-	if e != nil {
-		log.Println(err)
-		return
-	}
+	tx, e := payUtils.DecryptAES256GCM(utils.MchAPIv3Key, associatedData, nonce, ciphertext)
 	if e != nil {
 		err = errors.New("DecryptAES256GCM, Err: " + e.Error())
 		return

+ 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 {
+			go SendAlarmMsg("微信商户链接失败 os.Stat"+err.Error(), 2)
+			fmt.Println(err)
+			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
+}