-
Notifications
You must be signed in to change notification settings - Fork 0
/
pix.go
executable file
·95 lines (77 loc) · 2.29 KB
/
pix.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 pixsdk
import (
"context"
"github.com/libercapital/pix-sdk-go/services/webhook"
"github.com/libercapital/pix-sdk-go/bank"
"github.com/libercapital/pix-sdk-go/services"
"github.com/libercapital/pix-sdk-go/services/auth"
"github.com/libercapital/pix-sdk-go/services/pix"
)
type Service interface {
pix.Service
webhook.Service
SetConfig(config Config)
}
type Context struct {
Config Config
Bank bank.Bank
BaseService services.BaseService
AuthService auth.Service
PixService pix.Service
WebhookService webhook.Service
}
type Config struct {
AuthURL string
BaseURL string
ClientId string
ClientSecret string
ClientCrt string
ClientCrtKey string
}
func (c *Context) CreateWebhook(ctx context.Context, key string, webhook webhook.CreateWebhook) error {
return c.WebhookService.CreateWebhook(ctx, key, webhook)
}
func (c *Context) FindWebhook(ctx context.Context, key string) (webhook.Webhook, error) {
return c.WebhookService.FindWebhook(ctx, key)
}
func (c *Context) DeleteWebhook(ctx context.Context, key string) error {
return c.WebhookService.DeleteWebhook(ctx, key)
}
func (c *Context) FindPix(endToEndId string) (pix.Pix, error) {
return c.PixService.FindPix(endToEndId)
}
func (c *Context) ListPix(listPix pix.ListPix) (pix.ListPixResponse, error) {
return c.PixService.ListPix(listPix)
}
func (c *Context) setupConfig(config Config) {
var certificate *bank.Certificate
if config.ClientCrt != "" && config.ClientCrtKey != "" {
c := bank.NewCertificate(config.ClientCrt, config.ClientCrtKey)
certificate = &c
}
var bank = bank.NewBank(
config.BaseURL,
config.AuthURL,
certificate,
bank.NewCredentials(config.ClientId, config.ClientSecret),
)
var baseService = services.NewBaseService(bank)
var authService = auth.NewAuthService(bank, baseService)
baseService.SetAuthorizer(authService)
var pixService = pix.NewPixService(baseService)
var webhookService = webhook.NewWebhookService(baseService)
c.Config = config
c.Bank = bank
c.BaseService = baseService
c.AuthService = authService
c.PixService = pixService
c.WebhookService = webhookService
}
func (c *Context) SetConfig(config Config) {
c.setupConfig(config)
}
func NewService(config Config) Service {
var ctx = &Context{}
ctx.setupConfig(config)
return ctx
}