forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoices.go
263 lines (245 loc) · 11.2 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package recurly
import (
"encoding/xml"
"time"
)
// Invoice state constants.
// https://docs.recurly.com/docs/credit-invoices-release#section-invoice-attribute-changes
const (
ChargeInvoiceStatePending = "pending" // previously "open"
ChargeInvoiceStateProcessing = "processing" // ACH payments only
ChargeInvoiceStatePastDue = "past_due"
ChargeInvoiceStatePaid = "paid" // previously "collected"
ChargeInvoiceStateFailed = "failed"
CreditInvoiceStateOpen = "open"
CreditInvoiceStateProcessing = "processing" // ACH/bank refund processing
CreditInvoiceStateClosed = "closed"
CreditInvoiceStateVoided = "voided"
// Deprecated
InvoiceStateOpenDeprecated = "open"
InvoiceStateCollectedDeprecated = "collected"
)
// Collection method constants.
const (
CollectionMethodAutomatic = "automatic" // card on file
CollectionMethodManual = "manual" // external payment method
)
// Payment method constants.
const (
PaymentMethodCreditCard = "credit_card"
PaymentMethodPayPal = "paypal"
PaymentMethodEFT = "eft"
PaymentMethodWireTransfer = "wire_transfer"
PaymentMethodMoneyOrder = "money_order"
PaymentMethodCheck = "check"
PaymentMethodOther = "other"
)
// Invoice origin constants.
const (
ChargeInvoiceOriginPurchase = "purchase"
ChargeInvoiceOriginRenewal = "renewal"
ChargeInvoiceOriginImmediateChange = "immediate_change"
ChargeInvoiceOriginTermination = "termination"
CreditInvoiceOriginGiftCard = "gift_card"
CreditInvoiceOriginRefund = "refund"
CreditInvoiceOriginCredit = "credit"
CreditInvoiceOriginWriteOff = "write_off"
CreditInvoiceOriginExternalCredit = "external_credit"
)
// Invoice type constants.
const (
InvoiceTypeCharge = "charge"
InvoiceTypeCredit = "credit"
InvoiceTypeLegacy = "legacy" // all invoices prior to change have type legacy
)
// Refund constants.
const (
VoidRefundMethodTransactionFirst = "transaction_first"
VoidRefundMethodCreditFirst = "credit_first"
)
// Invoice is an individual invoice for an account.
// The only fields annotated with XML tags are those for posting an invoice.
// Unmarshaling an invoice is handled by the custom UnmarshalXML function.
type Invoice struct {
XMLName xml.Name `xml:"invoice,omitempty"`
AccountCode string `xml:"-"`
Address Address `xml:"-"`
OriginalInvoiceNumber int `xml:"-"`
UUID string `xml:"-"`
State string `xml:"-"`
InvoiceNumberPrefix string `xml:"-"`
InvoiceNumber int `xml:"-"`
PONumber string `xml:"po_number,omitempty"` // PostInvoice param
VATNumber string `xml:"-"`
DiscountInCents int `xml:"-"`
SubtotalInCents int `xml:"-"`
TaxInCents int `xml:"-"`
TotalInCents int `xml:"-"`
BalanceInCents int `xml:"-"`
Currency string `xml:"-"`
DueOn NullTime `xml:"-"`
CreatedAt NullTime `xml:"-"`
UpdatedAt NullTime `xml:"-"`
AttemptNextCollectionAt NullTime `xml:"-"`
ClosedAt NullTime `xml:"-"`
Type string `xml:"-"`
Origin string `xml:"-"`
TaxType string `xml:"-"`
TaxRegion string `xml:"-"`
TaxRate float64 `xml:"-"`
NetTerms NullInt `xml:"net_terms,omitempty"` // PostInvoice param
CollectionMethod string `xml:"collection_method,omitempty"` // PostInvoice param
TermsAndConditions string `xml:"terms_and_conditions,omitempty"` // PostInvoice param
CustomerNotes string `xml:"customer_notes,omitempty"` // PostInvoice param
VatReverseChargeNotes string `xml:"vat_reverse_charge_notes,omitempty"` // PostInvoice param
LineItems []Adjustment `xml:"-"`
Transactions []Transaction `xml:"-"`
CreditPayments []CreditPayment `xml:"-"`
}
// UnmarshalXML unmarshals invoices and handles intermediary state during unmarshaling
// for types like href.
func (i *Invoice) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v struct {
XMLName xml.Name `xml:"invoice,omitempty"`
invoiceFields
}
if err := d.DecodeElement(&v, &start); err != nil {
return err
}
*i = Invoice{
XMLName: v.XMLName,
AccountCode: string(v.AccountCode),
Address: v.Address,
OriginalInvoiceNumber: int(v.OriginalInvoiceNumber),
UUID: v.UUID,
State: v.State,
InvoiceNumberPrefix: v.InvoiceNumberPrefix,
InvoiceNumber: v.InvoiceNumber,
PONumber: v.PONumber,
VATNumber: v.VATNumber,
DiscountInCents: v.DiscountInCents,
SubtotalInCents: v.SubtotalInCents,
TaxInCents: v.TaxInCents,
TotalInCents: v.TotalInCents,
BalanceInCents: v.BalanceInCents,
Currency: v.Currency,
DueOn: v.DueOn,
CreatedAt: v.CreatedAt,
UpdatedAt: v.UpdatedAt,
AttemptNextCollectionAt: v.AttemptNextCollectionAt,
ClosedAt: v.ClosedAt,
Type: v.Type,
Origin: v.Origin,
TaxType: v.TaxType,
TaxRegion: v.TaxRegion,
TaxRate: v.TaxRate,
NetTerms: v.NetTerms,
CollectionMethod: v.CollectionMethod,
LineItems: v.LineItems,
Transactions: v.Transactions,
CreditPayments: v.CreditPayments,
}
return nil
}
// InvoiceCollection is the data type returned from Preview, Post,
// MarkFailed, and inside PreviewSubscription, and PreviewSubscriptionChange.
// In v2.12 this struct will include `credit_invoices`.
type InvoiceCollection struct {
XMLName xml.Name `xml:"invoice_collection"`
ChargeInvoice *Invoice `xml:"-"`
}
// UnmarshalXML unmarshals invoices and handles intermediary state during unmarshaling
// for types like href.
func (i *InvoiceCollection) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v struct {
XMLName xml.Name `xml:"invoice_collection"`
ChargeInvoice struct {
XMLName xml.Name `xml:"charge_invoice,omitempty"`
invoiceFields
} `xml:"charge_invoice,omitempty"`
}
if err := d.DecodeElement(&v, &start); err != nil {
return err
}
invoice := Invoice{
XMLName: xml.Name{Local: "invoice"},
AccountCode: string(v.ChargeInvoice.AccountCode),
Address: v.ChargeInvoice.Address,
OriginalInvoiceNumber: int(v.ChargeInvoice.OriginalInvoiceNumber),
UUID: v.ChargeInvoice.UUID,
State: v.ChargeInvoice.State,
InvoiceNumberPrefix: v.ChargeInvoice.InvoiceNumberPrefix,
InvoiceNumber: v.ChargeInvoice.InvoiceNumber,
PONumber: v.ChargeInvoice.PONumber,
VATNumber: v.ChargeInvoice.VATNumber,
DiscountInCents: v.ChargeInvoice.DiscountInCents,
SubtotalInCents: v.ChargeInvoice.SubtotalInCents,
TaxInCents: v.ChargeInvoice.TaxInCents,
TotalInCents: v.ChargeInvoice.TotalInCents,
BalanceInCents: v.ChargeInvoice.BalanceInCents,
Currency: v.ChargeInvoice.Currency,
DueOn: v.ChargeInvoice.DueOn,
CreatedAt: v.ChargeInvoice.CreatedAt,
UpdatedAt: v.ChargeInvoice.UpdatedAt,
AttemptNextCollectionAt: v.ChargeInvoice.AttemptNextCollectionAt,
ClosedAt: v.ChargeInvoice.ClosedAt,
Type: v.ChargeInvoice.Type,
Origin: v.ChargeInvoice.Origin,
TaxType: v.ChargeInvoice.TaxType,
TaxRegion: v.ChargeInvoice.TaxRegion,
TaxRate: v.ChargeInvoice.TaxRate,
NetTerms: v.ChargeInvoice.NetTerms,
CollectionMethod: v.ChargeInvoice.CollectionMethod,
LineItems: v.ChargeInvoice.LineItems,
Transactions: v.ChargeInvoice.Transactions,
CreditPayments: v.ChargeInvoice.CreditPayments,
}
*i = InvoiceCollection{
XMLName: xml.Name{Local: "invoice_collection"},
ChargeInvoice: &invoice,
}
return nil
}
// invoiceFields is used by custom unmarshal functions.
type invoiceFields struct {
AccountCode hrefString `xml:"account,omitempty"`
Address Address `xml:"address,omitempty"`
SubscriptionUUID hrefString `xml:"subscription,omitempty"`
OriginalInvoiceNumber hrefInt `xml:"original_invoice,omitempty"`
UUID string `xml:"uuid,omitempty"`
State string `xml:"state,omitempty"`
InvoiceNumberPrefix string `xml:"invoice_number_prefix,omitempty"`
InvoiceNumber int `xml:"invoice_number,omitempty"`
PONumber string `xml:"po_number,omitempty"`
VATNumber string `xml:"vat_number,omitempty"`
DiscountInCents int `xml:"discount_in_cents,omitempty"`
SubtotalInCents int `xml:"subtotal_in_cents,omitempty"`
TaxInCents int `xml:"tax_in_cents,omitempty"`
TotalInCents int `xml:"total_in_cents,omitempty"`
BalanceInCents int `xml:"balance_in_cents,omitempty"`
Currency string `xml:"currency,omitempty"`
DueOn NullTime `xml:"due_on,omitempty"`
CreatedAt NullTime `xml:"created_at,omitempty"`
UpdatedAt NullTime `xml:"updated_at,omitempty"`
AttemptNextCollectionAt NullTime `xml:"attempt_next_collection_at,omitempty"`
ClosedAt NullTime `xml:"closed_at,omitempty"`
Type string `xml:"type,omitempty"`
Origin string `xml:"origin,omitempty"`
TaxType string `xml:"tax_type,omitempty"`
TaxRegion string `xml:"tax_region,omitempty"`
TaxRate float64 `xml:"tax_rate,omitempty"`
NetTerms NullInt `xml:"net_terms,omitempty"`
CollectionMethod string `xml:"collection_method,omitempty"`
LineItems []Adjustment `xml:"line_items>adjustment,omitempty"`
Transactions []Transaction `xml:"transactions>transaction,omitempty"`
CreditPayments []CreditPayment `xml:"credit_payments>credit_payment,omitempty"`
}
// OfflinePayment is a payment received outside the system to be recorded in Recurly.
type OfflinePayment struct {
XMLName xml.Name `xml:"transaction"`
InvoiceNumber int `xml:"-"`
PaymentMethod string `xml:"payment_method"`
CollectedAt *time.Time `xml:"collected_at,omitempty"`
Amount int `xml:"amount_in_cents,omitempty"`
Description string `xml:"description,omitempty"`
}