Skip to content

Commit

Permalink
Merge pull request #3 from mailbaoer/master
Browse files Browse the repository at this point in the history
add waxNotify support
  • Loading branch information
aimuz authored Jun 27, 2018
2 parents 38e9c42 + fac306d commit be2cbab
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
## 快速开始
以下是APP和小程序支付简单例子
```go
import "github.com/aimuz/wechat-sdk/pay"

wePay := &WePay{
AppId: "xxx",
MchId: "xxx",
Expand All @@ -25,6 +27,25 @@ results, outTradeNo, err := wePay.WaxPay(100, "open_id") // 金额,以分为
```

## 使用
### 小程序支付通知
```go
waxNotify := pay.WaxPayNotifyReq{}
ctx.ReadXML(&waxNotify)
verifyParams := pay.WaxVerifyParams(waxNotify)
valid := pay.WaxpayVerifySign(verifyParams, appKey, waxNotify.Sign) //appKey 为自己在微信支付后台设置的API密钥

resp := new(pay.WaxPayNotifyResp)

if valid {
业务处理逻辑···
resp.ReturnCode = "SUCCESS"
resp.ReturnMsg = "OK"
} else {
错误处理逻辑···
resp.ReturnCode = "FAIL"
resp.ReturnMsg = "Verify Failed"
}
```


#### APP支付
Expand Down
71 changes: 67 additions & 4 deletions pay/check.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,69 @@
package pay

//// 验证支付回调
//func () {
//
//}
import (
"github.com/aimuz/wechat-sdk/utils"
"strconv"
)

type (
WaxPayNotifyReq struct {
AppID string `xml:"appid"`
MchID string `xml:"mch_id"`
BankType string `xml:"bank_type"`
CashFee float64 `xml:"cash_fee"`
FeeType string `xml:"fee_type"`
IsSubscribe string `xml:"is_subscribe"`

NonceStr string `xml:"nonce_str"`
OpenID string `xml:"openid"`
OutTradeNo string `xml:"out_trade_no"`
ResultCode string `xml:"result_code"`
ReturnCode string `xml:"return_code"`
Sign string `xml:"sign"`
TimeEnd string `xml:"time_end"`
TotalFee float64 `xml:"total_fee"`
TradeType string `xml:"trade_type"`
TransactionID string `xml:"transaction_id"`
}

WaxPayNotifyResp struct {
ReturnCode string `xml:"return_code"`
ReturnMsg string `xml:"return_msg"`
}
)

/**
* 微信通知验证
*/

// 微信小程序待验证参数
func WaxVerifyParams(req WaxPayNotifyReq) map[string]string {
verifyParams := make(map[string]string)

verifyParams["appid"] = req.AppID
verifyParams["bank_type"] = req.BankType
verifyParams["cash_fee"] = strconv.FormatFloat(req.CashFee, 'f', 0, 64)
verifyParams["fee_type"] = req.FeeType
verifyParams["is_subscribe"] = req.IsSubscribe
verifyParams["mch_id"] = req.MchID
verifyParams["nonce_str"] = req.NonceStr
verifyParams["openid"] = req.OpenID
verifyParams["out_trade_no"] = req.OutTradeNo
verifyParams["result_code"] = req.ResultCode
verifyParams["return_code"] = req.ReturnCode
verifyParams["time_end"] = req.TimeEnd
verifyParams["total_fee"] = strconv.FormatFloat(req.TotalFee, 'f', 0, 64)
verifyParams["trade_type"] = req.TradeType
verifyParams["transaction_id"] = req.TransactionID

return verifyParams
}

// 微信小程序支付签名验证
func WaxpayVerifySign(verifyParams map[string]string, signKey string, sign string) bool {
signCalc, _ := utils.GenWeChatPaySign(verifyParams, signKey)
if sign == signCalc {
return true
}
return false
}
5 changes: 4 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -96,7 +97,9 @@ func GenWeChatPaySign(m map[string]string, payKey string) (string, error) {
if err != nil {
return "", err
}
return fmt.Sprintf("%x", signByte), nil

sign := strings.ToUpper(hex.EncodeToString(signByte))
return sign, nil
}

// GetTradeNO 生成订单号,不推荐直接使用
Expand Down

0 comments on commit be2cbab

Please sign in to comment.