-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcob.go
49 lines (38 loc) · 951 Bytes
/
cob.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
package bb_pix_go
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func (bb *BB) CriarCob(pix Pix) (string, error) {
token, err := bb.GetAccessToken(ScopeCobWrite)
if err != nil {
return "", err
}
url := fmt.Sprintf("%s/%s/%s?%s=%s", bb.Environment, "/cob", pix.TxID, DevAppKey, bb.DeveloperKey)
// transformando a struct Pix em JSON
data, err := json.Marshal(pix)
if err != nil {
return "", err
}
// criando a requisição PUT para fazer a cobrança
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(data))
if err != nil {
return "", ErrHttpRequestFoundation
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token.AccessToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
defer resp.Body.Close()
return string(body), nil
}