代码拉取完成,页面将自动刷新
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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。