diff --git a/callback.go b/callback.go index 2d8fbd7..d9c40d8 100644 --- a/callback.go +++ b/callback.go @@ -4,6 +4,7 @@ import ( "encoding/xml" "errors" "fmt" + "github.com/fishedee/encoding" "io/ioutil" "net/http" "sort" @@ -91,7 +92,7 @@ func AliAppCallback(w http.ResponseWriter, r *http.Request) (*common.AliWebPayRe } // WeChatCallback 微信支付 -func WeChatWebCallback(w http.ResponseWriter, r *http.Request) (*common.WeChatPayResult, error) { +func WeChatCallback(w http.ResponseWriter, r *http.Request) (*common.WeChatPayResult, error) { var returnCode = "FAIL" var returnMsg = "" defer func() { @@ -101,24 +102,20 @@ func WeChatWebCallback(w http.ResponseWriter, r *http.Request) (*common.WeChatPa w.Write([]byte(returnBody)) }() var reXML common.WeChatPayResult - //body := cb.Ctx.Input.RequestBody body, err := ioutil.ReadAll(r.Body) if err != nil { - //log.Error(string(body)) returnCode = "FAIL" returnMsg = "Bodyerror" - panic(err) + return &reXML, errors.New(returnCode + ":" + returnMsg) } err = xml.Unmarshal(body, &reXML) if err != nil { - //log.Error(err, string(body)) - returnMsg = "参数错误" returnCode = "FAIL" - panic(err) + returnMsg = "参数错误" + return &reXML, errors.New(returnCode + ":" + returnMsg) } if reXML.ReturnCode != "SUCCESS" { - //log.Error(reXML) returnCode = "FAIL" return &reXML, errors.New(reXML.ReturnCode) } @@ -147,58 +144,10 @@ func WeChatWebCallback(w http.ResponseWriter, r *http.Request) (*common.WeChatPa return &reXML, nil } -func WeChatAppCallback(w http.ResponseWriter, r *http.Request) (*common.WeChatPayResult, error) { - var returnCode = "FAIL" - var returnMsg = "" - defer func() { - formatStr := ` - ![CDATA[%s]]` - returnBody := fmt.Sprintf(formatStr, returnCode, returnMsg) - w.Write([]byte(returnBody)) - }() - var reXML common.WeChatPayResult - //body := cb.Ctx.Input.RequestBody - body, err := ioutil.ReadAll(r.Body) - if err != nil { - //log.Error(string(body)) - returnCode = "FAIL" - returnMsg = "Bodyerror" - panic(err) - } - err = xml.Unmarshal(body, &reXML) - if err != nil { - //log.Error(err, string(body)) - returnMsg = "参数错误" - returnCode = "FAIL" - panic(err) - } - - if reXML.ReturnCode != "SUCCESS" { - //log.Error(reXML) - returnCode = "FAIL" - return &reXML, errors.New(reXML.ReturnCode) - } - m := util.XmlToMap(body) - - var signData []string - for k, v := range m { - if k == "sign" { - continue - } - signData = append(signData, fmt.Sprintf("%v=%v", k, v)) - } - - key := client.DefaultWechatAppClient().Key - - mySign, err := client.WechatGenSign(key, m) - if err != nil { - return &reXML, err - } - - if mySign != m["sign"] { - panic(errors.New("签名交易错误")) - } +func WeChatWebCallback(w http.ResponseWriter, r *http.Request) (*common.WeChatPayResult, error) { + return WeChatCallback(w, r) +} - returnCode = "SUCCESS" - return &reXML, nil +func WeChatAppCallback(w http.ResponseWriter, r *http.Request) (*common.WeChatPayResult, error) { + return WeChatCallback(w, r) } diff --git a/client/alipayApp.go b/client/alipayApp.go index 11bdd43..b8fa3e1 100644 --- a/client/alipayApp.go +++ b/client/alipayApp.go @@ -61,6 +61,10 @@ func (this *AliAppClient) Pay(charge *common.Charge) (map[string]string, error) return map[string]string{"orderString": this.ToURL(m)}, nil } +func (this *AliAppClient) CloseOrder(charge *common.Charge) (map[string]string, error) { + return map[string]string{}, errors.New("暂未开发该功能") +} + func (this *AliAppClient) PayToClient(charge *common.Charge) (map[string]string, error) { return map[string]string{}, errors.New("暂未开发该功能") } diff --git a/client/alipayWeb.go b/client/alipayWeb.go index d6f6063..d1856ee 100644 --- a/client/alipayWeb.go +++ b/client/alipayWeb.go @@ -55,6 +55,10 @@ func (this *AliWebClient) Pay(charge *common.Charge) (map[string]string, error) return map[string]string{"url": ToURL("https://mapi.alipay.com/gateway.do", m)}, nil } +func (this *AliWebClient) CloseOrder(charge *common.Charge) (map[string]string, error) { + return map[string]string{}, errors.New("暂未开发该功能") +} + func (this *AliWebClient) PayToClient(charge *common.Charge) (map[string]string, error) { return map[string]string{}, errors.New("暂未开发该功能") } diff --git a/client/common.go b/client/common.go index 3bbc315..2daa5cf 100644 --- a/client/common.go +++ b/client/common.go @@ -49,6 +49,48 @@ func WachatCompanyChange(mchAppid, mchid, key string, conn *HTTPSClient, charge return struct2Map(result) } +// 微信关闭订单 +func WachatCloseOrder(appid, mchid, key string, outTradeNo string) (common.WeChatQueryResult, error) { + var m = make(map[string]string) + m["appid"] = appid + m["mch_id"] = mchid + m["nonce_str"] = util.RandomStr() + m["out_trade_no"] = outTradeNo + m["sign_type"] = "MD5" + + sign, err := WechatGenSign(key, m) + if err != nil { + return common.WeChatQueryResult{}, err + } + m["sign"] = sign + + // 转出xml结构 + result, err := PostWechat("https://api.mch.weixin.qq.com/pay/closeorder", m, nil) + if err != nil { + return common.WeChatQueryResult{}, err + } + + return result, err +} + +// 微信订单查询 +func WachatQueryOrder(appID, mchID, key, tradeNum string) (common.WeChatQueryResult, error) { + var m = make(map[string]string) + m["appid"] = appID + m["mch_id"] = mchID + m["out_trade_no"] = tradeNum + m["nonce_str"] = util.RandomStr() + + sign, err := WechatGenSign(key, m) + if err != nil { + return common.WeChatQueryResult{}, err + } + + m["sign"] = sign + + return PostWechat("https://api.mch.weixin.qq.com/pay/orderquery", m, nil) +} + func WechatGenSign(key string, m map[string]string) (string, error) { var signData []string for k, v := range m { diff --git a/client/wechatApp.go b/client/wechatApp.go index 110f158..1cc565a 100644 --- a/client/wechatApp.go +++ b/client/wechatApp.go @@ -73,6 +73,11 @@ func (this *WechatAppClient) Pay(charge *common.Charge) (map[string]string, erro return c, nil } +// 关闭订单 +func (this *WechatAppClient) CloseOrder(outTradeNo string) (common.WeChatQueryResult, error) { + return WachatCloseOrder(this.AppID, this.MchID, this.Key, outTradeNo) +} + // 支付到用户的微信账号 func (this *WechatAppClient) PayToClient(charge *common.Charge) (map[string]string, error) { return WachatCompanyChange(this.AppID, this.MchID, this.Key, this.httpsClient, charge) @@ -80,18 +85,5 @@ func (this *WechatAppClient) PayToClient(charge *common.Charge) (map[string]stri // QueryOrder 查询订单 func (this *WechatAppClient) QueryOrder(tradeNum string) (common.WeChatQueryResult, error) { - var m = make(map[string]string) - m["appid"] = this.AppID - m["mch_id"] = this.MchID - m["out_trade_no"] = tradeNum - m["nonce_str"] = util.RandomStr() - - sign, err := WechatGenSign(this.Key, m) - if err != nil { - return common.WeChatQueryResult{}, err - } - - m["sign"] = sign - - return PostWechat("https://api.mch.weixin.qq.com/pay/orderquery", m, nil) + return WachatQueryOrder(this.AppID, this.MchID, this.Key, tradeNum) } diff --git a/client/wechatMiniProgram.go b/client/wechatMiniProgram.go index ffbfaf2..1f822ca 100644 --- a/client/wechatMiniProgram.go +++ b/client/wechatMiniProgram.go @@ -35,7 +35,11 @@ type WechatMiniProgramClient struct { // Pay 支付 func (this *WechatMiniProgramClient) Pay(charge *common.Charge) (map[string]string, error) { var m = make(map[string]string) - m["appid"] = this.AppID + appId := this.AppID + if charge.APPID != "" { + appId = charge.APPID + } + m["appid"] = appId m["mch_id"] = this.MchID m["nonce_str"] = util.RandomStr() m["body"] = TruncatedText(charge.Describe, 32) @@ -60,7 +64,7 @@ func (this *WechatMiniProgramClient) Pay(charge *common.Charge) (map[string]stri } var c = make(map[string]string) - c["appId"] = this.AppID + c["appId"] = appId c["timeStamp"] = fmt.Sprintf("%d", time.Now().Unix()) c["nonceStr"] = util.RandomStr() c["package"] = fmt.Sprintf("prepay_id=%s", xmlRe.PrepayID) @@ -74,6 +78,11 @@ func (this *WechatMiniProgramClient) Pay(charge *common.Charge) (map[string]stri return c, nil } +// 关闭订单 +func (this *WechatMiniProgramClient) CloseOrder(outTradeNo string) (common.WeChatQueryResult, error) { + return WachatCloseOrder(this.AppID, this.MchID, this.Key, outTradeNo) +} + // 支付到用户的微信账号 func (this *WechatMiniProgramClient) PayToClient(charge *common.Charge) (map[string]string, error) { return WachatCompanyChange(this.AppID, this.MchID, this.Key, this.httpsClient, charge) @@ -81,18 +90,5 @@ func (this *WechatMiniProgramClient) PayToClient(charge *common.Charge) (map[str // QueryOrder 查询订单 func (this *WechatMiniProgramClient) QueryOrder(tradeNum string) (common.WeChatQueryResult, error) { - var m = make(map[string]string) - m["appid"] = this.AppID - m["mch_id"] = this.MchID - m["out_trade_no"] = tradeNum - m["nonce_str"] = util.RandomStr() - - sign, err := WechatGenSign(this.Key, m) - if err != nil { - return common.WeChatQueryResult{}, err - } - - m["sign"] = sign - - return PostWechat("https://api.mch.weixin.qq.com/pay/orderquery", m, nil) + return WachatQueryOrder(this.AppID, this.MchID, this.Key, tradeNum) } diff --git a/client/wechatWeb.go b/client/wechatWeb.go index bcb31f6..31c94d6 100644 --- a/client/wechatWeb.go +++ b/client/wechatWeb.go @@ -3,8 +3,8 @@ package client import ( "errors" "fmt" - "github.com/milkbobo/gopay/common" - "github.com/milkbobo/gopay/util" + "github.com/fishedee/sdk/pay/common" + "github.com/fishedee/sdk/pay/util" "time" ) @@ -70,6 +70,11 @@ func (this *WechatWebClient) Pay(charge *common.Charge) (map[string]string, erro return c, nil } +// 关闭订单 +func (this *WechatWebClient) CloseOrder(outTradeNo string) (common.WeChatQueryResult, error) { + return WachatCloseOrder(this.AppID, this.MchID, this.Key, outTradeNo) +} + // 支付到用户的微信账号 func (this *WechatWebClient) PayToClient(charge *common.Charge) (map[string]string, error) { return WachatCompanyChange(this.AppID, this.MchID, this.Key, this.httpsClient, charge) @@ -77,18 +82,5 @@ func (this *WechatWebClient) PayToClient(charge *common.Charge) (map[string]stri // QueryOrder 查询订单 func (this *WechatWebClient) QueryOrder(tradeNum string) (common.WeChatQueryResult, error) { - var m = make(map[string]string) - m["appid"] = this.AppID - m["mch_id"] = this.MchID - m["out_trade_no"] = tradeNum - m["nonce_str"] = util.RandomStr() - - sign, err := WechatGenSign(this.Key, m) - if err != nil { - return common.WeChatQueryResult{}, err - } - - m["sign"] = sign - - return PostWechat("https://api.mch.weixin.qq.com/pay/orderquery", m, nil) + return WachatQueryOrder(this.AppID, this.MchID, this.Key, tradeNum) } diff --git a/common/common.go b/common/common.go index 438ae91..792b4a4 100644 --- a/common/common.go +++ b/common/common.go @@ -12,6 +12,7 @@ type PayClient interface { // Charge 支付参数 type Charge struct { + APPID string `json:"-"` TradeNum string `json:"tradeNum,omitempty"` Origin string `json:"origin,omitempty"` UserID string `json:"userId,omitempty"` diff --git a/constant/pay.go b/constant/pay.go index ad4c3a1..d14b027 100644 --- a/constant/pay.go +++ b/constant/pay.go @@ -1,9 +1,9 @@ package constant const ( - ALI_WEB = iota + 1 // 支付宝网页 - ALI_APP // 支付宝App - WECHAT_WEB // 微信网页 - WECHAT_APP // 微信App - WECHAT_MINI_PROGRAM // 微信小程序 + ALI_WEB = iota + 1 // 支付宝网页 + ALI_APP // 支付宝App + WECHAT_WEB // 微信网页 + WECHAT_APP // 微信App + WECHAT_MINI_PROGRAM // 微信小程序 )