forked from guilhermebr/go-stone-openbank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment_link.go
89 lines (65 loc) · 2.05 KB
/
payment_link.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
package openbank
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/stone-co/go-stone-openbank/types"
)
type PaymentLinkService struct {
client *Client
}
func (s *PaymentLinkService) Get(accountID, orderID string) (types.PaymentLink, *Response, error) {
accountID = strings.TrimSpace(accountID)
orderID = strings.TrimSpace(orderID)
if accountID == "" {
return types.PaymentLink{}, nil, errors.New("account_id can't be empty")
}
if orderID == "" {
return types.PaymentLink{}, nil, errors.New("order_id can't be empty")
}
path := fmt.Sprintf("/api/v1/payment_links/%s/orders/%s", accountID, orderID)
req, err := s.client.NewAPIRequest(http.MethodGet, path, nil)
if err != nil {
return types.PaymentLink{}, nil, err
}
var paymentLink types.PaymentLink
resp, err := s.client.Do(req, &paymentLink)
if err != nil {
return types.PaymentLink{}, resp, err
}
return paymentLink, resp, nil
}
func (s *PaymentLinkService) Create(input types.PaymentLinkInput) (types.PaymentLink, *Response, error) {
path := "/api/v1/payment_links/orders"
req, err := s.client.NewAPIRequest(http.MethodPost, path, input)
if err != nil {
return types.PaymentLink{}, nil, err
}
var paymentLink types.PaymentLink
resp, err := s.client.Do(req, &paymentLink)
if err != nil {
return types.PaymentLink{}, resp, err
}
return paymentLink, resp, nil
}
func (s *PaymentLinkService) Cancel(orderID string, input types.PaymentLinkCancelInput) (types.PaymentLink, *Response, error) {
orderID = strings.TrimSpace(orderID)
if orderID == "" {
return types.PaymentLink{}, nil, errors.New("account_id can't be empty")
}
if err := input.Validate(); err != nil {
return types.PaymentLink{}, nil, err
}
path := fmt.Sprintf("/api/v1/payment_links/orders/%s/closed", orderID)
req, err := s.client.NewAPIRequest(http.MethodPatch, path, input)
if err != nil {
return types.PaymentLink{}, nil, err
}
var paymentLink types.PaymentLink
resp, err := s.client.Do(req, &paymentLink)
if err != nil {
return types.PaymentLink{}, resp, err
}
return paymentLink, resp, nil
}