Skip to content

Commit 3ea7eb0

Browse files
committedJan 23, 2021
change
1 parent 484f745 commit 3ea7eb0

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
 

‎encrypt.go

+16
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,19 @@ func PKCS7UnPadding(origData []byte) []byte {
9494
unpadding := int(origData[length-1])
9595
return origData[:(length - unpadding)]
9696
}
97+
98+
// DecryptAesEcb aes ecb 解密
99+
func DecryptAesEcb(data, key []byte) ([]byte, error) {
100+
cip, err := aes.NewCipher(key)
101+
if err != nil {
102+
return nil, err
103+
}
104+
decrypted := make([]byte, len(data))
105+
size := cip.BlockSize()
106+
107+
for bs, be := 0, size; bs < len(data); bs, be = bs+size, be+size {
108+
cip.Decrypt(decrypted[bs:be], data[bs:be])
109+
}
110+
decrypted = PKCS7UnPadding(decrypted)
111+
return decrypted, nil
112+
}

‎wxpayv3.go

+53
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"crypto"
66
"crypto/aes"
77
"crypto/cipher"
8+
"crypto/md5"
89
"crypto/rand"
910
"crypto/rsa"
1011
"crypto/sha256"
1112
"crypto/x509"
1213
"encoding/base64"
1314
"encoding/json"
1415
"encoding/pem"
16+
"encoding/xml"
1517
"fmt"
1618
"io/ioutil"
1719
"net/url"
@@ -58,6 +60,24 @@ type StWxPayResp struct {
5860
} `json:"amount"`
5961
}
6062

63+
type StWxRefundCb struct {
64+
XMLName xml.Name `xml:"root"`
65+
Text string `xml:",chardata"`
66+
OutRefundNo string `xml:"out_refund_no"`
67+
OutTradeNo string `xml:"out_trade_no"`
68+
RefundAccount string `xml:"refund_account"`
69+
RefundFee string `xml:"refund_fee"`
70+
RefundID string `xml:"refund_id"`
71+
RefundRecvAccout string `xml:"refund_recv_accout"`
72+
RefundRequestSource string `xml:"refund_request_source"`
73+
RefundStatus string `xml:"refund_status"`
74+
SettlementRefundFee string `xml:"settlement_refund_fee"`
75+
SettlementTotalFee string `xml:"settlement_total_fee"`
76+
SuccessTime string `xml:"success_time"`
77+
TotalFee string `xml:"total_fee"`
78+
TransactionID string `xml:"transaction_id"`
79+
}
80+
6181
// RsaSign 签名
6282
func RsaSign(signContent string, privateKey *rsa.PrivateKey, hash crypto.Hash) (string, error) {
6383
shaNew := hash.New()
@@ -322,3 +342,36 @@ func WxPayV3DecodePayResp(v3Key string, body []byte, mchid, appid string) (*StWx
322342
}
323343
return &finalResp, nil
324344
}
345+
346+
// WxPayCheckRefundCb 验证回调
347+
func WxPayCheckRefundCb(mchKey string, body []byte) (*StWxRefundCb, error) {
348+
mchKeyMd5 := md5.Sum([]byte(mchKey))
349+
bodyMap, err := XMLWalk(body)
350+
if err != nil {
351+
// 返回数据
352+
return nil, err
353+
}
354+
reqInfo, ok := bodyMap["req_info"]
355+
if !ok {
356+
return nil, fmt.Errorf("no key req_info %s", body)
357+
}
358+
reqInfoStr, ok := reqInfo.(string)
359+
if !ok {
360+
return nil, fmt.Errorf("error format req_info: %s", body)
361+
}
362+
reqInfoBytes, err := base64.StdEncoding.DecodeString(reqInfoStr)
363+
if err != nil {
364+
return nil, err
365+
}
366+
reqInfoFull, err := DecryptAesEcb(reqInfoBytes, mchKeyMd5[:])
367+
if err != nil {
368+
return nil, err
369+
}
370+
Log.Debugf("reqInfoFull: %s", reqInfoFull)
371+
var bodyXML StWxRefundCb
372+
err = xml.Unmarshal(reqInfoFull, &bodyXML)
373+
if err != nil {
374+
return nil, err
375+
}
376+
return &bodyXML, nil
377+
}

0 commit comments

Comments
 (0)
Please sign in to comment.