forked from skOak/alipay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customs.go
146 lines (124 loc) · 3.67 KB
/
customs.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package alipay
import (
"encoding/xml"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strings"
)
// https://docs.open.alipay.com/155/104778/
type DeclareRequest struct {
// 必选
OutRequestNo string `xml:"out_request_no"`
TradeNo string `xml:"trade_no"`
MerchantCustomsCode string `xml:"merchant_customs_code"`
MerchantCustomsName string `xml:"merchant_customs_name"`
Amount string `xml:"amount"`
CustomsPlace string `xml:"customs_place"`
// 可选
IsSplit string `xml:"is_split"`
SubOutBizNo string `xml:"sub_out_biz_no"`
BuyerName string `xml:"buyer_name"`
BuyerIdNo string `xml:"buyer_id_no"`
}
func (this DeclareRequest) APIName() string {
return "alipay.acquire.customs"
}
func (this DeclareRequest) Params() map[string]string {
var m = make(map[string]string)
//m["partner"] = this.Partner
m["_input_charset"] = "UTF-8"
m["out_request_no"] = this.OutRequestNo
m["trade_no"] = this.TradeNo
m["merchant_customs_code"] = this.MerchantCustomsCode
m["merchant_customs_name"] = this.MerchantCustomsName
m["amount"] = this.Amount
m["customs_place"] = this.CustomsPlace
m["service"] = this.APIName()
// 可选
if this.IsSplit != "" {
m["is_split"] = this.IsSplit
m["sub_out_biz_no"] = this.SubOutBizNo
m["buyer_name"] = this.BuyerName
m["buyer_id_no"] = this.BuyerIdNo
}
return m
}
type DeclareResponse struct {
Body string `xml:"-"` // 方便记日志用
IsSuccess string `xml:"is_success"`
SignType string `xml:"sign_type"`
Sign string `xml:"sign"`
Error string `xml:"error"`
ResultCode string `xml:"result_code"`
TradeNo string `xml:"trade_no"`
AlipayDeclareNo string `xml:"alipay_declare_no"`
DetailErrorCode string `xml:"detail_error_code"`
DetailErrorDes string `xml:"detail_error_des"`
IdentityCheck string `xml:"identity_check"`
// 订购人身份信息和支付人身份信息的验证结果
// T表示商户传入的订购人姓名和身份证号和支付人的姓名和身份证号一致。
// F代表商户传入的订购人姓名和身份证号和支付人的姓名和身份证号不一致。
// 对于同一笔报关单支付宝只会校验一次,如果多次重推不会返回此参数。
}
func (this *DeclareResponse) SetOriginBody(body string) {
this.Body = body
}
func (this *AliPay) CustomsDeclare(param DeclareRequest) (result *DeclareResponse, err error) {
// 老接口,所以单独写一套
var p = url.Values{}
p.Add("partner", this.partnerId)
p.Add("charset", K_CHARSET)
p.Add("sign_type", this.SignType)
var ps = param.Params()
if ps != nil {
for key, value := range ps {
p.Add(key, value)
}
}
var keys = make([]string, 0, 0)
for key, _ := range p {
keys = append(keys, key)
}
sort.Strings(keys)
var sign string
if this.SignType == K_SIGN_TYPE_RSA {
sign, err = signRSA(keys, p, this.privateKey)
} else {
sign, err = signRSA2(keys, p, this.privateKey)
}
if err != nil {
return nil, err
}
p.Add("sign", sign)
buf := strings.NewReader(p.Encode())
req, err := http.NewRequest(http.MethodGet, this.apiDomain, buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
resp, err := this.client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
result = &DeclareResponse{}
err = xml.Unmarshal(data, &result)
if err != nil {
return nil, err
}
result.SetOriginBody(string(data))
if len(this.AliPayPublicKey) > 0 {
if ok, err := verifyResponseData(data, result.SignType, result.Sign, this.AliPayPublicKey); ok == false {
return nil, err
}
}
return
}