mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 12:52:27 +08:00
77
pay/notify/refund.go
Normal file
77
pay/notify/refund.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
|
||||
"github.com/silenceper/wechat/v2/util"
|
||||
)
|
||||
|
||||
// reference: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_16&index=10
|
||||
|
||||
// RefundedResult 退款回调
|
||||
type RefundedResult struct {
|
||||
ReturnCode *string `xml:"return_code"`
|
||||
ReturnMsg *string `xml:"return_msg"`
|
||||
|
||||
AppID *string `xml:"appid"`
|
||||
MchID *string `xml:"mch_id"`
|
||||
NonceStr *string `xml:"nonce_str"`
|
||||
ReqInfo *string `xml:"req_info"`
|
||||
}
|
||||
|
||||
// RefundedReqInfo 退款结果(明文)
|
||||
type RefundedReqInfo struct {
|
||||
TransactionID *string `xml:"transaction_id"`
|
||||
OutTradeNO *string `xml:"out_trade_no"`
|
||||
RefundID *string `xml:"refund_id"`
|
||||
OutRefundNO *string `xml:"out_refund_no"`
|
||||
TotalFee *int `xml:"total_fee"`
|
||||
SettlementTotalFee *int `xml:"settlement_total_fee"`
|
||||
RefundFee *int `xml:"refund_fee"`
|
||||
SettlementRefundFee *int `xml:"settlement_refund_fee"`
|
||||
RefundStatus *string `xml:"refund_status"`
|
||||
SuccessTime *string `xml:"success_time"`
|
||||
RefundRecvAccount *string `xml:"refund_recv_account"`
|
||||
RefundAccount *string `xml:"refund_account"`
|
||||
RefundRequestSource *string `xml:"refund_request_source"`
|
||||
}
|
||||
|
||||
// RefundedResp 消息通知返回
|
||||
type RefundedResp struct {
|
||||
ReturnCode string `xml:"return_code"`
|
||||
ReturnMsg string `xml:"return_msg"`
|
||||
}
|
||||
|
||||
// DecryptReqInfo 对退款结果进行解密
|
||||
func (notify *Notify) DecryptReqInfo(result *RefundedResult) (*RefundedReqInfo, error) {
|
||||
var err error
|
||||
if result == nil || result.ReqInfo == nil {
|
||||
return nil, errors.New("empty refunded_result or req_info")
|
||||
}
|
||||
|
||||
base64Decode, err := base64.StdEncoding.DecodeString(*result.ReqInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hash := md5.New()
|
||||
if _, err = hash.Write([]byte(notify.Key)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
md5APIKey := hex.EncodeToString(hash.Sum(nil))
|
||||
|
||||
data, err := util.AesECBDecrypt(base64Decode, []byte(md5APIKey))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := &RefundedReqInfo{}
|
||||
if err = xml.Unmarshal(data, res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
26
pay/notify/refund_test.go
Normal file
26
pay/notify/refund_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package notify
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"testing"
|
||||
|
||||
"github.com/silenceper/wechat/v2/pay/config"
|
||||
)
|
||||
|
||||
func TestNotify_DecryptReqInfo(t *testing.T) {
|
||||
// data_source: https://studygolang.com/articles/11811
|
||||
notify := &Notify{Config: &config.Config{Key: "ziR0QKsTUfMOuochC9RfCdmfHECorQAP"}}
|
||||
info := "YYwp8C48th0wnQzTqeI+41pflB26v+smFj9z6h9RPBgxTyZyxc+4YNEz7QEgZNWj/6rIb2MfyWMZmCc41CfjKSssoSZPXxOhUayb6KvNSZ1p6frOX1PDWzhyruXK7ouNND+gDsG4yZ0XXzsL4/pYNwLLba/71QrnkJ/BHcByk4EXnglju5DLup9pJQSnTxjomI9Rxu57m9jg5lLQFxMWXyeASZJNvof0ulnHlWJswS4OxKOkmW7VEyKyLGV6npoOm03Qsx2wkRxLsSa9gPpg4hdaReeUqh1FMbm7aWjyrVYT/MEZWg98p4GomEIYvz34XfDncTezX4bf/ZiSLXt79aE1/YTZrYfymXeCrGjlbe0rg/T2ezJHAC870u2vsVbY1/KcE2A443N+DEnAziXlBQ1AeWq3Rqk/O6/TMM0lomzgctAOiAMg+bh5+Gu1ubA9O3E+vehULydD5qx2o6i3+qA9ORbH415NyRrQdeFq5vmCiRikp5xYptWiGZA0tkoaLKMPQ4ndE5gWHqiBbGPfULZWokI+QjjhhBmwgbd6J0VqpRorwOuzC/BHdkP72DCdNcm7IDUpggnzBIy0+seWIkcHEryKjge3YDHpJeQCqrAH0CgxXHDt1xtbQbST1VqFyuhPhUjDXMXrknrGPN/oE1t0rLRq+78cI+k8xe5E6seeUXQsEe8r3358mpcDYSmXWSXVZxK6er9EF98APqHwcndyEJD2YyCh/mMVhERuX+7kjlRXSiNUWa/Cv/XAKFQuvUYA5ea2eYWtPRHa4DpyuF1SNsaqVKfgqKXZrJHfAgslVpSVqUpX4zkKszHF4kwMZO3M7J1P94Mxa7Tm9mTOJePOoHPXeEB+m9rX6pSfoi3mJDQ5inJ+Vc4gOkg/Wd/lqiy6TTyP/dHDN6/v+AuJx5AXBo/2NDD3fWhHjkqEKIuARr2ClZt9ZRQO4HkXdZo7CN06sGCHk48Tg8PmxnxKcMZm7Aoquv5yMIM2gWSWIRJhwJ8cUpafIHc+GesDlbF6Zbt+/KXkafJAQq2RklEN+WvZ/zFz113EPgWPjp16TwBoziq96MMekvWKY/vdhjol8VFtGH9F61Oy1Xwf6DJtPw=="
|
||||
res, err := notify.DecryptReqInfo(&RefundedResult{ReqInfo: &info})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
bytes, err := xml.Marshal(res)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
t.Log(string(bytes))
|
||||
}
|
||||
Reference in New Issue
Block a user