-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.go
234 lines (193 loc) · 5.58 KB
/
webhook.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package intersdk
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"strings"
"time"
types2 "github.com/enxservices/sdk-inter/internal/types"
)
type Notification struct {
WebHookUrl string `json:"webhookUrl"`
Payload []WebhookPayload `json:"payload"`
Tries int `json:"numeroTentativa"`
NotificationTime time.Time `json:"dataHoraDisparo"`
Send bool `json:"sucesso"`
HttpStatusCode int `json:"httpStatus"`
ErrorMessage string `json:"mensagemErro"`
}
type CreateWebhook struct {
WebhookUrl string `json:"webhookUrl"`
}
type WebhookPayload struct {
RequestCode string `json:"codigoSolicitacao"`
YourNumber string `json:"seuNumero"`
Status ChargeStatus `json:"situacao"`
StatusDateTime string `json:"dataHoraSituacao"`
TotalReceivedAmount *string `json:"valorTotalRecebido,omitempty"`
ReceivingSource *string `json:"origemRecebimento,omitempty"`
OurNumber string `json:"nossoNumero"`
Barcode string `json:"codigoBarras"`
DigitableLine string `json:"linhaDigitavel"`
TxID string `json:"txid"`
PixCopyPaste string `json:"pixCopiaECola"`
}
type Webhook struct {
WebhookUrl string `json:"webhookUrl"`
CreatedAt time.Time `json:"criacao"`
UpdatedAt time.Time `json:"atualizacao"`
}
type WebhookError struct {
Title string `json:"title"`
StatusCode int `json:"status"`
Detail string `json:"detail"`
Violations []Violations `json:"violacoes"`
}
func (e WebhookError) String() string {
var violationsStr strings.Builder
for _, v := range e.Violations {
violationsStr.WriteString(fmt.Sprintf("Reason: %s, Property: %s, Value: %s\n", v.Reason, v.Property, v.Value))
}
return fmt.Sprintf(
"Title: %s\nStatusCode: %d\nDetail: %s\nViolations:\n%s",
e.Title, e.StatusCode, e.Detail, violationsStr.String(),
)
}
type Violations struct {
Reason string `json:"razao"`
Property string `json:"propriedade"`
Value string `json:"valor"`
}
/**
* SizePage [10...50] Default 20
* SolicitaonCode <uuid>
*/
type Queries struct {
StartDate time.Time
EndDate time.Time
Page *int
SizePage *int
SolicitacionCode *string
}
type CallBackSend struct {
TotalItems int `json:"totalElementos"`
TotalPages int `json:"totalPaginas"`
FirstPage bool `json:"primeiraPagina"`
LastPage bool `json:"ultimaPagina"`
Data []Notification `json:"data"`
}
func (i inter) CreateWebhook(webhookUrl string) error {
token, err := i.Oauth.GetAccessToken("boleto-cobranca.write")
if err != nil {
return err
}
payload := CreateWebhook{
WebhookUrl: webhookUrl,
}
jsonData, err := json.Marshal(payload)
if err != nil {
return err
}
res, err := sendRequest(i.client, "PUT", types2.CobWebHookUrl, token, jsonData)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 204 {
return errors.New("unable to create webhook")
}
return nil
}
func (i inter) GetWebhook() (*Webhook, error) {
token, err := i.Oauth.GetAccessToken("boleto-cobranca.read")
if err != nil {
return nil, err
}
res, err := sendRequest(i.client, "GET", types2.CobWebHookUrl, token, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, errors.New("no webhooks exists")
}
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
var webhook Webhook
if err := json.Unmarshal(resBody, &webhook); err != nil {
return nil, err
}
return &webhook, nil
}
func (i inter) DeleteWebhook() (*WebhookError, error) {
token, err := i.Oauth.GetAccessToken("boleto-cobranca.write")
if err != nil {
return nil, err
}
res, err := sendRequest(i.client, "GET", types2.CobWebHookUrl, token, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 204 {
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
var WebhookError WebhookError
if err := json.Unmarshal(resBody, &WebhookError); err != nil {
return nil, err
}
return &WebhookError, errors.New("could not delete webhook")
}
return nil, nil
}
func (i inter) GetAllCallbackSend(queries Queries) (*CallBackSend, error) {
token, err := i.Oauth.GetAccessToken("boleto-cobranca.read")
if err != nil {
return nil, err
}
baseUrl, err := url.Parse(types2.CobWebHookUrlCallbacks)
if err != nil {
return nil, err
}
params := url.Values{}
layout := "2006-01-02T15:04:05.000Z07:00"
params.Add("start_date", queries.StartDate.Format(layout))
params.Add("end_date", queries.EndDate.Format(layout))
if queries.Page != nil {
params.Add("page", fmt.Sprintf("%d", *queries.Page))
}
if queries.SizePage != nil {
params.Add("size_page", fmt.Sprintf("%d", *queries.SizePage))
}
if queries.SolicitacionCode != nil {
params.Add("solicitacion_code", *queries.SolicitacionCode)
}
baseUrl.RawQuery = params.Encode()
res, err := sendRequest(i.client, "GET", baseUrl.String(), token, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
var webhookUrlError WebhookError
if err := json.Unmarshal(resBody, &webhookUrlError); err != nil {
return nil, err
}
return nil, errors.New(webhookUrlError.String())
}
var callbackSend CallBackSend
if err := json.Unmarshal(resBody, &callbackSend); err != nil {
return nil, err
}
return &callbackSend, nil
}