-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
169 lines (133 loc) · 3.66 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
// Copyright 2018 Matthieu CORNUT-CHAUVINC. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package adyen
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/mcornut/go-adyen/constants"
log "github.com/molotovtv/go-logger"
"github.com/pkg/errors"
)
// Client struct
type Client struct {
config *Configuration
HttpClient *http.Client
}
var debug bool
// SetDebug enables additional tracing globally.
// The method is designed for used during testing.
func (c *Client) SetDebug(value bool) {
debug = value
}
func (c *Client) getURL() string {
if c.config.Live {
return constants.LiveURL
}
return constants.SandboxURL
}
// New func
func New(config *Configuration) *Client {
transport := &http.Transport{}
if proxy, err := config.GetHTTPProxy(); err != nil {
log.Error(
errors.Wrap(err, "Adyen - Client - Proxy configuration"),
)
} else {
transport.Proxy = proxy
}
return &Client{
config: config,
HttpClient: &http.Client{
Transport: transport,
},
}
}
// Call is used by Call to generate an http.Request. It handles encoding parameters and attaching the Authorization header.
func (c *Client) call(method string, path string, body interface{}, v interface{}) error {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
var b io.Reader
path = c.getURL() + path
if debug {
log.Debugf("Call %v\n", path)
}
if strings.ToUpper(method) == "GET" {
return errors.New("Method GET not allowed")
} else {
jsonBody, err := json.Marshal(body)
if err != nil {
return errors.Wrap(err, "Marshal body")
}
b = bytes.NewBuffer(jsonBody)
}
req, err := http.NewRequest(method, path, b)
if err != nil {
return errors.Wrap(err, "Cannot create Adyen request")
}
req.SetBasicAuth(c.config.Username, c.config.Password)
req.Header.Add("User-Agent", "Mcornut/v1 GoAdyenSdk/"+Version)
if body != nil {
req.Header.Add("Content-Type", "application/json")
}
return c.do(req, v)
}
// do is used by Call to execute an API request and parse the response. It uses
// the environment's HTTP client to execute the request and unmarshals the response
// into v. It also handles unmarshaling errors returned by the API.
func (c *Client) do(req *http.Request, v interface{}) error {
log.Infof("Requesting %v %q\n", req.Method, req.URL.Path)
start := time.Now()
res, err := c.HttpClient.Do(req)
if debug {
log.Debugf("Completed in %v\n", time.Since(start))
}
if res.StatusCode >= 400 {
if debug {
log.Debugf("StatusCode %v with Status %v\n", res.StatusCode, res.Status)
}
adyenErr := &Error{
Type: ErrorType(res.Status),
HTTPStatusCode: res.StatusCode,
Message: res.Status,
}
switch adyenErr.Type {
case ErrorTypeAPIConnection:
adyenErr.Err = &APIConnectionError{adyenErr: adyenErr}
case ErrorTypeAuthentication:
adyenErr.Err = &AuthenticationError{adyenErr: adyenErr}
case ErrorTypeInvalidRequest:
adyenErr.Err = &InvalidRequestError{adyenErr: adyenErr}
case ErrorTypePermission:
adyenErr.Err = &PermissionError{adyenErr: adyenErr}
case ErrorTypeNotFound:
adyenErr.Err = &NotFoundError{adyenErr: adyenErr}
case ErrorTypeUnprocessable:
adyenErr.Err = &UnprocessableError{adyenErr: adyenErr}
default:
adyenErr.Err = &APIError{adyenErr: adyenErr}
}
return adyenErr
}
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Error(
errors.Wrap(err, "Cannot parse Adyen response"),
)
return err
}
if debug {
log.Debugf("Adyen Response: %v\n", string(resBody))
}
if v != nil {
return json.Unmarshal(resBody, v)
}
return nil
}