Skip to content

Commit

Permalink
微信支付添加关闭订单功能,优化支付回调,优化企业支付
Browse files Browse the repository at this point in the history
  • Loading branch information
milkbobo committed Jun 14, 2019
1 parent 045469b commit 9cb1c2e
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 112 deletions.
71 changes: 10 additions & 61 deletions callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"github.com/fishedee/encoding"
"io/ioutil"
"net/http"
"sort"
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 := `<xml><return_code><![CDATA[%s]]></return_code>
<return_msg>![CDATA[%s]]</return_msg></xml>`
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)
}
4 changes: 4 additions & 0 deletions client/alipayApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("暂未开发该功能")
}
Expand Down
4 changes: 4 additions & 0 deletions client/alipayWeb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("暂未开发该功能")
}
Expand Down
42 changes: 42 additions & 0 deletions client/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 6 additions & 14 deletions client/wechatApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,17 @@ 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)
}

// 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)
}
28 changes: 12 additions & 16 deletions client/wechatMiniProgram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -74,25 +78,17 @@ 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)
}

// 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)
}
24 changes: 8 additions & 16 deletions client/wechatWeb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -70,25 +70,17 @@ 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)
}

// 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)
}
1 change: 1 addition & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
10 changes: 5 additions & 5 deletions constant/pay.go
Original file line number Diff line number Diff line change
@@ -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 // 微信小程序
)

0 comments on commit 9cb1c2e

Please sign in to comment.