-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclient.go
127 lines (107 loc) · 2.92 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
// Package tinkoff allows sending token-signed requests to Tinkoff Acquiring API and parse incoming HTTP notifications
package tinkoff
import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"net/http"
"sort"
)
type Config struct {
httpClient *http.Client
terminalKey string
password string
baseURL string
}
func WithTerminalKey(terminalKey string) func(*Config) {
return func(config *Config) {
config.terminalKey = terminalKey
}
}
func WithPassword(password string) func(*Config) {
return func(config *Config) {
config.password = password
}
}
func WithBaseURL(baseURL string) func(*Config) {
return func(config *Config) {
config.baseURL = baseURL
}
}
func WithHTTPClient(c *http.Client) func(*Config) {
return func(config *Config) {
config.httpClient = c
}
}
// Client is the main entity which executes requests against the Tinkoff Acquiring API endpoint
type Client struct {
Config
}
// NewClient returns new Client instance
func NewClient(terminalKey, password string) *Client {
return NewClientWithOptions(
WithTerminalKey(terminalKey),
WithPassword(password),
)
}
func NewClientWithOptions(cfgOption ...func(*Config)) *Client {
defaultConfig := Config{
httpClient: http.DefaultClient,
baseURL: "https://securepay.tinkoff.ru/v2",
}
cfg := defaultConfig
for _, opt := range cfgOption {
opt(&cfg)
}
return &Client{
Config: cfg,
}
}
// SetBaseURL allows to change default API endpoint
func (c *Client) SetBaseURL(baseURL string) {
c.baseURL = baseURL
}
func (c *Client) decodeResponse(response *http.Response, result interface{}) error {
return json.NewDecoder(response.Body).Decode(result)
}
// Deprecated: use PostRequestWithContext instead
func (c *Client) PostRequest(url string, request RequestInterface) (*http.Response, error) {
return c.PostRequestWithContext(context.Background(), url, request)
}
// PostRequestWithContext will automatically sign the request with token
// Use BaseRequest type to implement any API request
func (c *Client) PostRequestWithContext(ctx context.Context, url string, request RequestInterface) (*http.Response, error) {
c.secureRequest(request)
data, err := json.Marshal(request)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+url, bytes.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
return c.httpClient.Do(req)
}
func (c *Client) secureRequest(request RequestInterface) {
request.SetTerminalKey(c.terminalKey)
v := request.GetValuesForToken()
v["TerminalKey"] = c.terminalKey
v["Password"] = c.password
request.SetToken(generateToken(v))
}
func generateToken(v map[string]string) string {
keys := make([]string, 0)
for key := range v {
keys = append(keys, key)
}
sort.Strings(keys)
var b bytes.Buffer
for _, key := range keys {
b.WriteString(v[key])
}
sum := sha256.Sum256(b.Bytes())
return fmt.Sprintf("%x", sum)
}