-
Notifications
You must be signed in to change notification settings - Fork 0
/
everypay.go
114 lines (98 loc) · 2.35 KB
/
everypay.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
package everypay
import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"time"
)
var (
client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 3 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 4,
MaxConnsPerHost: 0,
IdleConnTimeout: 90 * time.Second,
ResponseHeaderTimeout: 2 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxResponseHeaderBytes: 2 * 1024,
ForceAttemptHTTP2: false,
},
}
)
type Everypay struct {
endpoint *url.URL
username string
secret string
account string
}
func NewEverypay(username, secret, account string, production bool) *Everypay {
e := &Everypay{
username: username,
secret: secret,
account: account,
}
if production {
// https://pay.every-pay.eu/api/v4/
e.endpoint = &url.URL{
Scheme: "https",
Host: "pay.every-pay.eu",
Path: "/api/v4/",
}
} else {
// https://igw-demo.every-pay.com/api/v4/
e.endpoint = &url.URL{
Scheme: "https",
Host: "igw-demo.every-pay.com",
Path: "/api/v4/",
}
}
return e
}
func (e *Everypay) request(
method string,
reference *url.URL,
requestData interface{},
responseData interface{},
) error {
var request *http.Request
switch method {
case http.MethodGet:
request, _ = http.NewRequest(http.MethodGet, "", nil)
case http.MethodPost:
requestBody := &bytes.Buffer{}
_ = json.NewEncoder(requestBody).Encode(requestData)
request, _ = http.NewRequest(http.MethodPost, "", requestBody)
request.Header.Set("Content-Type", "application/json; charset=utf-8")
default:
return fmt.Errorf("invalid method: %s", method)
}
request.URL = e.endpoint.ResolveReference(reference)
request.SetBasicAuth(e.username, e.secret)
response, err := client.Do(request)
if err != nil {
if response != nil {
response.Body.Close()
}
return fmt.Errorf("send: %w", err)
}
defer response.Body.Close()
switch response.StatusCode {
case http.StatusOK, http.StatusCreated, http.StatusAccepted:
break
default:
return fmt.Errorf("response: %s", response.Status)
}
if err := json.NewDecoder(response.Body).Decode(responseData); err != nil {
return fmt.Errorf("decode: %w", err)
}
return nil
}