-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinter.go
78 lines (63 loc) · 1.77 KB
/
inter.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
package intersdk
import (
"errors"
"net/http"
"github.com/enxservices/sdk-inter/internal/oauth"
"github.com/enxservices/sdk-inter/internal/types"
)
var ErrTlsCertificateNil = errors.New("tls certificate not provided")
type Inter interface {
// Charges - Boleto with Pix QR Code
CreateCharge(charge CreateChargeRequest) (string, error)
GetCharge(solicitationCode string) (*ChargeResponse, error)
GetChargeList(params QueryParamChargeList) (*ChargeList, error)
DowloadCharge(solicitationCode string) (string, error)
CancelCharge(solicitationCode string, reason string) error
GetWebhook() (*Webhook, error)
DeleteWebhook() (*WebhookError, error)
CreateWebhook(webhookUrl string) error
}
type inter struct {
ClientID string
ClientSecret string
ContaCorrente string
BaseURL string
DisableTLS bool
client *http.Client
Oauth *oauth.OAuth
}
type Option func(*inter)
func WithSandboxEnv() Option {
return func(i *inter) {
i.BaseURL = types.BaseUrlSandBox
}
}
func WithLocalEnv(url string) Option {
return func(i *inter) {
i.BaseURL = url
i.DisableTLS = true
}
}
// New creates a new Inter instance with the provided key file path, certificate file path, client id and client secret
func New(keyFilePath, certFilePath, clientID, clientSecret string, accountNumber *string, options ...Option) (Inter, error) {
i := &inter{
ClientID: clientID,
ClientSecret: clientSecret,
BaseURL: types.BaseUrlProduction,
}
for _, option := range options {
option(i)
}
var err error
if !i.DisableTLS {
i.client, err = NewClient(certFilePath, keyFilePath, accountNumber)
if err != nil {
return nil, err
}
} else {
i.client = http.DefaultClient
}
o := oauth.NewOAuth(i.client, clientID, clientSecret, i.BaseURL)
i.Oauth = o
return i, nil
}