123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 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
- }
|