-
Notifications
You must be signed in to change notification settings - Fork 303
/
invoices.go
226 lines (192 loc) · 6.95 KB
/
invoices.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
package godo
import (
"bytes"
"context"
"fmt"
"net/http"
"time"
)
const invoicesBasePath = "v2/customers/my/invoices"
// InvoicesService is an interface for interfacing with the Invoice
// endpoints of the DigitalOcean API
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Billing
type InvoicesService interface {
Get(context.Context, string, *ListOptions) (*Invoice, *Response, error)
GetPDF(context.Context, string) ([]byte, *Response, error)
GetCSV(context.Context, string) ([]byte, *Response, error)
List(context.Context, *ListOptions) (*InvoiceList, *Response, error)
GetSummary(context.Context, string) (*InvoiceSummary, *Response, error)
}
// InvoicesServiceOp handles communication with the Invoice related methods of
// the DigitalOcean API.
type InvoicesServiceOp struct {
client *Client
}
var _ InvoicesService = &InvoicesServiceOp{}
// Invoice represents a DigitalOcean Invoice
type Invoice struct {
InvoiceItems []InvoiceItem `json:"invoice_items"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
// InvoiceItem represents a line-item on a DigitalOcean Invoice
type InvoiceItem struct {
Product string `json:"product"`
ResourceID string `json:"resource_id"`
ResourceUUID string `json:"resource_uuid"`
GroupDescription string `json:"group_description"`
Description string `json:"description"`
Amount string `json:"amount"`
Duration string `json:"duration"`
DurationUnit string `json:"duration_unit"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
ProjectName string `json:"project_name"`
Category string `json:"category"`
}
// InvoiceList contains a paginated list of all of a customer's invoices.
// The InvoicePreview is the month-to-date usage generated by DigitalOcean.
type InvoiceList struct {
Invoices []InvoiceListItem `json:"invoices"`
InvoicePreview InvoiceListItem `json:"invoice_preview"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}
// InvoiceListItem contains a small list of information about a customer's invoice.
// More information can be found in the Invoice or InvoiceSummary
type InvoiceListItem struct {
InvoiceUUID string `json:"invoice_uuid"`
Amount string `json:"amount"`
InvoicePeriod string `json:"invoice_period"`
UpdatedAt time.Time `json:"updated_at"`
}
// InvoiceSummary contains metadata and summarized usage for an invoice generated by DigitalOcean
type InvoiceSummary struct {
InvoiceUUID string `json:"invoice_uuid"`
BillingPeriod string `json:"billing_period"`
Amount string `json:"amount"`
UserName string `json:"user_name"`
UserBillingAddress Address `json:"user_billing_address"`
UserCompany string `json:"user_company"`
UserEmail string `json:"user_email"`
ProductCharges InvoiceSummaryBreakdown `json:"product_charges"`
Overages InvoiceSummaryBreakdown `json:"overages"`
Taxes InvoiceSummaryBreakdown `json:"taxes"`
CreditsAndAdjustments InvoiceSummaryBreakdown `json:"credits_and_adjustments"`
}
// Address represents the billing address of a customer
type Address struct {
AddressLine1 string `json:"address_line1"`
AddressLine2 string `json:"address_line2"`
City string `json:"city"`
Region string `json:"region"`
PostalCode string `json:"postal_code"`
CountryISO2Code string `json:"country_iso2_code"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// InvoiceSummaryBreakdown is a grouped set of InvoiceItems from an invoice
type InvoiceSummaryBreakdown struct {
Name string `json:"name"`
Amount string `json:"amount"`
Items []InvoiceSummaryBreakdownItem `json:"items"`
}
// InvoiceSummaryBreakdownItem further breaks down the InvoiceSummary by product
type InvoiceSummaryBreakdownItem struct {
Name string `json:"name"`
Amount string `json:"amount"`
Count string `json:"count"`
}
func (i Invoice) String() string {
return Stringify(i)
}
// Get detailed invoice items for an Invoice
func (s *InvoicesServiceOp) Get(ctx context.Context, invoiceUUID string, opt *ListOptions) (*Invoice, *Response, error) {
path := fmt.Sprintf("%s/%s", invoicesBasePath, invoiceUUID)
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(Invoice)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root, resp, err
}
// List invoices for a customer
func (s *InvoicesServiceOp) List(ctx context.Context, opt *ListOptions) (*InvoiceList, *Response, error) {
path := invoicesBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(InvoiceList)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root, resp, err
}
// GetSummary returns a summary of metadata and summarized usage for an Invoice
func (s *InvoicesServiceOp) GetSummary(ctx context.Context, invoiceUUID string) (*InvoiceSummary, *Response, error) {
path := fmt.Sprintf("%s/%s/summary", invoicesBasePath, invoiceUUID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(InvoiceSummary)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root, resp, err
}
// GetPDF returns the pdf for an Invoice
func (s *InvoicesServiceOp) GetPDF(ctx context.Context, invoiceUUID string) ([]byte, *Response, error) {
path := fmt.Sprintf("%s/%s/pdf", invoicesBasePath, invoiceUUID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var root bytes.Buffer
resp, err := s.client.Do(ctx, req, &root)
if err != nil {
return nil, resp, err
}
return root.Bytes(), resp, err
}
// GetCSV returns the csv for an Invoice
func (s *InvoicesServiceOp) GetCSV(ctx context.Context, invoiceUUID string) ([]byte, *Response, error) {
path := fmt.Sprintf("%s/%s/csv", invoicesBasePath, invoiceUUID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var root bytes.Buffer
resp, err := s.client.Do(ctx, req, &root)
if err != nil {
return nil, resp, err
}
return root.Bytes(), resp, err
}