mirror of
https://github.com/silenceper/wechat.git
synced 2026-03-01 00:35:26 +08:00
Merge pull request #168 from cielu/master
支付 新增 BridgeConfig 方法,可获得 prepay ID,及js支付时所需要的参数
This commit is contained in:
+77
-8
@@ -2,10 +2,17 @@ package pay
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/md5"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"errors"
|
"errors"
|
||||||
|
"hash"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/context"
|
"github.com/silenceper/wechat/context"
|
||||||
"github.com/silenceper/wechat/util"
|
"github.com/silenceper/wechat/util"
|
||||||
@@ -27,15 +34,21 @@ type Params struct {
|
|||||||
OutTradeNo string
|
OutTradeNo string
|
||||||
OpenID string
|
OpenID string
|
||||||
TradeType string
|
TradeType string
|
||||||
|
SignType string
|
||||||
|
Detail string
|
||||||
|
Attach string
|
||||||
|
GoodsTag string
|
||||||
|
NotifyURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config 是传出用于 jsdk 用的参数
|
// Config 是传出用于 js sdk 用的参数
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Timestamp int64
|
Timestamp string `json:"timestamp"`
|
||||||
NonceStr string
|
NonceStr string `json:"nonceStr"`
|
||||||
PrePayID string
|
PrePayID string `json:"prePayId"`
|
||||||
SignType string
|
SignType string `json:"signType"`
|
||||||
Sign string
|
Package string `json:"package"`
|
||||||
|
PaySign string `json:"paySign"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PreOrder 是 unifie order 接口的返回
|
// PreOrder 是 unifie order 接口的返回
|
||||||
@@ -86,20 +99,72 @@ func NewPay(ctx *context.Context) *Pay {
|
|||||||
return &pay
|
return &pay
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BridgeConfig get js bridge config
|
||||||
|
func (pcf *Pay) BridgeConfig(p *Params) (cfg Config, err error) {
|
||||||
|
var (
|
||||||
|
buffer strings.Builder
|
||||||
|
h hash.Hash
|
||||||
|
timestamp = strconv.FormatInt(time.Now().Unix(), 10)
|
||||||
|
)
|
||||||
|
order, err := pcf.PrePayOrder(p)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
buffer.WriteString("appId=")
|
||||||
|
buffer.WriteString(order.AppID)
|
||||||
|
buffer.WriteString("&nonceStr=")
|
||||||
|
buffer.WriteString(order.NonceStr)
|
||||||
|
buffer.WriteString("&package=")
|
||||||
|
buffer.WriteString("prepay_id=" + order.PrePayID)
|
||||||
|
buffer.WriteString("&signType=")
|
||||||
|
buffer.WriteString(p.SignType)
|
||||||
|
buffer.WriteString("&timeStamp=")
|
||||||
|
buffer.WriteString(timestamp)
|
||||||
|
buffer.WriteString("&key=")
|
||||||
|
buffer.WriteString(pcf.PayKey)
|
||||||
|
if p.SignType == "MD5" {
|
||||||
|
h = md5.New()
|
||||||
|
} else {
|
||||||
|
h = hmac.New(sha256.New, []byte(pcf.PayKey))
|
||||||
|
}
|
||||||
|
h.Write([]byte(buffer.String()))
|
||||||
|
// 签名
|
||||||
|
cfg.PaySign = strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
|
||||||
|
cfg.NonceStr = order.NonceStr
|
||||||
|
cfg.Timestamp = timestamp
|
||||||
|
cfg.PrePayID = order.PrePayID
|
||||||
|
cfg.SignType = p.SignType
|
||||||
|
cfg.Package = "prepay_id=" + order.PrePayID
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// PrePayOrder return data for invoke wechat payment
|
// PrePayOrder return data for invoke wechat payment
|
||||||
func (pcf *Pay) PrePayOrder(p *Params) (payOrder PreOrder, err error) {
|
func (pcf *Pay) PrePayOrder(p *Params) (payOrder PreOrder, err error) {
|
||||||
nonceStr := util.RandomStr(32)
|
nonceStr := util.RandomStr(32)
|
||||||
|
notifyURL := pcf.PayNotifyURL
|
||||||
|
// 签名类型
|
||||||
|
if p.SignType == "" {
|
||||||
|
p.SignType = "MD5"
|
||||||
|
}
|
||||||
|
// 通知地址
|
||||||
|
if p.NotifyURL != "" {
|
||||||
|
notifyURL = p.NotifyURL
|
||||||
|
}
|
||||||
param := make(map[string]interface{})
|
param := make(map[string]interface{})
|
||||||
param["appid"] = pcf.AppID
|
param["appid"] = pcf.AppID
|
||||||
param["body"] = p.Body
|
param["body"] = p.Body
|
||||||
param["mch_id"] = pcf.PayMchID
|
param["mch_id"] = pcf.PayMchID
|
||||||
param["nonce_str"] = nonceStr
|
param["nonce_str"] = nonceStr
|
||||||
param["notify_url"] = pcf.PayNotifyURL
|
|
||||||
param["out_trade_no"] = p.OutTradeNo
|
param["out_trade_no"] = p.OutTradeNo
|
||||||
param["spbill_create_ip"] = p.CreateIP
|
param["spbill_create_ip"] = p.CreateIP
|
||||||
param["total_fee"] = p.TotalFee
|
param["total_fee"] = p.TotalFee
|
||||||
param["trade_type"] = p.TradeType
|
param["trade_type"] = p.TradeType
|
||||||
param["openid"] = p.OpenID
|
param["openid"] = p.OpenID
|
||||||
|
param["sign_type"] = p.SignType
|
||||||
|
param["detail"] = p.Detail
|
||||||
|
param["attach"] = p.Attach
|
||||||
|
param["goods_tag"] = p.GoodsTag
|
||||||
|
param["notify_url"] = notifyURL
|
||||||
|
|
||||||
bizKey := "&key=" + pcf.PayKey
|
bizKey := "&key=" + pcf.PayKey
|
||||||
str := orderParam(param, bizKey)
|
str := orderParam(param, bizKey)
|
||||||
@@ -113,9 +178,13 @@ func (pcf *Pay) PrePayOrder(p *Params) (payOrder PreOrder, err error) {
|
|||||||
OutTradeNo: p.OutTradeNo,
|
OutTradeNo: p.OutTradeNo,
|
||||||
TotalFee: p.TotalFee,
|
TotalFee: p.TotalFee,
|
||||||
SpbillCreateIP: p.CreateIP,
|
SpbillCreateIP: p.CreateIP,
|
||||||
NotifyURL: pcf.PayNotifyURL,
|
NotifyURL: notifyURL,
|
||||||
TradeType: p.TradeType,
|
TradeType: p.TradeType,
|
||||||
OpenID: p.OpenID,
|
OpenID: p.OpenID,
|
||||||
|
SignType: p.SignType,
|
||||||
|
Detail: p.Detail,
|
||||||
|
Attach: p.Attach,
|
||||||
|
GoodsTag: p.GoodsTag,
|
||||||
}
|
}
|
||||||
rawRet, err := util.PostXML(payGateway, request)
|
rawRet, err := util.PostXML(payGateway, request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user