/
invoice.go
76 lines (57 loc) · 1.44 KB
/
invoice.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
package stripeutil
import (
"encoding/json"
"strings"
"time"
"github.com/stripe/stripe-go/v72"
)
// Invoice is the Invoice resource from Striped. Embedded in this struct is the
// stripe.Invoice struct from Stripe.
type Invoice struct {
*stripe.Invoice
Updated time.Time // Updated is when the Invoice was last updated.
}
var (
_ Resource = (*Invoice)(nil)
invoiceEndpoint = "/v1/invoices"
)
// RetrieveUpcomingInvoice will retrieve the upcoming Invoice for the given
// Customer.
func RetrieveUpcomingInvoice(s *Stripe, c *Customer) (*Invoice, error) {
resp, err := s.Get(invoiceEndpoint + "/upcoming?customer=" + c.ID)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if !respCode2xx(resp.StatusCode) {
return nil, s.Error(resp)
}
var inv Invoice
if err := json.NewDecoder(resp.Body).Decode(&inv.Invoice); err != nil {
return nil, err
}
return &inv, nil
}
// Endpoint implements the Resource interface.
func (i *Invoice) Endpoint(uris ...string) string {
endpoint := invoiceEndpoint
if i.ID != "" {
endpoint += "/" + i.ID
}
if len(uris) > 0 {
endpoint += "/"
}
return endpoint + strings.Join(uris, "/")
}
// Load implements the Resource interface.
func (i *Invoice) Load(s *Stripe) error {
resp, err := s.Client.Get(i.Endpoint())
if err != nil {
return err
}
defer resp.Body.Close()
if !respCode2xx(resp.StatusCode) {
return s.Error(resp)
}
return json.NewDecoder(resp.Body).Decode(&i.Invoice)
}