-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapi_voucher.go
202 lines (169 loc) · 5.24 KB
/
api_voucher.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
package sdk
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"time"
)
// VoucherVoidResponse :
type VoucherVoidResponse struct {
Item struct {
Key string `json:"key"`
Label string `json:"label"`
RedemptionRuleKey interface{} `json:"redemptionRuleKey"`
VoucherBatchKey string `json:"voucherBatchKey"`
Type string `json:"type"`
Amount int `json:"amount"`
DiscountRate int `json:"discountRate"`
MinimumSpendAmount int `json:"minimumSpendAmount"`
Origin string `json:"origin"`
ImageURL string `json:"imageUrl"`
MemberProfile interface{} `json:"memberProfile"`
RedemptionRule interface{} `json:"redemptionRule"`
AssignedAt time.Time `json:"assignedAt"`
Payload interface{} `json:"payload"`
QrURL string `json:"qrUrl"`
Code string `json:"code"`
IsShipping bool `json:"isShipping"`
Address interface{} `json:"address"`
Expiry struct {
Type string `json:"type"`
Day int `json:"day"`
ExpiredAt time.Time `json:"expiredAt"`
} `json:"expiry"`
UsedAt time.Time `json:"usedAt"`
RedeemedAt time.Time `json:"redeemedAt"`
IsDeviceRedeem bool `json:"isDeviceRedeem"`
Status string `json:"status"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
} `json:"item"`
Code string `json:"code"`
Err *Error `json:"error"`
}
// GetVoucherByCodeResponse :
type GetVoucherByCodeResponse struct {
Item Voucher `json:"item"`
Code string `json:"code"`
Err *Error `json:"error"`
}
// RequestGetVoucherBatchByKey :
type RequestGetVoucherBatchByKey struct {
Key string `json:"key"`
Cursor string `json:"cursor"`
}
// GetVoucherBatchByKeyResponse :
type GetVoucherBatchByKeyResponse struct {
Items []Voucher `json:"items"`
Code string `json:"code"`
Meta struct {
Count int `json:"count"`
Cursor string `json:"cursor"`
} `json:"meta"`
Err *Error `json:"error"`
}
// RequestGetVoucherBatches :
type RequestGetVoucherBatches struct {
Status string `json:"status"`
IsStatic bool `json:"isStatic"`
Cursor string `json:"cursor"`
}
// GetVoucherBatchesResponse :
type GetVoucherBatchesResponse struct {
Items []VoucherBatch `json:"items"`
Code string `json:"code"`
Meta struct {
Count int `json:"count"`
Cursor string `json:"cursor"`
} `json:"meta"`
Err *Error `json:"error"`
}
// VoucherVoid :
func (c Client) VoucherVoid(code string, usedDateTime time.Time) (*VoucherVoidResponse, error) {
if c.err != nil {
return nil, c.err
}
method := pathAPIVoucherVoidURL.method
requestURL := c.prepareAPIURL(pathAPIVoucherVoidURL)
response := new(VoucherVoidResponse)
if err := c.httpAPI(method, fmt.Sprintf("%s/%s/void", requestURL, code), struct {
UsedDateTime time.Time `json:"usedAt"`
}{
UsedDateTime: usedDateTime,
}, response); err != nil {
return nil, err
}
return response, nil
}
// GetVoucherByCode :
func (c Client) GetVoucherByCode(code string) (*GetVoucherByCodeResponse, error) {
if c.err != nil {
return nil, c.err
}
code = strings.Replace(code, "#", "%23", -1)
method := pathAPIGetVoucherByCodeURL.method
requestURL := c.prepareAPIURL(pathAPIGetVoucherByCodeURL)
response := new(GetVoucherByCodeResponse)
if err := c.httpAPI(method, fmt.Sprintf("%s/%s", requestURL, code), nil, response); err != nil {
return nil, err
}
return response, nil
}
// GetVoucherBatches :
func (c Client) GetVoucherBatches(request RequestGetVoucherBatches) (*GetVoucherBatchesResponse, error) {
if c.err != nil {
return nil, c.err
}
method := pathAPIGetVoucherBatchesURL.method
requestURL := c.prepareAPIURL(pathAPIGetVoucherBatchesURL)
rawURL, err := url.Parse(requestURL)
if err != nil {
return nil, err
}
parameters := url.Values{}
parameters.Add("cursor", request.Cursor)
type Filter struct {
Status string `json:"status,omitempty"`
IsStatic bool `json:"isStatic,omitempty"`
}
filter := new(Filter)
if request.Status != "" {
filter.Status = request.Status
}
if request.IsStatic {
filter.IsStatic = true
}
data, _ := json.Marshal(filter)
parameters.Add("filter", string(data))
rawURL.RawQuery = parameters.Encode()
response := new(GetVoucherBatchesResponse)
if err := c.httpAPI(method, rawURL.String(), nil, response); err != nil {
return nil, err
}
if response.Err != nil {
return response, errors.New(response.Err.Message)
}
return response, nil
}
// GetVoucherBatchByKey :
func (c Client) GetVoucherBatchByKey(request RequestGetVoucherBatchByKey) (*GetVoucherBatchByKeyResponse, error) {
if c.err != nil {
return nil, c.err
}
method := pathAPIGetVoucherBatchByKeyURL.method
requestURL := c.prepareAPIURL(pathAPIGetVoucherBatchByKeyURL)
rawURL, err := url.Parse(fmt.Sprintf("%s/%s/vouchers", requestURL, request.Key))
if err != nil {
return nil, err
}
parameters := url.Values{}
parameters.Add("cursor", request.Cursor)
rawURL.RawQuery = parameters.Encode()
response := new(GetVoucherBatchByKeyResponse)
if err := c.httpAPI(method, rawURL.String(), nil, response); err != nil {
return nil, err
}
return response, nil
}