-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommerce.go
More file actions
291 lines (246 loc) · 8.32 KB
/
Copy pathcommerce.go
File metadata and controls
291 lines (246 loc) · 8.32 KB
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
package bootpay
import (
"bytes"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"
)
const (
COMMERCE_DEVELOPMENT string = "https://dev-api.bootapi.com/v1"
COMMERCE_STAGE string = "https://stage-api.bootapi.com/v1"
COMMERCE_PRODUCTION string = "https://api.bootapi.com/v1"
COMMERCE_API_VERSION string = "1.0.0"
COMMERCE_SDK_VERSION string = "1.0.0"
)
// CommerceApi is the main struct for Commerce API
type CommerceApi struct {
token string
clientKey string
secretKey string
baseUrl string
role string
client *http.Client
// Modules
User *UserModule
UserGroup *UserGroupModule
Product *ProductModule
Invoice *InvoiceModule
Order *OrderModule
OrderCancel *OrderCancelModule
OrderSubscription *OrderSubscriptionModule
OrderSubscriptionBill *OrderSubscriptionBillModule
OrderSubscriptionAdjustment *OrderSubscriptionAdjustmentModule
OrderSubscriptionRequest *OrderSubscriptionRequestModule
Store *StoreModule
Category *CategoryModule
Coupon *CouponModule
Point *PointModule
Cart *CartModule
}
// CommerceResponse is the common response structure for Commerce API
type CommerceResponse struct {
Success bool `json:"success"`
Data interface{} `json:"data,omitempty"`
ErrorCode int `json:"error_code,omitempty"`
Message string `json:"message,omitempty"`
}
// CommerceListResponse is the common response structure for list APIs
type CommerceListResponse[T any] struct {
Success bool `json:"success"`
Data T `json:"data,omitempty"`
ErrorCode int `json:"error_code,omitempty"`
Message string `json:"message,omitempty"`
}
// CommerceTokenResponse represents the token response
type CommerceTokenResponse struct {
AccessToken string `json:"access_token"`
ExpiredAt string `json:"expired_at,omitempty"`
}
// NewCommerceAPI creates a new Commerce API instance (recommended)
func NewCommerceAPI(clientKey string, secretKey string, client *http.Client, mode string) *CommerceApi {
if client == nil {
client = &http.Client{
Timeout: 60 * time.Second,
Transport: &http.Transport{
TLSNextProto: make(map[string]func(string, *tls.Conn) http.RoundTripper),
},
}
}
baseUrl := COMMERCE_PRODUCTION
if mode == "development" {
baseUrl = COMMERCE_DEVELOPMENT
} else if mode == "stage" {
baseUrl = COMMERCE_STAGE
}
api := &CommerceApi{
clientKey: clientKey,
secretKey: secretKey,
baseUrl: baseUrl,
role: "user",
client: client,
}
// Initialize modules
api.User = &UserModule{api: api}
api.UserGroup = &UserGroupModule{api: api}
api.Product = &ProductModule{api: api}
api.Invoice = &InvoiceModule{api: api}
api.Order = &OrderModule{api: api}
api.OrderCancel = &OrderCancelModule{api: api}
api.OrderSubscription = &OrderSubscriptionModule{
api: api,
RequestIng: &OrderSubscriptionRequestIngModule{api: api},
}
api.OrderSubscriptionBill = &OrderSubscriptionBillModule{api: api}
api.OrderSubscriptionAdjustment = &OrderSubscriptionAdjustmentModule{api: api}
api.OrderSubscriptionRequest = &OrderSubscriptionRequestModule{api: api}
api.Store = &StoreModule{api: api}
api.Category = &CategoryModule{api: api}
api.Coupon = &CouponModule{api: api}
api.Point = &PointModule{api: api}
api.Cart = &CartModule{api: api}
return api
}
// NewCommerceApi creates a new Commerce API instance (deprecated: use NewCommerceAPI instead)
func NewCommerceApi(clientKey string, secretKey string, client *http.Client, mode string) *CommerceApi {
return NewCommerceAPI(clientKey, secretKey, client, mode)
}
// SetRole sets the role for API requests
func (api *CommerceApi) SetRole(role string) *CommerceApi {
api.role = role
return api
}
// AsUser sets role to "user"
func (api *CommerceApi) AsUser() *CommerceApi {
return api.SetRole("user")
}
// AsManager sets role to "manager"
func (api *CommerceApi) AsManager() *CommerceApi {
return api.SetRole("manager")
}
// AsPartner sets role to "partner"
func (api *CommerceApi) AsPartner() *CommerceApi {
return api.SetRole("partner")
}
// AsVendor sets role to "vendor"
func (api *CommerceApi) AsVendor() *CommerceApi {
return api.SetRole("vendor")
}
// AsSupervisor sets role to "supervisor"
func (api *CommerceApi) AsSupervisor() *CommerceApi {
return api.SetRole("supervisor")
}
// GetRole returns the current role
func (api *CommerceApi) GetRole() string {
return api.role
}
// GetToken returns the current token
func (api *CommerceApi) GetToken() string {
return api.token
}
// SetToken sets the access token
func (api *CommerceApi) SetToken(token string) {
api.token = token
}
// getBasicAuthHeader returns Basic Auth header value
func (api *CommerceApi) getBasicAuthHeader() string {
if api.clientKey == "" || api.secretKey == "" {
return ""
}
credentials := fmt.Sprintf("%s:%s", api.clientKey, api.secretKey)
encoded := base64.StdEncoding.EncodeToString([]byte(credentials))
return fmt.Sprintf("Basic %s", encoded)
}
// newRequest creates a new HTTP request with common headers
func (api *CommerceApi) newRequest(method string, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, api.baseUrl+"/"+url, body)
if err != nil {
return nil, errors.New("cannot create Commerce API request: " + err.Error())
}
if basic := api.getBasicAuthHeader(); basic != "" {
req.Header.Set("Authorization", basic)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set("Accept-Charset", "utf-8")
req.Header.Set("BOOTPAY-SDK-VERSION", COMMERCE_SDK_VERSION)
req.Header.Set("BOOTPAY-API-VERSION", COMMERCE_API_VERSION)
req.Header.Set("BOOTPAY-SDK-TYPE", "305")
req.Header.Set("BOOTPAY-ROLE", api.role)
return req, nil
}
// GetAccessToken obtains an access token using client_key and secret_key
func (api *CommerceApi) GetAccessToken() (map[string]interface{}, error) {
data := map[string]string{
"client_key": api.clientKey,
"secret_key": api.secretKey,
}
postBody, _ := json.Marshal(data)
body := bytes.NewBuffer(postBody)
req, err := http.NewRequest(http.MethodPost, api.baseUrl+"/request/token", body)
if err != nil {
return nil, errors.New("commerce: getAccessToken error: " + err.Error())
}
req.Header.Set("Authorization", api.getBasicAuthHeader())
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set("Accept-Charset", "utf-8")
req.Header.Set("BOOTPAY-SDK-VERSION", COMMERCE_SDK_VERSION)
req.Header.Set("BOOTPAY-API-VERSION", COMMERCE_API_VERSION)
req.Header.Set("BOOTPAY-SDK-TYPE", "305")
req.Header.Set("BOOTPAY-ROLE", api.role)
res, err := api.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
result := make(map[string]interface{})
json.NewDecoder(res.Body).Decode(&result)
if accessToken, ok := result["access_token"].(string); ok {
api.token = accessToken
}
return result, nil
}
// doRequest performs an HTTP request and returns the response
func (api *CommerceApi) doRequest(method string, url string, data interface{}) (map[string]interface{}, error) {
var body io.Reader
if data != nil {
postBody, err := json.Marshal(data)
if err != nil {
return nil, err
}
body = bytes.NewBuffer(postBody)
}
req, err := api.newRequest(method, url, body)
if err != nil {
return nil, err
}
res, err := api.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
result := make(map[string]interface{})
json.NewDecoder(res.Body).Decode(&result)
return result, nil
}
// Get performs a GET request
func (api *CommerceApi) Get(url string) (map[string]interface{}, error) {
return api.doRequest(http.MethodGet, url, nil)
}
// Post performs a POST request
func (api *CommerceApi) Post(url string, data interface{}) (map[string]interface{}, error) {
return api.doRequest(http.MethodPost, url, data)
}
// Put performs a PUT request
func (api *CommerceApi) Put(url string, data interface{}) (map[string]interface{}, error) {
return api.doRequest(http.MethodPut, url, data)
}
// Delete performs a DELETE request
func (api *CommerceApi) Delete(url string) (map[string]interface{}, error) {
return api.doRequest(http.MethodDelete, url, nil)
}