iedc-go/dri/wechat/pay.go

143 lines
4.1 KiB
Go
Raw Permalink Normal View History

2022-05-03 14:40:02 +00:00
package wechat
import (
"context"
2022-05-04 16:29:30 +00:00
"fmt"
2022-05-03 14:40:02 +00:00
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/wechat/v3"
2022-05-04 16:29:30 +00:00
"net/http"
2022-05-03 14:40:02 +00:00
"time"
)
//基于此文档开发
//https://github.com/silenceper/wechat/blob/v2/doc/api/officialaccount.md
type wxpay struct {
2022-05-04 16:29:30 +00:00
client *wechat.ClientV3
ctx context.Context
apiV3Key string
MchId string
2022-05-03 14:40:02 +00:00
}
var WxPay = wxpay{}
// Init 初始化
func (that *wxpay) Init(MchId, SerialNo, APIv3Key, PrivateKey string) {
client, err := wechat.NewClientV3(MchId, SerialNo, APIv3Key, PrivateKey)
if err != nil {
2022-05-04 16:29:30 +00:00
//xlog.Error(err)
fmt.Println(err)
2022-05-03 14:40:02 +00:00
return
}
that.client = client
2022-05-04 16:29:30 +00:00
that.apiV3Key = APIv3Key
that.MchId = MchId
2022-05-03 14:40:02 +00:00
// 设置微信平台API证书和序列号如开启自动验签请忽略此步骤
//client.SetPlatformCert([]byte(""), "")
that.ctx = context.Background()
// 启用自动同步返回验签并定时更新微信平台API证书开启自动验签时无需单独设置微信平台API证书和序列号
err = client.AutoVerifySign()
if err != nil {
2022-05-04 16:29:30 +00:00
//xlog.Error(err)
fmt.Println(err)
2022-05-03 14:40:02 +00:00
return
}
// 打开Debug开关输出日志默认是关闭的
client.DebugSwitch = gopay.DebugOn
}
// GetUserInfo 获取用户信息
2022-05-04 16:29:30 +00:00
func (that *wxpay) GetJsOrder(money int64, appid, openid, name, tradeNo, notifyUrl string) (jsApiParams *wechat.JSAPIPayParams, err error) {
fmt.Println("dasdas", money, appid, name, tradeNo, notifyUrl)
PrepayId, err := that.getPrepayId(money, appid, that.MchId, openid, name, tradeNo, notifyUrl)
2022-05-03 14:40:02 +00:00
if err != nil {
return nil, err
}
2022-05-04 16:29:30 +00:00
2022-05-03 14:40:02 +00:00
//小程序
jsapi, err := that.client.PaySignOfJSAPI(appid, PrepayId)
return jsapi, err
}
2022-05-04 16:29:30 +00:00
func (that *wxpay) CallbackJsOrder(req *http.Request) (*wechat.V3DecryptResult, error) {
2022-05-03 14:40:02 +00:00
2022-05-04 16:29:30 +00:00
notifyReq, err := wechat.V3ParseNotify(req)
2022-05-03 14:40:02 +00:00
if err != nil {
2022-05-04 16:29:30 +00:00
//xlog.Error(err)
2022-05-03 14:40:02 +00:00
return nil, err
}
2022-05-04 16:29:30 +00:00
// wxPublicKey 通过 client.WxPublicKey() 获取
err = notifyReq.VerifySignByPK(that.client.WxPublicKey())
if err != nil {
//xlog.Error(err)
return nil, err
}
2022-05-03 14:40:02 +00:00
2022-05-04 16:29:30 +00:00
// ========异步通知敏感信息解密========
// 普通支付通知解密
result, err := notifyReq.DecryptCipherText(that.apiV3Key)
//that.client.V3TransactionQueryOrder(that.ctx,result.BankType,result.OR)
return result, err
// 合单支付通知解密
//result, err := notifyReq.DecryptCombineCipherText(apiV3Key)
//// 退款通知解密
//result, err := notifyReq.DecryptRefundCipherText(apiV3Key)
// ========异步通知应答========
// 退款通知http应答码为200且返回状态码为SUCCESS才会当做商户接收成功否则会重试。
// 注意:重试过多会导致微信支付端积压过多通知而堵塞,影响其他正常通知。
// 此写法是 gin 框架返回微信的写法
//c.JSON(http.StatusOK, &wechat.V3NotifyRsp{Code: gopay.SUCCESS, Message: "成功"})
//
//// 此写法是 echo 框架返回微信的写法
//return c.JSON(http.StatusOK, &wechat.V3NotifyRsp{Code: gopay.SUCCESS, Message: "成功"})
2022-05-03 14:40:02 +00:00
}
2022-05-04 16:29:30 +00:00
// GetUserInfo 获取用户信息
//func (that *wxpay) GetMiniOrder(money int64,appid,name,tradeNo,notifyUrl string) (jsApiParams *wechat.AppletParams,err error){
//
// PrepayId,err:=that.getPrepayId(money,name,tradeNo,notifyUrl)
// if err!=nil{
// return nil,err
// }
//
// //小程序
// applet, err := that.client.PaySignOfApplet(appid,PrepayId)
//
// return applet,err
//}
func (that *wxpay) getPrepayId(money int64, appid, mchid, openid, name, tradeNo, notifyUrl string) (prepayid string, err error) {
2022-05-03 14:40:02 +00:00
expire := time.Now().Add(10 * time.Minute).Format(time.RFC3339)
// 初始化 BodyMap
bm := make(gopay.BodyMap)
2022-05-04 16:29:30 +00:00
bm.Set("appid", appid).
Set("mchid", mchid).
//Set("sub_mchid", "sub_mchid").
2022-05-03 14:40:02 +00:00
Set("description", name).
Set("out_trade_no", tradeNo).
Set("time_expire", expire).
Set("notify_url", notifyUrl).
SetBodyMap("amount", func(bm gopay.BodyMap) {
bm.Set("total", money).
Set("currency", "CNY")
}).
SetBodyMap("payer", func(bm gopay.BodyMap) {
2022-05-04 16:29:30 +00:00
bm.Set("openid", openid)
2022-05-03 14:40:02 +00:00
})
//ctx:=context.Context()
wxRsp, err := that.client.V3TransactionJsapi(that.ctx, bm)
2022-05-04 16:29:30 +00:00
fmt.Println("获取PrepayId", wxRsp, err)
2022-05-03 14:40:02 +00:00
if err != nil {
2022-05-04 16:29:30 +00:00
//xlog.Error(err)
2022-05-03 14:40:02 +00:00
return "", err
}
return wxRsp.Response.PrepayId, nil
}