From 97752d09a6208287b21884ba006c4287c6cec0fd Mon Sep 17 00:00:00 2001 From: Mongo Date: Sat, 28 Oct 2017 00:32:08 +0800 Subject: [PATCH 01/13] add jsapi payment demo version --- context/context.go | 2 + pay/pay.go | 91 ++++++++++++++++++++++++++++++++++++++++++++++ util/http.go | 21 +++++++++++ wechat.go | 9 +++++ 4 files changed, 123 insertions(+) create mode 100644 pay/pay.go diff --git a/context/context.go b/context/context.go index 1975863..8dd09ff 100644 --- a/context/context.go +++ b/context/context.go @@ -13,6 +13,8 @@ type Context struct { AppSecret string Token string EncodingAESKey string + PayMchID string + PayNotifyURL string Cache cache.Cache diff --git a/pay/pay.go b/pay/pay.go new file mode 100644 index 0000000..66c133b --- /dev/null +++ b/pay/pay.go @@ -0,0 +1,91 @@ +package pay + +import ( + "crypto/md5" + "strings" + "github.com/silenceper/wechat/context" + "github.com/silenceper/wechat/util" +) + +var payGateway := "https://api.mch.weixin.qq.com/pay/unifiedorder" + +// Pay struct extends context +type Pay struct { + *context.Context +} + +// PayParams was NEEDED when request unifiedorder +type PayParams struct { + TotalFee string + CreateIP string + Body string + OutTradeNo string +} + +type PayResult struct { + Success bool + PrePayID string +} + +//PayRequest +type payRequest struct { + AppID string `xml:"appid"` + MchID string `xml:"mch_id"` + NotifyUrl string `xml:"notify_url"` //通知地址 + DeviceInfo string `xml:"device_info,omitempty"` + NonceStr string `xml:"nonce_str"` + Sign string `xml:"sign"` + SignType string `xml:"sign_type,omitempty"` + Body string `xml:"body"` + Detail string `xml:"detail,omitempty"` + Attach string `xml:"attach,omitempty"` //附加数据 + OutTradeNo string `xml:"out_trade_no"` //商户订单号 + FeeType string `xml:"fee_type,omitempty"` //标价币种 + TotalFee string `xml:"total_fee"` //标价金额 + SpbillCreateIp string `xml:"spbill_create_ip"` //终端IP + TimeStart string `xml:"time_start,omitempty"` //交易起始时间 + TimeExpire string `xml:"time_expire,omitempty"` //交易结束时间 + GoodsTag string `xml:"goods_tag,omitempty"` //订单优惠标记 + TradeType string `xml:"trade_type"` //交易类型 + ProductId string `xml:"product_id,omitempty"` //商品ID + LimitPay string `xml:"limit_pay,omitempty"` // + OpenID string `xml:"openid,omitempty"` //用户标识 + SceneInfo string `xml:"scene_info,omitempty"` //场景信息 +} + +type payResponse struct { + +} + +// NewPay return an instance of Pay package +func NewPay(ctx *context.Context) *Pay { + pay := Pay{Context: ctx} + return &pay +} + +// PrePayId will request wechat merchant api and request for a pre payment order id +func (pcf *Pay) PrePayId(p *PayParams) payResult *PayResult { + nonceStr := util.RandomStr(32) + pType = "JSAPI" + template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type" + stringA := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.MchID, nonceStr, pcf.NotifyUrl, p.OutTradeNo, p.CreateIP, p.TotalFee, pType) + signature := md5.Sum(stringA + pcf.PayKey) + sign := strings.ToUpper(signature) + request := payRequest{ + AppID: pcf.AppID, + MchID: pcf.MchID, + NotifyUrl: pcf.NotifyUrl, + NonceStr: util.RandomStr(32), + Sign: sign, + Body: p.Body, + OutTradeNo: p.OutTradeNo, + TotalFee: p.TotalFee, + SpbillCreateIp: params.CreateIP, + OpenID: params.OpenID, + } + ret, err := util.PostXML(payGateway, request) + if err != nil { + + } + fmt.Println(string(ret)) +} \ No newline at end of file diff --git a/util/http.go b/util/http.go index ca98ae7..ff6edca 100644 --- a/util/http.go +++ b/util/http.go @@ -3,6 +3,7 @@ package util import ( "bytes" "encoding/json" + "encoding/xml" "fmt" "io" "io/ioutil" @@ -120,3 +121,23 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte respBody, err = ioutil.ReadAll(resp.Body) return } + +//PostXML perform a HTTP/POST request with XML body +func PostXML(uri string, obj interface{}) ([]byte, error) { + xmlData, err := xml.Marshal(obj) + if err != nil { + return nil, err + } + + body := bytes.NewBuffer(xmlData) + response, err := http.Post(uri, "application/xml;charset=utf-8", body) + if err != nil { + return nil, err + } + defer response.Body.Close() + + if response.StatusCode != http.StatusOK { + return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode) + } + return ioutil.ReadAll(response.Body) +} diff --git a/wechat.go b/wechat.go index e48de19..1dcdd2f 100644 --- a/wechat.go +++ b/wechat.go @@ -13,6 +13,7 @@ import ( "github.com/silenceper/wechat/server" "github.com/silenceper/wechat/template" "github.com/silenceper/wechat/user" + "github.com/silenceper/wechat/pay" ) // Wechat struct @@ -26,6 +27,9 @@ type Config struct { AppSecret string Token string EncodingAESKey string + PayMchID string + PayNotifyURL string //支付的通知接口 + PayKey string //商家后台设置的支付 key Cache cache.Cache } @@ -87,3 +91,8 @@ func (wc *Wechat) GetUser() *user.User { func (wc *Wechat) GetTemplate() *template.Template { return template.NewTemplate(wc.Context) } + +// GetPay 返回支付消息的实例 +func (wc *Wechat) GetPay() *pay.Pay { + return pay.NewPay(wc.Context) +} From a4a567e1d400c014f270c9c153d49963d7172def Mon Sep 17 00:00:00 2001 From: Mongo Date: Sun, 29 Oct 2017 21:01:27 +0800 Subject: [PATCH 02/13] add H5 payment support --- context/context.go | 1 + pay/pay.go | 68 ++++++++++++++++++++++++++++++++-------------- util/http.go | 2 +- wechat | 1 + 4 files changed, 50 insertions(+), 22 deletions(-) create mode 120000 wechat diff --git a/context/context.go b/context/context.go index 8dd09ff..45bcf50 100644 --- a/context/context.go +++ b/context/context.go @@ -15,6 +15,7 @@ type Context struct { EncodingAESKey string PayMchID string PayNotifyURL string + PayKey string Cache cache.Cache diff --git a/pay/pay.go b/pay/pay.go index 66c133b..86212a9 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -1,13 +1,16 @@ package pay import ( - "crypto/md5" + "errors" + "crypto/md5" + "encoding/xml" + "fmt" "strings" "github.com/silenceper/wechat/context" "github.com/silenceper/wechat/util" ) -var payGateway := "https://api.mch.weixin.qq.com/pay/unifiedorder" +var payGateway = "https://api.mch.weixin.qq.com/pay/unifiedorder" // Pay struct extends context type Pay struct { @@ -20,14 +23,26 @@ type PayParams struct { CreateIP string Body string OutTradeNo string + OpenID string } -type PayResult struct { - Success bool - PrePayID string +// payResult 是 unifie order 接口的返回 +type payResult struct { + ReturnCode string `xml:"return_code"` + ReturnMsg string `xml:"return_msg"` + AppID string `xml:"appid,omitempty"` + MchID string `xml:"mch_id,omitempty"` + NonceStr string `xml:"nonce_str,omitempty"` + Sign string `xml:"sign,omitempty"` + ResultCode string `xml:"result_code,omitempty"` + TradeType string `xml:"trade_type,omitempty"` + PrePayID string `xml:"prepay_id,omitempty"` + CodeURL string `xml:"code_url,omitempty"` + ErrCode string `xml:"err_code,omitempty"` + ErrCodeDes string `xml:"err_code_des,omitempty"` } -//PayRequest +//payRequest 接口请求参数 type payRequest struct { AppID string `xml:"appid"` MchID string `xml:"mch_id"` @@ -53,10 +68,6 @@ type payRequest struct { SceneInfo string `xml:"scene_info,omitempty"` //场景信息 } -type payResponse struct { - -} - // NewPay return an instance of Pay package func NewPay(ctx *context.Context) *Pay { pay := Pay{Context: ctx} @@ -64,28 +75,43 @@ func NewPay(ctx *context.Context) *Pay { } // PrePayId will request wechat merchant api and request for a pre payment order id -func (pcf *Pay) PrePayId(p *PayParams) payResult *PayResult { +func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, err error) { nonceStr := util.RandomStr(32) - pType = "JSAPI" + pType := "JSAPI" template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type" - stringA := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.MchID, nonceStr, pcf.NotifyUrl, p.OutTradeNo, p.CreateIP, p.TotalFee, pType) - signature := md5.Sum(stringA + pcf.PayKey) + str := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.PayMchID, nonceStr, pcf.PayNotifyURL, p.OutTradeNo, p.CreateIP, p.TotalFee, pType) + str += pcf.PayKey + sum := md5.Sum([]byte(str)) + signature := string(sum[:]) sign := strings.ToUpper(signature) request := payRequest{ AppID: pcf.AppID, - MchID: pcf.MchID, - NotifyUrl: pcf.NotifyUrl, + MchID: pcf.PayMchID, + NotifyUrl: pcf.PayNotifyURL, NonceStr: util.RandomStr(32), Sign: sign, Body: p.Body, OutTradeNo: p.OutTradeNo, TotalFee: p.TotalFee, - SpbillCreateIp: params.CreateIP, - OpenID: params.OpenID, + SpbillCreateIp: p.CreateIP, + OpenID: p.OpenID, } - ret, err := util.PostXML(payGateway, request) + rawRet, err := util.PostXML(payGateway, request) if err != nil { - + return "", err + } + payRet := payResult{} + err = xml.Unmarshal(rawRet, &payRet) + if err != nil { + return "", errors.New(err.Error()) + } + if payRet.ReturnCode == "SUCCESS" { + //pay success + if payRet.ResultCode == "SUCCESS" { + return payRet.PrePayID, nil + } + return "", errors.New(payRet.ErrCode + payRet.ErrCodeDes) + } else { + return "", errors.New("xml unmarshal err : raw - " + string(rawRet)) } - fmt.Println(string(ret)) } \ No newline at end of file diff --git a/util/http.go b/util/http.go index ff6edca..6881052 100644 --- a/util/http.go +++ b/util/http.go @@ -137,7 +137,7 @@ func PostXML(uri string, obj interface{}) ([]byte, error) { defer response.Body.Close() if response.StatusCode != http.StatusOK { - return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode) + return nil, fmt.Errorf("http code error : uri=%v , statusCode=%v", uri, response.StatusCode) } return ioutil.ReadAll(response.Body) } diff --git a/wechat b/wechat new file mode 120000 index 0000000..4e7c289 --- /dev/null +++ b/wechat @@ -0,0 +1 @@ +wechat \ No newline at end of file From 0db6094b242ccbee005164000a3f6e14bcd3c5c6 Mon Sep 17 00:00:00 2001 From: Mongo Date: Mon, 30 Oct 2017 15:28:41 +0800 Subject: [PATCH 03/13] fix bug --- pay/pay.go | 10 ++++++++++ wechat.go | 8 ++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pay/pay.go b/pay/pay.go index 86212a9..6ca11a9 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -17,6 +17,7 @@ type Pay struct { *context.Context } +// 传入的参数,用于生成 prepay_id 的必需参数 // PayParams was NEEDED when request unifiedorder type PayParams struct { TotalFee string @@ -26,6 +27,15 @@ type PayParams struct { OpenID string } +// PayConfig 是传出用于 jsdk 用的参数 +type PayConfig struct { + Timestamp int64 + NonceStr string + PrePayID string + SignType string + Sign string +} + // payResult 是 unifie order 接口的返回 type payResult struct { ReturnCode string `xml:"return_code"` diff --git a/wechat.go b/wechat.go index 1dcdd2f..7c84644 100644 --- a/wechat.go +++ b/wechat.go @@ -27,9 +27,9 @@ type Config struct { AppSecret string Token string EncodingAESKey string - PayMchID string - PayNotifyURL string //支付的通知接口 - PayKey string //商家后台设置的支付 key + PayMchID string //支付 - 商户 ID + PayNotifyURL string //支付 - 接受微信支付结果通知的接口地址 + PayKey string //支付 - 商户后台设置的支付 key Cache cache.Cache } @@ -95,4 +95,4 @@ func (wc *Wechat) GetTemplate() *template.Template { // GetPay 返回支付消息的实例 func (wc *Wechat) GetPay() *pay.Pay { return pay.NewPay(wc.Context) -} +} \ No newline at end of file From 016a9a58d9627697102ab525b60646287cca54b6 Mon Sep 17 00:00:00 2001 From: Mongo Date: Mon, 30 Oct 2017 21:50:51 +0800 Subject: [PATCH 04/13] add config copy --- wechat.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/wechat.go b/wechat.go index 7c84644..02e3a34 100644 --- a/wechat.go +++ b/wechat.go @@ -45,6 +45,9 @@ func copyConfigToContext(cfg *Config, context *context.Context) { context.AppSecret = cfg.AppSecret context.Token = cfg.Token context.EncodingAESKey = cfg.EncodingAESKey + context.PayMchID = cfg.PayMchID + context.PayKey = cfg.PayKey + context.PayNotifyURL = cfg.PayNotifyURL context.Cache = cfg.Cache context.SetAccessTokenLock(new(sync.RWMutex)) context.SetJsAPITicketLock(new(sync.RWMutex)) From d199cc947b4019f08419b8afccb7a2dad6a11270 Mon Sep 17 00:00:00 2001 From: Mongo Date: Mon, 30 Oct 2017 21:55:48 +0800 Subject: [PATCH 05/13] fix parameter trade_type --- pay/pay.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pay/pay.go b/pay/pay.go index 6ca11a9..ad8b9cb 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -88,7 +88,7 @@ func NewPay(ctx *context.Context) *Pay { func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, err error) { nonceStr := util.RandomStr(32) pType := "JSAPI" - template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type" + template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type=%s" str := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.PayMchID, nonceStr, pcf.PayNotifyURL, p.OutTradeNo, p.CreateIP, p.TotalFee, pType) str += pcf.PayKey sum := md5.Sum([]byte(str)) @@ -108,7 +108,7 @@ func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, err error) { } rawRet, err := util.PostXML(payGateway, request) if err != nil { - return "", err + return "", errors.New(err.Error() + " parameters : " + str) } payRet := payResult{} err = xml.Unmarshal(rawRet, &payRet) From 1d2f06365e584bd598294d1d0d5e4e04c1d624cb Mon Sep 17 00:00:00 2001 From: Mongo Date: Tue, 31 Oct 2017 16:45:59 +0800 Subject: [PATCH 06/13] fix payment bug --- pay/pay.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pay/pay.go b/pay/pay.go index ad8b9cb..dc61dd5 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -87,10 +87,10 @@ func NewPay(ctx *context.Context) *Pay { // PrePayId will request wechat merchant api and request for a pre payment order id func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, err error) { nonceStr := util.RandomStr(32) - pType := "JSAPI" + tradeType := "JSAPI" template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type=%s" - str := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.PayMchID, nonceStr, pcf.PayNotifyURL, p.OutTradeNo, p.CreateIP, p.TotalFee, pType) - str += pcf.PayKey + str := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.PayMchID, nonceStr, pcf.PayNotifyURL, p.OutTradeNo, p.CreateIP, p.TotalFee, tradeType) + str += "&key="pcf.PayKey sum := md5.Sum([]byte(str)) signature := string(sum[:]) sign := strings.ToUpper(signature) @@ -98,13 +98,14 @@ func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, err error) { AppID: pcf.AppID, MchID: pcf.PayMchID, NotifyUrl: pcf.PayNotifyURL, - NonceStr: util.RandomStr(32), + NonceStr: nonceStr, Sign: sign, Body: p.Body, OutTradeNo: p.OutTradeNo, TotalFee: p.TotalFee, SpbillCreateIp: p.CreateIP, OpenID: p.OpenID, + TradeType: tradeType, } rawRet, err := util.PostXML(payGateway, request) if err != nil { From a87ef15482c803772a7494a57a9e3feb885ea54a Mon Sep 17 00:00:00 2001 From: Mongo Date: Tue, 31 Oct 2017 16:48:04 +0800 Subject: [PATCH 07/13] fix payment typo --- pay/pay.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pay/pay.go b/pay/pay.go index dc61dd5..4527e93 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -90,7 +90,7 @@ func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, err error) { tradeType := "JSAPI" template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type=%s" str := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.PayMchID, nonceStr, pcf.PayNotifyURL, p.OutTradeNo, p.CreateIP, p.TotalFee, tradeType) - str += "&key="pcf.PayKey + str := str + "&key=" + pcf.PayKey sum := md5.Sum([]byte(str)) signature := string(sum[:]) sign := strings.ToUpper(signature) From 0762d6c249d31ab9c72ab535c5401391bb7e06ab Mon Sep 17 00:00:00 2001 From: Mongo Date: Tue, 31 Oct 2017 16:51:20 +0800 Subject: [PATCH 08/13] fix payment params error --- pay/pay.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pay/pay.go b/pay/pay.go index 4527e93..720bc73 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -90,7 +90,7 @@ func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, err error) { tradeType := "JSAPI" template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type=%s" str := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.PayMchID, nonceStr, pcf.PayNotifyURL, p.OutTradeNo, p.CreateIP, p.TotalFee, tradeType) - str := str + "&key=" + pcf.PayKey + str = str + "&key=" + pcf.PayKey sum := md5.Sum([]byte(str)) signature := string(sum[:]) sign := strings.ToUpper(signature) From a9bfce3fbe9954811437b9a0b2569775d84e53b1 Mon Sep 17 00:00:00 2001 From: Mongo Date: Tue, 31 Oct 2017 19:22:43 +0800 Subject: [PATCH 09/13] fix signature calculate --- pay/pay.go | 13 ++++--------- util/crypto.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/pay/pay.go b/pay/pay.go index 720bc73..bf7e6df 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -2,10 +2,8 @@ package pay import ( "errors" - "crypto/md5" "encoding/xml" "fmt" - "strings" "github.com/silenceper/wechat/context" "github.com/silenceper/wechat/util" ) @@ -88,12 +86,9 @@ func NewPay(ctx *context.Context) *Pay { func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, err error) { nonceStr := util.RandomStr(32) tradeType := "JSAPI" - template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&out_trade_no=%s&spbill_create_ip=%s&total_fee=%s&trade_type=%s" - str := fmt.Sprintf(template, pcf.AppID, p.Body, pcf.PayMchID, nonceStr, pcf.PayNotifyURL, p.OutTradeNo, p.CreateIP, p.TotalFee, tradeType) - str = str + "&key=" + pcf.PayKey - sum := md5.Sum([]byte(str)) - signature := string(sum[:]) - sign := strings.ToUpper(signature) + template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%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.OutTradeNo, p.CreateIP, p.TotalFee, tradeType, pcf.PayKey) + sign := util.Md5Sum(str) request := payRequest{ AppID: pcf.AppID, MchID: pcf.PayMchID, @@ -123,6 +118,6 @@ func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, err error) { } return "", errors.New(payRet.ErrCode + payRet.ErrCodeDes) } else { - return "", errors.New("xml unmarshal err : raw - " + string(rawRet)) + return "", errors.New("[msg : xmlUnmarshalError] [rawReturn : " + string(rawRet) + "] [params : " + str + "] [sign : " + sign + "]") } } \ No newline at end of file diff --git a/util/crypto.go b/util/crypto.go index 1ec7a4f..124b141 100644 --- a/util/crypto.go +++ b/util/crypto.go @@ -1,9 +1,13 @@ package util import ( + "bytes" + "bufio" "crypto/aes" "crypto/cipher" + "crypto/md5" "encoding/base64" + "encoding/hex" "fmt" ) @@ -181,3 +185,15 @@ func decodeNetworkByteOrder(orderBytes []byte) (n uint32) { uint32(orderBytes[2])<<8 | uint32(orderBytes[3]) } + +// 计算 32 位长度的 MD5 sum +func Md5Sum(txt string) (sum string) { + h := md5.New() + buf := bufio.NewWriterSize(h, 128) + buf.WriteString(txt) + buf.Flush() + sign := make([]byte, hex.EncodedLen(h.Size())) + hex.Encode(sign, h.Sum(nil)) + sum = string(bytes.ToUpper(sign)) + return +} From 610bee53a1d3ae90b2c66b3e04738e8c75bfd318 Mon Sep 17 00:00:00 2001 From: Mongo Date: Wed, 1 Nov 2017 20:06:33 +0800 Subject: [PATCH 10/13] fix sign parameters \n remove openid which not needed --- pay/pay.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pay/pay.go b/pay/pay.go index bf7e6df..c21d6aa 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -54,7 +54,6 @@ type payResult struct { type payRequest struct { AppID string `xml:"appid"` MchID string `xml:"mch_id"` - NotifyUrl string `xml:"notify_url"` //通知地址 DeviceInfo string `xml:"device_info,omitempty"` NonceStr string `xml:"nonce_str"` Sign string `xml:"sign"` @@ -69,6 +68,7 @@ type payRequest struct { TimeStart string `xml:"time_start,omitempty"` //交易起始时间 TimeExpire string `xml:"time_expire,omitempty"` //交易结束时间 GoodsTag string `xml:"goods_tag,omitempty"` //订单优惠标记 + NotifyUrl string `xml:"notify_url"` //通知地址 TradeType string `xml:"trade_type"` //交易类型 ProductId string `xml:"product_id,omitempty"` //商品ID LimitPay string `xml:"limit_pay,omitempty"` // @@ -92,14 +92,13 @@ func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, err error) { request := payRequest{ AppID: pcf.AppID, MchID: pcf.PayMchID, - NotifyUrl: pcf.PayNotifyURL, NonceStr: nonceStr, Sign: sign, Body: p.Body, OutTradeNo: p.OutTradeNo, TotalFee: p.TotalFee, SpbillCreateIp: p.CreateIP, - OpenID: p.OpenID, + NotifyUrl: pcf.PayNotifyURL, TradeType: tradeType, } rawRet, err := util.PostXML(payGateway, request) From c4ba989322b4f26c0176822959435f4c98276e6f Mon Sep 17 00:00:00 2001 From: Mongo Date: Wed, 1 Nov 2017 20:10:14 +0800 Subject: [PATCH 11/13] openid needed when pay with JSAPI --- pay/pay.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pay/pay.go b/pay/pay.go index c21d6aa..9c48212 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -86,8 +86,8 @@ func NewPay(ctx *context.Context) *Pay { func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, err error) { nonceStr := util.RandomStr(32) tradeType := "JSAPI" - template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%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.OutTradeNo, p.CreateIP, p.TotalFee, tradeType, pcf.PayKey) + 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) sign := util.Md5Sum(str) request := payRequest{ AppID: pcf.AppID, @@ -100,6 +100,7 @@ func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, err error) { SpbillCreateIp: p.CreateIP, NotifyUrl: pcf.PayNotifyURL, TradeType: tradeType, + OpenID: p.OpenID, } rawRet, err := util.PostXML(payGateway, request) if err != nil { From 4b67aa76bb0c801b916aba8666115fbf7c2b298e Mon Sep 17 00:00:00 2001 From: Mongo Date: Wed, 1 Nov 2017 21:05:47 +0800 Subject: [PATCH 12/13] change md5 sum to HMAC-SHA256 --- pay/pay.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pay/pay.go b/pay/pay.go index 9c48212..af2c16b 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -86,9 +86,10 @@ func NewPay(ctx *context.Context) *Pay { func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, 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) - sign := util.Md5Sum(str) + template := "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&openid=%s&out_trade_no=%s&sign_type=%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, "HMAC-SHA256", p.CreateIP, p.TotalFee, tradeType, pcf.PayKey) + // sign := util.Md5Sum(str) + sign := util.Signature(str) request := payRequest{ AppID: pcf.AppID, MchID: pcf.PayMchID, From f7b6cff271d0fcbf5a76c5bf6eecdddc7da78607 Mon Sep 17 00:00:00 2001 From: Mongo Date: Wed, 1 Nov 2017 21:46:26 +0800 Subject: [PATCH 13/13] change crypt method to MD5 --- pay/pay.go | 7 +++---- util/crypto.go | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pay/pay.go b/pay/pay.go index af2c16b..b9995d8 100644 --- a/pay/pay.go +++ b/pay/pay.go @@ -86,10 +86,9 @@ func NewPay(ctx *context.Context) *Pay { func (pcf *Pay) PrePayId(p *PayParams) (prePayID string, 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&sign_type=%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, "HMAC-SHA256", p.CreateIP, p.TotalFee, tradeType, pcf.PayKey) - // sign := util.Md5Sum(str) - sign := util.Signature(str) + 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) + sign := util.MD5Sum(str) request := payRequest{ AppID: pcf.AppID, MchID: pcf.PayMchID, diff --git a/util/crypto.go b/util/crypto.go index 124b141..98e2b4e 100644 --- a/util/crypto.go +++ b/util/crypto.go @@ -187,7 +187,7 @@ func decodeNetworkByteOrder(orderBytes []byte) (n uint32) { } // 计算 32 位长度的 MD5 sum -func Md5Sum(txt string) (sum string) { +func MD5Sum(txt string) (sum string) { h := md5.New() buf := bufio.NewWriterSize(h, 128) buf.WriteString(txt)