-
Notifications
You must be signed in to change notification settings - Fork 42
/
refund.go
109 lines (90 loc) · 2.67 KB
/
refund.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package wechatpay
import (
"bytes"
"encoding/xml"
"io/ioutil"
"net/http"
"strings"
)
//退款
func (this *WechatPay) Refund(param OrderRefund) (*OrderRefundResult, error) {
param.AppId = this.AppId
param.MchId = this.MchId
param.NonceStr = randomNonceStr()
var m map[string]interface{}
m = make(map[string]interface{}, 0)
m["appid"] = param.AppId
m["mch_id"] = param.MchId
m["total_fee"] = param.TotalFee
m["out_trade_no"] = param.OutTradeNo
m["nonce_str"] = param.NonceStr
m["refund_fee"] = param.RefundFee
m["out_refund_no"] = param.OutRefundNo
param.Sign = GetSign(m, this.ApiKey)
bytes_req, err := xml.Marshal(param)
if err != nil {
return nil, err
}
str_req := string(bytes_req)
str_req = strings.Replace(str_req, "Refund", "xml", -1)
bytes_req = []byte(str_req)
//发送unified order请求.
req, err := http.NewRequest("POST", REFUND_URL, bytes.NewReader(bytes_req))
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/xml")
req.Header.Set("Content-Type", "application/xml;charset=utf-8")
w_req := http.Client{
Transport: WithCertBytes(this.ApiclientCert, this.ApiclientKey),
}
resp, _err := w_req.Do(req)
if _err != nil {
return nil, err
}
body, _ := ioutil.ReadAll(resp.Body)
var refund_resp OrderRefundResult
_err = xml.Unmarshal(body, &refund_resp)
if _err != nil {
return nil, err
}
return &refund_resp, nil
}
//退款查询
func (this *WechatPay) RefundQuery(refund_status OrderRefundQuery) (*OrderRefundQueryResult, error) {
refund_status.AppId = this.AppId
refund_status.MchId = this.MchId
refund_status.NonceStr = randomNonceStr()
var m map[string]interface{}
m = make(map[string]interface{}, 0)
m["appid"] = refund_status.AppId
m["mch_id"] = refund_status.MchId
m["out_trade_no"] = refund_status.OutTradeNo
m["nonce_str"] = refund_status.NonceStr
refund_status.Sign = GetSign(m, this.ApiKey)
bytes_req, err := xml.Marshal(refund_status)
if err != nil {
return nil, err
}
str_req := string(bytes_req)
str_req = strings.Replace(str_req, "RefundQuery", "xml", -1)
bytes_req = []byte(str_req)
req, err := http.NewRequest("POST", REFUND_QUERY_URL, bytes.NewReader(bytes_req))
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/xml")
req.Header.Set("Content-Type", "application/xml;charset=utf-8")
w_req := http.Client{}
resp, _err := w_req.Do(req)
if _err != nil {
return nil, err
}
var refund_resp OrderRefundQueryResult
body, _ := ioutil.ReadAll(resp.Body)
_err = xml.Unmarshal(body, &refund_resp)
if _err != nil {
return nil, err
}
return &refund_resp, nil
}