forked from go-pay/gopay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopay_test.go
95 lines (85 loc) · 2.48 KB
/
gopay_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package gopay
import (
"encoding/xml"
"testing"
"github.com/go-pay/gopay/pkg/xlog"
)
func TestBodyMap_CheckParamsNull(t *testing.T) {
bm := make(BodyMap)
bm.Set("name", "jerry")
bm.Set("age", 2)
bm.Set("phone", "")
bm.Set("pi", 3.1415926)
err := bm.CheckEmptyError("name", "age", "phone")
if err != nil {
xlog.Errorf("bm.CheckEmptyError():error:%+v", err)
return
}
}
func TestBodyMap_UnmarshalXML(t *testing.T) {
xmlData := `<xml>
<appid><![CDATA[wx2421b1c4370ec43b]]></appid>
<mch_id><![CDATA[10000100]]></mch_id>
<nonce_str><![CDATA[TeqClE3i0mvn3DrK]]></nonce_str>
<out_refund_no_0><![CDATA[1415701182]]></out_refund_no_0>
<out_trade_no><![CDATA[1415757673]]></out_trade_no>
<refund_count>1</refund_count>
<refund_fee_0>1</refund_fee_0>
<refund_id_0><![CDATA[2008450740201411110000174436]]></refund_id_0>
<refund_status_0><![CDATA[PROCESSING]]></refund_status_0>
<result_code><![CDATA[SUCCESS]]></result_code>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
<sign><![CDATA[1F2841558E233C33ABA71A961D27561C]]></sign>
<transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>
</xml>`
mm := make(BodyMap)
err := xml.Unmarshal([]byte(xmlData), &mm)
if err != nil {
xlog.Errorf("xml.Unmarshal(%s),error:%+v", xmlData, err)
return
}
for k, v := range mm {
xlog.Infof("%s:%s\n", k, v)
}
}
type QueryRedRecordResponse struct {
HbType string `xml:"hb_type,omitempty" json:"hb_type,omitempty"`
Hblist *hbList `xml:"hblist,omitempty" json:"hblist,omitempty"`
}
type hbList struct {
HbinfoList []*hbinfo `xml:"hbinfo,omitempty" json:"hbinfo,omitempty"`
}
type hbinfo struct {
Openid string `xml:"openid,omitempty" json:"openid,omitempty"`
Amount string `xml:"amount,omitempty" json:"amount,omitempty"`
RcvTime string `xml:"rcv_time,omitempty" json:"rcv_time,omitempty"`
}
func TestBodyMap_UnmarshalXML2(t *testing.T) {
xmlData := `
<xml>
<hb_type><![CDATA[NORMAL]]></hb_type>
<hblist>
<hbinfo>
<openid><![CDATA[111]]></openid>
<amount>222</amount>
<rcv_time><![CDATA[333]]></rcv_time>
</hbinfo>
<hbinfo>
<openid><![CDATA[444]]></openid>
<amount>555</amount>
<rcv_time><![CDATA[666]]></rcv_time>
</hbinfo>
</hblist>
</xml>`
mm := new(QueryRedRecordResponse)
err := xml.Unmarshal([]byte(xmlData), &mm)
if err != nil {
xlog.Errorf("xml.Unmarshal(%s),error:%+v", xmlData, err)
return
}
xlog.Debugf("%+v", mm)
for _, v := range mm.Hblist.HbinfoList {
xlog.Debugf("%+v", v)
}
}