-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinter.go
95 lines (76 loc) · 2.41 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package inter
import (
"context"
"os"
"time"
"github.com/raniellyferreira/interbank-go/auth"
"github.com/raniellyferreira/interbank-go/backend"
"github.com/raniellyferreira/interbank-go/banking"
"github.com/raniellyferreira/interbank-go/cobranca"
"github.com/raniellyferreira/interbank-go/pix"
)
// Client is a client for the Inter service
type Client struct {
// Backend backend
backend *backend.BackendImplement
// Conta corrente (opcional)
accountNumber string
// Serviços
Pix *pix.Service
Banking *banking.Service
Cobranca *cobranca.Service
}
// NewClientWithCredentials creates a new client with the given credentials
func NewClientWithCredentials(creds *auth.Credentials) *Client {
// Create the backend
backend := backend.NewBackendWithCredentials(creds)
// Set the default URL
backend.SetURL("https://cdpj.partners.bancointer.com.br")
// Set the default timeout
backend.SetTimeout(60 * time.Second)
return &Client{
backend: backend,
Pix: pix.NewService(backend),
Banking: banking.NewService(backend),
Cobranca: cobranca.NewService(backend),
}
}
// NewClient creates a new client with default credentials loaded from the environment variables (see NewDefaultCredentials)
func NewClient() (*Client, error) {
creds, err := auth.NewDefaultCredentials()
if err != nil {
return nil, err
}
// Create the client
client := NewClientWithCredentials(creds)
// Check if we should use the sandbox
if os.Getenv("INTERBANK_USE_SANDBOX") == "true" {
client.UseSandBox()
}
return client, nil
}
// SetTimeout sets the timeout for the client
func (c *Client) SetTimeout(timeout time.Duration) *Client {
c.backend.SetTimeout(timeout)
return c
}
// Token returns the current token or fetches a new one if it's expired (thread-safe)
func (c *Client) Token(ctx context.Context) (*auth.Token, error) {
return c.backend.Token(ctx)
}
// UseSandBox sets the base URL to the sandbox environment (set URL to https://cdpj-sandbox.partners.uatinter.co)
func (c *Client) UseSandBox() *Client {
c.SetURL("https://cdpj-sandbox.partners.uatinter.co")
return c
}
// SetURL sets the base URL for the client
func (c *Client) SetURL(url string) *Client {
c.backend.SetURL(url)
return c
}
// SetAccountNumber sets the account number for the client
func (c *Client) SetAccountNumber(accountNumber string) *Client {
c.accountNumber = accountNumber
c.backend.SetHeader("x-conta-corrente", accountNumber)
return c
}