-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_on.go
121 lines (98 loc) · 2.81 KB
/
add_on.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
package lago
import (
"context"
"net/url"
"strconv"
"time"
"github.com/google/uuid"
)
type addOnParams struct {
AddOn *AddOnInput `json:"add_on"`
}
type AddOnInput struct {
Name string `json:"name,omitempty"`
InvoiceDisplayName string `json:"invoice_display_name,omitempty"`
Code string `json:"code,omitempty"`
Description string `json:"description,omitempty"`
AmountCents int `json:"amount_cents,omitempty"`
AmountCurrency Currency `json:"amount_currency,omitempty"`
TaxCodes []string `json:"tax_codes,omitempty"`
}
type AddOnListInput struct {
PerPage int `json:"per_page,omitempty,string"`
Page int `json:"page,omitempty,string"`
}
func (i *AddOnListInput) query() url.Values {
q := make(url.Values)
if i.PerPage > 0 {
q.Add("per_page", strconv.Itoa(i.PerPage))
}
if i.Page > 0 {
q.Add("page", strconv.Itoa(i.Page))
}
return q
}
type AddOnList struct {
AddOns []*AddOn `json:"add_ons,omitempty"`
Meta Metadata `json:"meta,omitempty"`
}
type addOnResult struct {
AddOn *AddOn `json:"add_on,omitempty"`
}
type AddOn struct {
LagoID uuid.UUID `json:"lago_id,omitempty"`
Name string `json:"name,omitempty"`
InvoiceDisplayName string `json:"invoice_display_name,omitempty"`
Code string `json:"code,omitempty"`
Description string `json:"description,omitempty"`
AmountCents int `json:"amount_cents,omitempty"`
AmountCurrency Currency `json:"amount_currency,omitempty"`
Taxes []*Tax `json:"tax,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
func (c *Client) GetAddOn(ctx context.Context, addOnCode string) (*AddOn, error) {
u := c.url("add_ons/"+addOnCode, nil)
result, err := get[addOnResult](ctx, c, u)
if err != nil {
return nil, err
}
return result.AddOn, nil
}
func (c *Client) ListAddOns(ctx context.Context, addOnListInput *AddOnListInput) (*AddOnList, error) {
u := c.url("add_ons", addOnListInput.query())
return get[AddOnList](ctx, c, u)
}
func (c *Client) CreateAddOn(ctx context.Context, addOnInput *AddOnInput) (*AddOn, error) {
u := c.url("add_ons", nil)
result, err := post[addOnParams, addOnResult](
ctx,
c,
u,
&addOnParams{AddOn: addOnInput},
)
if err != nil {
return nil, err
}
return result.AddOn, nil
}
func (c *Client) UpdateAddOn(ctx context.Context, addOnInput *AddOnInput) (*AddOn, error) {
u := c.url("add_ons/"+addOnInput.Code, nil)
result, err := put[addOnParams, addOnResult](
ctx,
c,
u,
&addOnParams{AddOn: addOnInput},
)
if err != nil {
return nil, err
}
return result.AddOn, nil
}
func (c *Client) DeleteAddOn(ctx context.Context, addOnCode string) (*AddOn, error) {
u := c.url("add_ons/"+addOnCode, nil)
result, err := delete[addOnResult](ctx, c, u)
if err != nil {
return nil, err
}
return result.AddOn, nil
}