-
Notifications
You must be signed in to change notification settings - Fork 5
/
bill_downloadurl_query.go
297 lines (258 loc) · 8.48 KB
/
bill_downloadurl_query.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Package alipay https://docs.open.alipay.com/api_15/alipay.data.dataservice.bill.downloadurl.query
package alipay
import (
"archive/zip"
"bytes"
"encoding/csv"
"encoding/json"
"io"
"io/ioutil"
"strings"
)
const (
// BillTypeTrade 指商户基于支付宝交易收单的业务账
BillTypeTrade = "trade"
// BillTypeSigncustomer 指基于商户支付宝余额收入及支出等资金变动的帐务账单
BillTypeSigncustomer = "signcustomer"
)
// CsvBusinessType ...
const (
CsvBusinessTypeTrade = "交易"
CsvBusinessTypeRefund = "退款"
)
// BillTradeEntry ...
type BillTradeEntry struct {
TradeNo string // 支付宝交易号
OutTradeNo string // 商户订单号
BusinessType string // 业务类型
Subject string // 商品名称
TimeStart string // 创建时间
TimeEnd string // 完成时间
ShopNo string // 门店编号
ShopName string // 门店名称
Operator string // 操作员
TerminalNo string // 终端号
BuyerEmail string // 对方账户
TotalAmount string // 订单金额(元)
ReceiptAmount string // 商家实收(元)
Coupon string // 支付宝红包(元)
Jf string // 集分宝(元)
AlipayOff string // 支付宝优惠(元)
SellerOff string // 商家优惠(元)
CouponChargeOff string // 券核销金额(元)
CouponName string // 券名称
SellerCouponConsume string // 商家红包消费金额(元)
HandlingCharge string // 卡消费金额(元)
OutRequestNo string // 退款批次号/请求号
Service string // 服务费(元)
Fr string // 分润(元)
Body string // 备注
}
// BillSigncustomerEntry ...
type BillSigncustomerEntry struct {
FundFlowID string // 账务流水号
TransactionID string // 业务流水号
BusinessID string // 商户订单号
ProductName string // 商品名称
TimeStart string // 发生时间
OtherAccount string // 对方账号
IncomeAmount string // 收入金额(+元)
ExpensesAmount string // 支出金额(-元)
Balance string // 账户余额(元)
TradingChannel string // 交易渠道
BusinessType string // 业务类型
Remark string // 备注
}
// BillDownloadURLQueryParam ...
type BillDownloadURLQueryParam struct {
BillType string `json:"bill_type,omitempty"` // 账单类型,商户通过接口或商户经开放平台授权后其所属服务商通过接口可以获取以下账单类型:trade、signcustomer;trade指商户基于支付宝交易收单的业务账单;signcustomer是指基于商户支付宝余额收入及支出等资金变动的帐务账单;
BillDate string `json:"bill_date,omitempty"` // 账单时间:日账单格式为yyyy-MM-dd,月账单格式为yyyy-MM。
}
// BillDownloadURLQueryResponse ...
type BillDownloadURLQueryResponse struct {
ResponseError
BillDownloadURL string `json:"bill_download_url"` // 账单下载地址链接,获取连接后30秒后未下载,链接地址失效。
}
// IsBillNotExist ...
func (resp *BillDownloadURLQueryResponse) IsBillNotExist() bool {
return resp.SubCode == "isp.bill_not_exist"
}
// BillDownloadurlQuery ...
func (alipay *Alipay) BillDownloadurlQuery(param *BillDownloadURLQueryParam) (int, *BillDownloadURLQueryResponse, error) {
statusCode, body, err := alipay.OnRequest(
param,
MethodAlipayDataDataserviceBillDownloadurlQuery,
)
if err != nil {
return 0, nil, err
}
billDownloadURLQueryResponse := new(BillDownloadURLQueryResponse)
if err := json.Unmarshal(body, billDownloadURLQueryResponse); err != nil {
return 0, nil, err
}
return statusCode, billDownloadURLQueryResponse, nil
}
// DownloadBill ...
func (alipay *Alipay) DownloadBill(billURL string) ([]byte, error) {
resp, err := alipay.HTTPClient().Get(billURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
// BillTradeList ...
func (alipay *Alipay) BillTradeList(bill []byte) ([]*BillTradeEntry, error) {
billTradeEntryList := make([]*BillTradeEntry, 0)
byteReader := bytes.NewReader(bill)
zipReader, err := zip.NewReader(byteReader, int64(byteReader.Len()))
if err != nil {
return nil, err
}
for _, file := range zipReader.File {
fileNameBytesInUtf8, _ := GbkToUtf8([]byte(file.Name))
fileNameInUtf8 := string(fileNameBytesInUtf8)
if strings.HasSuffix(fileNameInUtf8, "业务明细.csv") {
var buf bytes.Buffer
fileReaderCloser, err := file.Open()
if err != nil {
return nil, err
}
defer fileReaderCloser.Close()
if _, err := io.Copy(&buf, fileReaderCloser); err != nil {
return nil, err
}
contentBytesInUtd8, _ := GbkToUtf8(buf.Bytes())
content := string(contentBytesInUtd8)
lines := strings.Split(content, "\n")
var (
validContent string
)
for _, line := range lines {
if strings.HasPrefix(line, "#") {
continue
}
if len(validContent) > 0 {
validContent += "\n"
}
validContent += line
}
csvReader := csv.NewReader(strings.NewReader(validContent))
records, err := csvReader.ReadAll()
for _, record := range records {
for idx, field := range record {
record[idx] = strings.TrimSuffix(field, "\t")
}
}
if len(records) > 0 {
for _, record := range records[1:] {
if len(record) != 25 {
continue
}
entry := new(BillTradeEntry)
billTradeEntryList = append(billTradeEntryList, entry)
entry.TradeNo = record[0]
entry.OutTradeNo = record[1]
entry.BusinessType = record[2]
entry.Subject = record[3]
entry.TimeStart = record[4]
entry.TimeEnd = record[5]
entry.ShopNo = record[6]
entry.ShopName = record[7]
entry.Operator = record[8]
entry.TerminalNo = record[9]
entry.BuyerEmail = record[10]
entry.TotalAmount = record[11]
entry.ReceiptAmount = record[12]
entry.Coupon = record[13]
entry.Jf = record[14]
entry.AlipayOff = record[15]
entry.SellerOff = record[16]
entry.CouponChargeOff = record[17]
entry.CouponName = record[18]
entry.SellerCouponConsume = record[19]
entry.HandlingCharge = record[20]
entry.OutRequestNo = record[21]
entry.Service = record[22]
entry.Fr = record[23]
entry.Body = record[24]
}
}
break
}
}
return billTradeEntryList, nil
}
// BillSigncustomerList ...
func (alipay *Alipay) BillSigncustomerList(bill []byte) ([]*BillSigncustomerEntry, error) {
billSigncustomerEntryList := make([]*BillSigncustomerEntry, 0)
byteReader := bytes.NewReader(bill)
zipReader, err := zip.NewReader(byteReader, int64(byteReader.Len()))
if err != nil {
return nil, err
}
for _, file := range zipReader.File {
fileNameBytesInUtf8, _ := GbkToUtf8([]byte(file.Name))
fileNameInUtf8 := string(fileNameBytesInUtf8)
if strings.HasSuffix(fileNameInUtf8, "账务明细.csv") {
var buf bytes.Buffer
fileReaderCloser, err := file.Open()
if err != nil {
return nil, err
}
defer fileReaderCloser.Close()
if _, err := io.Copy(&buf, fileReaderCloser); err != nil {
return nil, err
}
contentBytesInUtd8, _ := GbkToUtf8(buf.Bytes())
content := string(contentBytesInUtd8)
lines := strings.Split(content, "\n")
var (
validContent string
)
for _, line := range lines {
if strings.HasPrefix(line, "#") {
continue
}
if len(validContent) > 0 {
validContent += "\n"
}
validContent += line
}
csvReader := csv.NewReader(strings.NewReader(validContent))
records, err := csvReader.ReadAll()
for _, record := range records {
for idx, field := range record {
record[idx] = strings.TrimSuffix(field, "\t")
}
}
if len(records) > 0 {
for _, record := range records[1:] {
if len(record) != 12 {
continue
}
entry := new(BillSigncustomerEntry)
billSigncustomerEntryList = append(billSigncustomerEntryList, entry)
entry.FundFlowID = record[0]
entry.TransactionID = record[1]
entry.BusinessID = record[2]
entry.ProductName = record[3]
entry.TimeStart = record[4]
entry.OtherAccount = record[5]
entry.IncomeAmount = record[6]
entry.ExpensesAmount = record[7]
entry.Balance = record[8]
entry.TradingChannel = record[9]
entry.BusinessType = record[10]
entry.Remark = record[11]
}
}
break
}
}
return billSigncustomerEntryList, nil
}