diff --git a/context/context.go b/context/context.go index 1975863..45bcf50 100644 --- a/context/context.go +++ b/context/context.go @@ -13,6 +13,9 @@ type Context struct { AppSecret string Token string EncodingAESKey string + PayMchID string + PayNotifyURL string + PayKey string Cache cache.Cache diff --git a/pay/pay.go b/pay/pay.go new file mode 100644 index 0000000..b9995d8 --- /dev/null +++ b/pay/pay.go @@ -0,0 +1,123 @@ +package pay + +import ( + "errors" + "encoding/xml" + "fmt" + "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 +} + +// 传入的参数,用于生成 prepay_id 的必需参数 +// PayParams was NEEDED when request unifiedorder +type PayParams struct { + TotalFee string + CreateIP string + Body string + OutTradeNo string + 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"` + 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 接口请求参数 +type payRequest struct { + AppID string `xml:"appid"` + MchID string `xml:"mch_id"` + 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"` //订单优惠标记 + NotifyUrl string `xml:"notify_url"` //通知地址 + 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"` //场景信息 +} + +// 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) (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) + request := payRequest{ + AppID: pcf.AppID, + MchID: pcf.PayMchID, + NonceStr: nonceStr, + Sign: sign, + Body: p.Body, + OutTradeNo: p.OutTradeNo, + TotalFee: p.TotalFee, + SpbillCreateIp: p.CreateIP, + NotifyUrl: pcf.PayNotifyURL, + TradeType: tradeType, + OpenID: p.OpenID, + } + rawRet, err := util.PostXML(payGateway, request) + if err != nil { + return "", errors.New(err.Error() + " parameters : " + str) + } + 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("[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..98e2b4e 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 +} diff --git a/util/http.go b/util/http.go index ca98ae7..6881052 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 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 diff --git a/wechat.go b/wechat.go index e48de19..02e3a34 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 //支付 - 商户 ID + PayNotifyURL string //支付 - 接受微信支付结果通知的接口地址 + PayKey string //支付 - 商户后台设置的支付 key Cache cache.Cache } @@ -41,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)) @@ -87,3 +94,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) +} \ No newline at end of file