1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
wx_mp.go 2.28 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2022-06-01 16:56 . go.mod module
package client
import (
"errors"
"fmt"
"gitee.com/h79/goutils/pay/common"
"gitee.com/h79/goutils/pay/util"
"time"
)
var defaultWechatMiniProgramClient *WxMPClient
func InitWxMiniProgramClient(c *WxMPClient) {
if len(c.PrivateKey) != 0 && len(c.PublicKey) != 0 {
c.httpsClient = NewHTTPSClient(c.PublicKey, c.PrivateKey)
}
defaultWechatMiniProgramClient = c
}
func DefaultWxMPClient() *WxMPClient {
return defaultWechatMiniProgramClient
}
// WxMPClient 微信小程序
type WxMPClient struct {
WxClient
}
// Pay 支付
func (client *WxMPClient) Pay(charge *common.Charge) (map[string]string, error) {
var m = make(map[string]string)
appId := client.AppID
if charge.AppId != "" {
appId = charge.AppId
}
m["appid"] = appId
m["mch_id"] = client.MchID
m["nonce_str"] = util.RandomStr()
m["body"] = TruncatedText(charge.Describe, 32)
m["out_trade_no"] = charge.TradeNum
m["total_fee"] = WxMoneyFeeToString(charge.MoneyFee)
m["spbill_create_ip"] = util.LocalIP()
m["notify_url"] = charge.CallbackURL
m["trade_type"] = "JSAPI"
m["openid"] = charge.OpenId
m["sign_type"] = "MD5"
sign, err := WxGenSign(client.Key, m)
if err != nil {
return map[string]string{}, err
}
m["sign"] = sign
// 转出xml结构
xmlRe, err := PostWx(client.PayURL, m, nil)
if err != nil {
return map[string]string{}, err
}
var c = make(map[string]string)
c["appId"] = appId
c["timeStamp"] = fmt.Sprintf("%d", time.Now().Unix())
c["nonceStr"] = util.RandomStr()
c["package"] = fmt.Sprintf("prepay_id=%s", xmlRe.PrepayID)
c["signType"] = "MD5"
sign2, err := WxGenSign(client.Key, c)
if err != nil {
return map[string]string{}, errors.New("WechatWeb: " + err.Error())
}
c["paySign"] = sign2
delete(c, "appId")
return c, nil
}
// 关闭订单
func (client *WxMPClient) CloseOrder(outTradeNo string) (common.WxQueryResult, error) {
return WxCloseOrder(client.AppID, client.MchID, client.Key, outTradeNo)
}
// 支付到用户的微信账号
func (client *WxMPClient) PayToClient(charge *common.Charge) (map[string]string, error) {
return WxCompanyChange(client.AppID, client.MchID, client.Key, client.httpsClient, charge)
}
// QueryOrder 查询订单
func (client *WxMPClient) QueryOrder(tradeNum string) (common.WxQueryResult, error) {
return WxQueryOrder(client.AppID, client.MchID, client.Key, tradeNum)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.2.14

搜索帮助