-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootpay.go
More file actions
117 lines (104 loc) · 3.39 KB
/
Copy pathbootpay.go
File metadata and controls
117 lines (104 loc) · 3.39 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
package bootpay
import (
"crypto/tls"
"encoding/base64"
"io"
"net/http"
"time"
)
const (
DEVELOPMENT string = "https://dev-api.bootpay.co.kr/v2"
TEST string = "https://test-api.bootpay.co.kr/v2"
STAGE string = "https://stage-api.bootpay.co.kr/v2"
PRODUCTION string = "https://api.bootpay.co.kr/v2"
API_VERSION string = "5.1.0"
SDK_VERSION string = "2.3.0"
)
const defaultHTTPTimeout = 10 * time.Second
type APIResponse = map[string]interface{}
//type APIResponse struct {
// Data map[string]interface{}
// //Status int `json:"status"`
// //Code int `json:"code"`
// //Message string `json:"message"`
// //Data map[string]interface{} `json:"data"`
//}
type RestConfig struct {
ApplicationId string `json:"application_id,omitempty"`
PrivateKey string `json:"private_key,omitempty"`
ClientKey string `json:"client_key,omitempty"`
SecretKey string `json:"secret_key,omitempty"`
}
type Api struct {
token string
applicationId string
privateKey string
clientKey string
secretKey string
baseUrl string
client *http.Client
}
func (api Api) 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, err
}
if api.clientKey != "" && api.secretKey != "" {
credentials := base64.StdEncoding.EncodeToString([]byte(api.clientKey + ":" + api.secretKey))
req.Header.Set("Authorization", "Basic "+credentials)
} else if api.token != "" {
req.Header.Set("Authorization", "Bearer "+api.token)
}
req.Header.Set("BOOTPAY-API-VERSION", API_VERSION)
req.Header.Set("BOOTPAY-SDK-VERSION", SDK_VERSION)
req.Header.Set("BOOTPAY-SDK-TYPE", "305")
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept-Charset", "utf-8")
return req, nil
}
func newAPIBase(client *http.Client, mode string) (string, *http.Client) {
if client == nil {
client = &http.Client{
Timeout: defaultHTTPTimeout,
Transport: &http.Transport{
TLSNextProto: make(map[string]func(string, *tls.Conn) http.RoundTripper),
},
}
}
baseUrl := PRODUCTION
if mode == "development" {
baseUrl = DEVELOPMENT
} else if mode == "test" {
baseUrl = TEST
} else if mode == "stage" {
baseUrl = STAGE
}
return baseUrl, client
}
// NewAPI creates a new PG API instance using legacy application_id/private_key.
// Deprecated credentials remain supported for backward compatibility.
func NewAPI(applicationId string, privateKey string, client *http.Client, mode string) *Api {
baseUrl, httpClient := newAPIBase(client, mode)
return &Api{
applicationId: applicationId,
privateKey: privateKey,
baseUrl: baseUrl,
client: httpClient,
}
}
// NewAPIWithClientKey creates a PG API instance using client_key/secret_key.
// When client_key is present this Basic Auth flow takes precedence over legacy token auth.
func NewAPIWithClientKey(clientKey string, secretKey string, client *http.Client, mode string) *Api {
baseUrl, httpClient := newAPIBase(client, mode)
return &Api{
clientKey: clientKey,
secretKey: secretKey,
baseUrl: baseUrl,
client: httpClient,
}
}
// New creates a new PG API instance (deprecated: use NewAPI instead)
func (api Api) New(applicationId string, privateKey string, client *http.Client, mode string) *Api {
return NewAPI(applicationId, privateKey, client, mode)
}