|
@@ -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
|