微信支付支持退款
This commit is contained in:
@@ -1,17 +1,16 @@
|
||||
package wxpay
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"github.com/clbanning/mxj"
|
||||
"github.com/nanjishidu/gomini/gocrypto"
|
||||
)
|
||||
|
||||
type CData string
|
||||
@@ -69,7 +68,7 @@ type RefundReqInfo struct {
|
||||
OutRefundNo string `json:"out_refund_no"`
|
||||
OutTradeNo string `json:"out_trade_no"`
|
||||
RefundAccount string `json:"refund_account"`
|
||||
RefundFee string `json:"refund_fee"`
|
||||
RefundFee string `json:"refund_fee" xml:"refund_fee"`
|
||||
RefundID string `json:"refund_id"`
|
||||
RefundRecvAccout string `json:"refund_recv_accout"`
|
||||
RefundRequestSource string `json:"refund_request_source"`
|
||||
@@ -94,7 +93,8 @@ type RefundResultMsg struct {
|
||||
ErrCode string `json:"err_code,omitempty" xml:"err_code,omitempty"`
|
||||
ErrCodeDes string `json:"err_code_des,omitempty" xml:"err_code_des,omitempty"`
|
||||
|
||||
ReqInfo *RefundReqInfo `json:"req_info,omitempty" xml:"req_info,omitempty"`
|
||||
ReqInfoStr string `json:"req_info,omitempty"`
|
||||
ReqInfoObj *RefundReqInfo `json:"req_info_obj,omitempty"`
|
||||
}
|
||||
|
||||
type CallbackMsg struct {
|
||||
@@ -127,13 +127,12 @@ func Err2CallbackResponse(err error, data string) *CallbackResponse {
|
||||
func (a *API) decodeReqInfo(msg string) (decryptedMsg string, err error) {
|
||||
binMsg, err := base64.StdEncoding.DecodeString(msg)
|
||||
if err == nil {
|
||||
aesKey := []byte(fmt.Sprintf("%x", md5.Sum([]byte(a.appKey))))
|
||||
binResult, err2 := utils.AESCBCDecpryt(binMsg, aesKey, aesKey[:16])
|
||||
// aesKey := []byte(fmt.Sprintf("%x", md5.Sum([]byte(a.appKey))))
|
||||
// binResult, err2 := utils.AESCBCDecpryt(binMsg, aesKey, aesKey[:16])
|
||||
gocrypto.SetAesKey(strings.ToLower(gocrypto.Md5(a.appKey)))
|
||||
binResult, err2 := gocrypto.AesECBDecrypt(binMsg)
|
||||
if err = err2; err == nil {
|
||||
var msgLen int32
|
||||
if err = binary.Read(bytes.NewBuffer(binResult[16:]), binary.BigEndian, &msgLen); err == nil {
|
||||
decryptedMsg = string(binResult[16+4 : 16+4+msgLen])
|
||||
}
|
||||
decryptedMsg = string(binResult)
|
||||
}
|
||||
}
|
||||
return decryptedMsg, err
|
||||
@@ -144,15 +143,15 @@ func (a *API) GetCallbackMsg(request *http.Request) (msg *CallbackMsg, callbackR
|
||||
if err != nil {
|
||||
return nil, Err2CallbackResponse(err, "")
|
||||
}
|
||||
mapData, _, err := a.checkResultAsMap(string(data))
|
||||
return a.getCallbackMsg(string(data))
|
||||
}
|
||||
|
||||
func (a *API) getCallbackMsg(msgBody string) (msg *CallbackMsg, callbackResponse *CallbackResponse) {
|
||||
mapData, _, err := a.checkResultAsMap(msgBody)
|
||||
if err != nil {
|
||||
return nil, Err2CallbackResponse(err, "")
|
||||
}
|
||||
sign := utils.Interface2String(mapData[sigKey])
|
||||
desiredSign := a.signParam(mapData)
|
||||
if desiredSign != sign {
|
||||
return nil, Err2CallbackResponse(fmt.Errorf("desiredSign:%s <> sign:%s", desiredSign, sign), "")
|
||||
}
|
||||
|
||||
msg = &CallbackMsg{
|
||||
MapData: mapData,
|
||||
}
|
||||
@@ -163,13 +162,15 @@ func (a *API) GetCallbackMsg(request *http.Request) (msg *CallbackMsg, callbackR
|
||||
ReturnMsg: utils.Interface2String(mapData["return_msg"]),
|
||||
}
|
||||
} else {
|
||||
if transactionID := utils.Interface2String(mapData["transaction_id"]); transactionID != "" {
|
||||
msg.MsgType = MsgTypePay
|
||||
var payResult *PayResultMsg
|
||||
if err = utils.Map2StructByJson(mapData, &payResult, false); err == nil {
|
||||
msg.Data = payResult
|
||||
reqInfo := utils.Interface2String(mapData["req_info"])
|
||||
if reqInfo == "" {
|
||||
sign := utils.Interface2String(mapData[sigKey])
|
||||
desiredSign := a.signParam(mapData)
|
||||
if desiredSign != sign {
|
||||
return nil, Err2CallbackResponse(fmt.Errorf("desiredSign:%s <> sign:%s", desiredSign, sign), "")
|
||||
}
|
||||
} else if reqInfo := utils.Interface2String(mapData["req_info"]); reqInfo != "" {
|
||||
}
|
||||
if reqInfo != "" {
|
||||
msg.MsgType = MsgTypeRefund
|
||||
var refundResult *RefundResultMsg
|
||||
if err = utils.Map2StructByJson(mapData, &refundResult, false); err == nil {
|
||||
@@ -177,12 +178,18 @@ func (a *API) GetCallbackMsg(request *http.Request) (msg *CallbackMsg, callbackR
|
||||
mv, err2 := mxj.NewMapXml([]byte(reqInfo))
|
||||
if err = err2; err == nil {
|
||||
reqInfoMap := mv["root"].(map[string]interface{})
|
||||
if err = utils.Map2StructByJson(reqInfoMap, &refundResult.ReqInfo, false); err == nil {
|
||||
if err = utils.Map2StructByJson(reqInfoMap, &refundResult.ReqInfoObj, false); err == nil {
|
||||
msg.Data = refundResult
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if transactionID := utils.Interface2String(mapData["transaction_id"]); transactionID != "" {
|
||||
msg.MsgType = MsgTypePay
|
||||
var payResult *PayResultMsg
|
||||
if err = utils.Map2StructByJson(mapData, &payResult, false); err == nil {
|
||||
msg.Data = payResult
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user