From a6b1c56c253545e16c5e3497a521bd730fc14718 Mon Sep 17 00:00:00 2001 From: song kang Date: Wed, 19 Dec 2018 17:59:17 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=B8=8B=E5=8D=95?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9tradeType:=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E4=BB=8Eparam=E4=B8=AD=E6=8C=87=E5=AE=9A=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E4=BB=80=E4=B9=88tradeType;=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=80=85=E6=A0=B9=E6=8D=AE=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=9C=BA=E6=99=AF=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pay/pay.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pay/pay.go b/pay/pay.go index 0b2c601..54df3aa 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -24,6 +24,7 @@ type Params struct { Body string OutTradeNo string OpenID string + TradeType string } // Config 是传出用于 jsdk 用的参数 @@ -86,9 +87,8 @@ func NewPay(ctx *context.Context) *Pay { // PrePayOrder return data for invoke wechat payment func (pcf *Pay) PrePayOrder(p *Params) (payOrder PreOrder, err error) { nonceStr := util.RandomStr(32) - tradeType := "JSAPI" template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&openid=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type=%s&key=%s" - str := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.PayMchID, nonceStr, pcf.PayNotifyURL, p.OpenID, p.OutTradeNo, p.CreateIP, p.TotalFee, tradeType, pcf.PayKey) + str := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.PayMchID, nonceStr, pcf.PayNotifyURL, p.OpenID, p.OutTradeNo, p.CreateIP, p.TotalFee, p.TradeType, pcf.PayKey) sign := util.MD5Sum(str) request := payRequest{ AppID: pcf.AppID, @@ -100,7 +100,7 @@ func (pcf *Pay) PrePayOrder(p *Params) (payOrder PreOrder, err error) { TotalFee: p.TotalFee, SpbillCreateIP: p.CreateIP, NotifyURL: pcf.PayNotifyURL, - TradeType: tradeType, + TradeType: p.TradeType, OpenID: p.OpenID, } rawRet, err := util.PostXML(payGateway, request) From d5302dbfdcb1eb9f736320a196f5dfb69b45b8fd Mon Sep 17 00:00:00 2001 From: song kang Date: Wed, 19 Dec 2018 18:58:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E4=B8=8B=E5=8D=95=E6=8E=A5=E5=8F=A3=E7=AD=BE=E5=90=8D=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9A=E5=A6=82=E6=9E=9C=E5=8F=82=E6=95=B0=E5=80=BC?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=E5=80=BC=EF=BC=8C=E5=88=99=E4=B8=8D=E5=8F=82?= =?UTF-8?q?=E4=B8=8E=E7=AD=BE=E5=90=8D=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pay/pay.go | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/pay/pay.go b/pay/pay.go index 54df3aa..dd9fafd 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -1,9 +1,11 @@ package pay import ( + "bytes" "encoding/xml" "errors" - "fmt" + "sort" + "strconv" "github.com/silenceper/wechat/context" "github.com/silenceper/wechat/util" @@ -87,8 +89,20 @@ func NewPay(ctx *context.Context) *Pay { // PrePayOrder return data for invoke wechat payment func (pcf *Pay) PrePayOrder(p *Params) (payOrder PreOrder, err error) { nonceStr := util.RandomStr(32) - template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&openid=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type=%s&key=%s" - str := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.PayMchID, nonceStr, pcf.PayNotifyURL, p.OpenID, p.OutTradeNo, p.CreateIP, p.TotalFee, p.TradeType, pcf.PayKey) + param := make(map[string]interface{}) + param["appid"] = pcf.AppID + param["body"] = p.Body + param["mch_id"] = pcf.PayMchID + param["nonce_str"] =nonceStr + param["notify_url"] =pcf.PayNotifyURL + param["out_trade_no"] =p.OutTradeNo + param["spbill_create_ip"] =p.CreateIP + param["total_fee"] =p.TotalFee + param["trade_type"] =p.TradeType + param["openid"] = p.OpenID + + bizKey := "&key="+pcf.PayKey + str := orderParam(param,bizKey) sign := util.MD5Sum(str) request := payRequest{ AppID: pcf.AppID, @@ -136,3 +150,63 @@ func (pcf *Pay) PrePayID(p *Params) (prePayID string, err error) { prePayID = order.PrePayID return } + +// order params +func orderParam(source interface{}, bizKey string) (returnStr string) { + switch v := source.(type) { + case map[string]string: + keys := make([]string, 0, len(v)) + for k := range v { + if k == "sign" { + continue + } + keys = append(keys, k) + } + sort.Strings(keys) + var buf bytes.Buffer + for _, k := range keys { + if v[k] == "" { + continue + } + if buf.Len() > 0 { + buf.WriteByte('&') + } + buf.WriteString(k) + buf.WriteByte('=') + buf.WriteString(v[k]) + } + buf.WriteString(bizKey) + returnStr = buf.String() + case map[string]interface{}: + keys := make([]string, 0, len(v)) + for k := range v { + if k == "sign" { + continue + } + keys = append(keys, k) + } + sort.Strings(keys) + var buf bytes.Buffer + for _, k := range keys { + if v[k] == "" { + continue + } + if buf.Len() > 0 { + buf.WriteByte('&') + } + buf.WriteString(k) + buf.WriteByte('=') + switch vv := v[k].(type) { + case string: + buf.WriteString(vv) + case int: + buf.WriteString(strconv.FormatInt(int64(vv), 10)) + default: + panic("params type not supported") + } + } + buf.WriteString(bizKey) + returnStr = buf.String() + } + return +}