-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
221 lines (185 loc) · 6 KB
/
client.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
// Package zooz contains Go client for Zooz API.
//
// Zooz API documentation: https://developers.paymentsos.com/docs/api
//
// Before using this client you need to register and configure Zooz account: https://developers.paymentsos.com/docs/quick-start.html
package zooz
import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
)
// Caller makes HTTP call with given options and decode response into given struct.
// Client implements this interface and pass itself to entity clients. You may create entity clients with own caller for
// test purposes.
type Caller interface {
Call(ctx context.Context, method, path string, headers map[string]string, reqObj interface{}, respObj interface{}) error
}
// HTTPClient is interface fot HTTP client. Built-in net/http.Client implements this interface as well.
type HTTPClient interface {
Do(r *http.Request) (*http.Response, error)
}
// Option is a callback for redefine client parameters.
type Option func(*Client)
// Client contains API parameters and provides set of API entity clients.
type Client struct {
httpClient HTTPClient
appID string
privateKey string
env env
}
type env string
const (
apiVersion = "1.2.0"
apiURL = "https://api.paymentsos.com/"
// EnvTest is a value for test environment header
EnvTest env = "test"
// EnvLive is a value for live environment header
EnvLive env = "live"
headerAPIVersion = "api-version"
headerEnv = "x-payments-os-env"
headerIdempotencyKey = "idempotency_key"
headerAppID = "app_id"
headerPrivateKey = "private_key"
headerClientIPAddress = "x-client-ip-address"
headerClientUserAgent = "x-client-user-agent"
headerRequestID = "X-Zooz-Request-Id"
)
// New creates new client with given options.
func New(options ...Option) *Client {
c := &Client{
httpClient: http.DefaultClient,
env: EnvTest,
}
for _, option := range options {
option(c)
}
return c
}
// OptHTTPClient returns option with given HTTP client.
func OptHTTPClient(httpClient HTTPClient) Option {
return func(c *Client) {
c.httpClient = httpClient
}
}
// OptAppID returns option with given App ID.
func OptAppID(appID string) Option {
return func(c *Client) {
c.appID = appID
}
}
// OptPrivateKey returns option with given private key.
func OptPrivateKey(privateKey string) Option {
return func(c *Client) {
c.privateKey = privateKey
}
}
// OptEnv returns option with given environment value.
func OptEnv(env env) Option {
return func(c *Client) {
c.env = env
}
}
// Call does HTTP request with given params using set HTTP client. Response will be decoded into respObj.
// Error may be returned if something went wrong. If API return error as response, then Call returns error of type zooz.Error.
func (c *Client) Call(ctx context.Context, method, path string, headers map[string]string, reqObj interface{}, respObj interface{}) (callErr error) {
var reqBody io.Reader
if reqObj != nil {
reqBodyBytes, err := json.Marshal(reqObj)
if err != nil {
return errors.Wrap(err, "failed to marshal request body")
}
reqBody = bytes.NewBuffer(reqBodyBytes)
}
req, err := http.NewRequest(method, apiURL+path, reqBody)
if err != nil {
return errors.Wrap(err, "failed to create HTTP request")
}
req = req.WithContext(ctx)
// Set call-specific headers
for key, value := range headers {
req.Header.Set(key, value)
}
// Set common client headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set(headerAPIVersion, apiVersion)
req.Header.Set(headerEnv, string(c.env))
req.Header.Set(headerAppID, c.appID)
req.Header.Set(headerPrivateKey, c.privateKey)
resp, err := c.httpClient.Do(req)
if err != nil {
return errors.Wrap(err, "failed to do request")
}
defer func() {
if err := resp.Body.Close(); err != nil {
callErr = err
}
}()
// Handle 4xx and 5xx statuses
if resp.StatusCode >= http.StatusBadRequest {
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "failed to read response body")
}
var apiError APIError
if err := json.Unmarshal(respBody, &apiError); err != nil {
return errors.Wrapf(err, "failed to unmarshal response error with status %d: %s", resp.StatusCode, string(respBody))
}
return &Error{
StatusCode: resp.StatusCode,
RequestID: resp.Header.Get(headerRequestID),
APIError: apiError,
}
}
// Decode response into a struct if it was given
if respObj != nil {
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.Wrap(err, "failed to read response body")
}
if err := json.Unmarshal(respBody, respObj); err != nil {
return errors.Wrapf(err, "failed to unmarshal response body: %s", string(respBody))
}
}
return nil
}
// Payment creates client for work with corresponding entity.
func (c *Client) Payment() *PaymentClient {
return &PaymentClient{Caller: c}
}
// Customer creates client for work with corresponding entity.
func (c *Client) Customer() *CustomerClient {
return &CustomerClient{Caller: c}
}
// PaymentMethod creates client for work with corresponding entity.
func (c *Client) PaymentMethod() *PaymentMethodClient {
return &PaymentMethodClient{Caller: c}
}
// Authorization creates client for work with corresponding entity.
func (c *Client) Authorization() *AuthorizationClient {
return &AuthorizationClient{Caller: c}
}
// Charge creates client for work with corresponding entity.
func (c *Client) Charge() *ChargeClient {
return &ChargeClient{Caller: c}
}
// Capture creates client for work with corresponding entity.
func (c *Client) Capture() *CaptureClient {
return &CaptureClient{Caller: c}
}
// Void creates client for work with corresponding entity.
func (c *Client) Void() *VoidClient {
return &VoidClient{Caller: c}
}
// Refund creates client for work with corresponding entity.
func (c *Client) Refund() *RefundClient {
return &RefundClient{Caller: c}
}
// Redirection creates client for work with corresponding entity.
func (c *Client) Redirection() *RedirectionClient {
return &RedirectionClient{Caller: c}
}