-
Notifications
You must be signed in to change notification settings - Fork 3
/
invoices.go
123 lines (111 loc) · 5.1 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
package chartmogul
import "strings"
const (
invoicesEndpoint = "invoices"
singleInvoiceEndpoint = "invoices/:uuid"
customersInvoicesEndpoint = "import/customers/:customerUUID/invoices"
)
// Invoices is wrapper for bulk importing invoices
// In case of /v1/invoices endpoint, the customer_uuid is on individual invoices and here it's empty.
type Invoices struct {
CustomerUUID string `json:"customer_uuid,omitempty"`
Error string `json:"error,omitempty"`
Invoices []*Invoice `json:"invoices"`
Pagination
}
// Invoice is the data for ChartMogul to auto-generate subscriptions.
type Invoice struct {
UUID string `json:"uuid,omitempty"`
CustomerUUID string `json:"customer_uuid,omitempty"`
CustomerExternalID string `json:"customer_external_id,omitempty"`
Currency string `json:"currency"`
DataSourceUUID string `json:"data_source_uuid,omitempty"`
Date string `json:"date"`
DueDate string `json:"due_date,omitempty"`
ExternalID string `json:"external_id"`
LineItems []*LineItem `json:"line_items"`
Transactions []*Transaction `json:"transactions,omitempty"`
Errors *Errors `json:"errors,omitempty"`
}
// LineItem represents a singular items of the invoices
type LineItem struct {
UUID string `json:"uuid,omitempty"`
AccountCode string `json:"account_code,omitempty"`
AmountInCents int `json:"amount_in_cents"`
CancelledAt string `json:"cancelled_at,omitempty"`
Description string `json:"description,omitempty"`
DiscountAmountInCents int `json:"discount_amount_in_cents,omitempty"`
DiscountCode string `json:"discount_code,omitempty"`
ExternalID string `json:"external_id,omitempty"`
PlanUUID string `json:"plan_uuid,omitempty"`
Prorated bool `json:"prorated,omitempty"`
Quantity int `json:"quantity,omitempty"`
ServicePeriodEnd string `json:"service_period_end,omitempty"`
ServicePeriodStart string `json:"service_period_start,omitempty"`
SubscriptionExternalID string `json:"subscription_external_id,omitempty"`
SubscriptionSetExternalID string `json:"subscription_set_external_id,omitempty"`
SubscriptionUUID string `json:"subscription_uuid,omitempty"`
TaxAmountInCents int `json:"tax_amount_in_cents,omitempty"`
TransactionFeesInCents int `json:"transaction_fees_in_cents,omitempty"`
TransactionFeesCurrency string `json:"transaction_fees_currency,omitempty"`
DiscountDescription string `json:"discount_description,omitempty"`
EventOrder int `json:"event_order,omitempty"`
Type string `json:"type"`
}
// ListAllInvoicesParams optional parameters for ListAllInvoices
type ListAllInvoicesParams struct {
CustomerUUID string `json:"customer_uuid,omitempty"`
DataSourceUUID string `json:"data_source_uuid,omitempty"`
ExternalID string `json:"external_id,omitempty"`
Cursor
}
// CreateInvoices loads an invoice to a customer in Chartmogul.
// Customer must have a valid UUID! (use return value of API)
//
// See https://dev.chartmogul.com/v1.0/reference#invoices
func (api API) CreateInvoices(invoices []*Invoice, customerUUID string) (*Invoices, error) {
if len(invoices) == 0 {
return nil, nil
}
input := Invoices{Invoices: invoices}
result := &Invoices{}
path := strings.Replace(customersInvoicesEndpoint, ":customerUUID", customerUUID, 1)
return result, api.create(path, input, result)
}
// ListInvoices lists all imported invoices for a customer.
//
// See https://dev.chartmogul.com/v1.0/reference#invoices
func (api API) ListInvoices(cursor *Cursor, customerUUID string) (*Invoices, error) {
result := &Invoices{}
path := strings.Replace(customersInvoicesEndpoint, ":customerUUID", customerUUID, 1)
query := make([]interface{}, 0, 1)
if cursor != nil {
query = append(query, *cursor)
}
return result, api.list(path, result, query...)
}
// ListAllInvoices lists all imported invoices. Use parameters to narrow down the search/for paging.
// listAllInvoicesParams can be nil, in which case default values on API are used.
//
// See https://dev.chartmogul.com/v1.0/reference#invoices
func (api API) ListAllInvoices(listAllInvoicesParams *ListAllInvoicesParams) (*Invoices, error) {
result := &Invoices{}
query := make([]interface{}, 0, 1)
if listAllInvoicesParams != nil {
query = append(query, *listAllInvoicesParams)
}
return result, api.list(invoicesEndpoint, result, query...)
}
// RetrieveInvoice returns one Invoice by UUID.
//
// See https://dev.chartmogul.com/v1.0/reference#invoices
func (api API) RetrieveInvoice(invoiceUUID string) (*Invoice, error) {
result := &Invoice{}
return result, api.retrieve(singleInvoiceEndpoint, invoiceUUID, result)
}
// DeleteInvoice deletes one invoice by UUID.
//
// See https://dev.chartmogul.com/v1.0/reference#invoices
func (api API) DeleteInvoice(invoiceUUID string) error {
return api.delete(singleInvoiceEndpoint, invoiceUUID)
}