package services import ( "context" "fmt" "github.com/wechatpay-apiv3/wechatpay-go/core" "github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi" "github.com/wechatpay-apiv3/wechatpay-go/services/payments/native" "hongze/hongze_web_mfyx/models/order" "hongze/hongze_web_mfyx/utils" "time" ) //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 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 init() { // ExampleNativeApiService_Prepay() //} func ExampleNativeApiServicePrepay(orderDetail *order.CygxOrder) (codeUrl string, err error) { defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg(fmt.Sprint("生成预支付交易单失败 ExampleNativeApiServicePrepay, err:", err.Error(), "OrderCode:", orderDetail.OrderCode), 2) } }() //如果是试用卡,商品名称就显示日卡月卡,如果是单场付费就显示,单场活动名称 if orderDetail.OrderType == 1 { orderDetail.SourceTitle = "畅读" + orderDetail.GoodsName } //ctx, client, _ := getWechatClient() ctx := context.Background() svc := native.NativeApiService{Client: utils.WechatCertClient} resp, _, err := svc.Prepay(ctx, native.PrepayRequest{ Appid: core.String(utils.WxAppIdMfyx), Mchid: core.String(utils.Mchid), Description: core.String(orderDetail.SourceTitle), OutTradeNo: core.String(orderDetail.OutTradeNo), TimeExpire: core.Time(time.Now().Add(10 * time.Minute)), Attach: core.String(""), NotifyUrl: core.String(utils.WxPayJsapiNotifyUrl), GoodsTag: core.String(""), SupportFapiao: core.Bool(false), Amount: &native.Amount{ Currency: core.String("CNY"), Total: core.Int64(int64(orderDetail.OrderMoney * 100)), // 分 }, }, ) if err != nil { fmt.Println(err) return } codeUrl = *resp.CodeUrl return } type WechatPayCallback struct { MchID string `json:"mchid"` AppID string `json:"appid"` OutTradeNo string `json:"out_trade_no"` TransactionID string `json:"transaction_id"` TradeType string `json:"trade_type"` TradeState string `json:"trade_state"` TradeStateDesc string `json:"trade_state_desc"` BankType string `json:"bank_type"` Attach string `json:"attach"` SuccessTime time.Time `json:"success_time"` Payer struct { OpenID string `json:"openid"` } `json:"payer"` Amount struct { Total int `json:"total"` PayerTotal int `json:"payer_total"` Currency string `json:"currency"` PayerCurrency string `json:"payer_currency"` } `json:"amount"` } // 订单超时手动关闭订单 func ExampleNativeApiService_CloseOrder(OutTradeNo string) { var err error defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg(fmt.Sprint("订单超时手动关闭订单 失败 ExampleNativeApiService_CloseOrder, err:", err.Error(), "OutTradeNo:", OutTradeNo), 2) } }() ctx := context.Background() svc := native.NativeApiService{Client: utils.WechatCertClient} _, err = svc.CloseOrder(ctx, native.CloseOrderRequest{ OutTradeNo: core.String(OutTradeNo), Mchid: core.String(utils.Mchid), }, ) return } // 订单超时手动关闭订单(两者功能更一样,与小程序端代码统一) func ExampleJsapiApiService_CloseOrder(OutTradeNo string) { var err error defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg(fmt.Sprint("订单超时手动关闭订单 失败 ExampleJsapiApiService_CloseOrder, err:", err.Error(), "OutTradeNo:", OutTradeNo), 2) } }() ctx := context.Background() svc := jsapi.JsapiApiService{Client: utils.WechatCertClient} _, err = svc.CloseOrder(ctx, jsapi.CloseOrderRequest{ OutTradeNo: core.String(OutTradeNo), Mchid: core.String(utils.Mchid), }, ) return }