-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvivawallet.go
81 lines (68 loc) · 1.45 KB
/
vivawallet.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
package vivawallet
import (
"bytes"
"net/http"
"sync"
"time"
)
type Config struct {
Demo bool
ClientID string
ClientSecret string
MerchantID string
APIKey string
}
type token struct {
value string
expires time.Time
}
type OAuthClient struct {
Config Config
Client *http.Client
lock *sync.RWMutex
tokenValue *token
}
type BasicAuthClient struct {
Config Config
Client *http.Client
}
// defaultHTTPTimeout is the default timeout on the http.Client used by the library.
const defaultTimeout = 60 * time.Second
var (
httpClient *http.Client
)
func init() {
httpClient = &http.Client{
Timeout: defaultTimeout,
}
}
type Client interface {
Get(uri string, v interface{}) error
Post(uri string, reader *bytes.Reader, v interface{}) error
Patch(uri string, reader *bytes.Reader, v interface{}) error
}
// ApiUri returns the uri of the production or the demo api.
func ApiUri(c Config) string {
if isDemo(c) {
return "https://demo-api.vivapayments.com"
}
return "https://api.vivapayments.com"
}
func AppUri(c Config) string {
if isDemo(c) {
return "https://demo.vivapayments.com"
}
return "https://www.vivapayments.com"
}
func isDemo(c Config) bool {
return c.Demo
}
func newRequest(method string, uri string, reader *bytes.Reader) *http.Request {
var req *http.Request
if reader != nil {
req, _ = http.NewRequest(method, uri, reader)
} else {
req, _ = http.NewRequest(method, uri, nil)
}
return req
}