forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
invoices.go
549 lines (488 loc) · 20 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package recurly
import (
"bytes"
"context"
"encoding/xml"
"fmt"
"net/http"
)
// InvoicesService manages the interactions for invoices.
type InvoicesService interface {
// List returns a pager to paginate invoices. PagerOptions are used to optionally
// filter the results.
//
// https://dev.recurly.com/docs/list-invoices
List(opts *PagerOptions) Pager
// ListAccount returns a pager to paginate invoices for an account. Params
// are used to optionally filter the results.
//
// https://dev.recurly.com/docs/list-an-accounts-invoices
ListAccount(accountCode string, opts *PagerOptions) Pager
// Get retrieves an invoice. If the invoice does not exist,
// a nil invoice and nil error are returned.
//
// https://dev.recurly.com/docs/lookup-invoice-details
Get(ctx context.Context, invoiceNumber int) (*Invoice, error)
// GetPDF retrieves an invoice as a PDF. If the invoice does not exist,
// a nil bytes.Buffer and nil error are returned. If language is not provided
// or not valid, English will be used.
//
// https://dev.recurly.com/docs/retrieve-a-pdf-invoice
GetPDF(ctx context.Context, invoiceNumber int, language string) (*bytes.Buffer, error)
// Preview allows you to display the invoice details, including estimated
// tax, before you post it.
//
// https://dev.recurly.com/docs/preview-an-invoice
Preview(ctx context.Context, accountCode string) (*Invoice, error)
// Create posts an invoice with pending adjustments to the account.
// See Recurly's documentation for details.
//
// https://dev.recurly.com/docs/post-an-invoice-invoice-pending-charges-on-an-acco
Create(ctx context.Context, accountCode string, invoice Invoice) (*Invoice, error)
// Collect allows collecting a past-due or pending invoice. This API
// is rate limited. See Recurly's documentation for details.
//
// https://dev.recurly.com/docs/collect-an-invoice
Collect(ctx context.Context, invoiceNumber int, collectInvoice CollectInvoice) (*Invoice, error)
// MarkPaid marks an invoice as paid successfully.
//
// https://dev.recurly.com/docs/mark-an-invoice-as-paid-successfully
MarkPaid(ctx context.Context, invoiceNumber int) (*Invoice, error)
// MarkFailed marks an invoice as failed collection.
//
// https://dev.recurly.com/docs/mark-an-invoice-as-failed-collection
MarkFailed(ctx context.Context, invoiceNumber int) (*Invoice, error)
// RefundVoidLineItems allows specific invoice line items and/or quantities to
// be refunded and generates a refund invoice. Full amount line item refunds
//of invoices with an unsettled transaction will voide the transaction and
// generate a void invoice. See Recurly's documentation for details.
//
// https://dev.recurly.com/docs/line-item-refunds
RefundVoidLineItems(ctx context.Context, invoiceNumber int, refund InvoiceLineItemsRefund) (*Invoice, error)
// RefundVoidOpenAmount allows custom invoice amounts to be refunded and
// generates a refund invoice. Full open amount refunds of invoices with
// an unsettled transaction will void the transaction and generate a
// void invoice. See Recurly's documentation for details.
//
// https://dev.recurly.com/docs/open-amount-refunds
RefundVoidOpenAmount(ctx context.Context, invoiceNumber int, refund InvoiceRefund) (*Invoice, error)
// VoidCreditInvoice voids an open credit invoice.
//
// https://dev.recurly.com/docs/void-credit-invoice
VoidCreditInvoice(ctx context.Context, invoiceNumber int) (*Invoice, error)
// RecordPayment records an offline payment for a manual invoice, such as a check
// or money order.
//
// https://dev.recurly.com/docs/enter-an-offline-payment-for-a-manual-invoice
RecordPayment(ctx context.Context, offlinePayment OfflinePayment) (*Transaction, error)
}
// Invoice state constants.
// https://docs.recurly.com/docs/invoices#section-statuses
const (
ChargeInvoiceStatePending = "pending"
ChargeInvoiceStateProcessing = "processing"
ChargeInvoiceStatePastDue = "past_due"
ChargeInvoiceStatePaid = "paid"
ChargeInvoiceStateFailed = "failed"
CreditInvoiceStateOpen = "open"
CreditInvoiceStateProcessing = "processing"
CreditInvoiceStateClosed = "closed"
CreditInvoiceStateVoided = "voided"
)
// Collection method constants.
const (
CollectionMethodAutomatic = "automatic"
CollectionMethodManual = "manual"
)
// 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. Transactions are guaranteed
// to be sorted from oldest to newest by date.
// 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 = v.ToInvoice()
return nil
}
// InvoiceCollection is the data type returned from Preview, Post,
// MarkFailed, and inside PreviewSubscription, and PreviewSubscriptionChange.
type InvoiceCollection struct {
XMLName xml.Name `xml:"invoice_collection"`
ChargeInvoice *Invoice `xml:"-"`
CreditInvoices []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"`
CreditInvoices []struct {
XMLName xml.Name `xml:"credit_invoice,omitempty"`
invoiceFields
} `xml:"credit_invoices>credit_invoice,omitempty"`
}
if err := d.DecodeElement(&v, &start); err != nil {
return err
}
chargeInvoice := v.ChargeInvoice.ToInvoice()
creditInvoices := make([]Invoice, len(v.CreditInvoices))
for i := range v.CreditInvoices {
creditInvoices[i] = v.CreditInvoices[i].ToInvoice()
}
*i = InvoiceCollection{
XMLName: xml.Name{Local: "invoice_collection"},
ChargeInvoice: &chargeInvoice,
CreditInvoices: creditInvoices,
}
return nil
}
// invoiceFields is used by custom unmarshal functions.
type invoiceFields struct {
AccountCode href `xml:"account,omitempty"`
Address Address `xml:"address,omitempty"`
SubscriptionUUID href `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"`
}
// convert to Invoice and sort transactions.
func (i invoiceFields) ToInvoice() Invoice {
inv := Invoice{
XMLName: xml.Name{Local: "invoice"},
AccountCode: i.AccountCode.LastPartOfPath(),
Address: i.Address,
OriginalInvoiceNumber: i.OriginalInvoiceNumber.LastPartOfPath(),
UUID: i.UUID,
State: i.State,
InvoiceNumberPrefix: i.InvoiceNumberPrefix,
InvoiceNumber: i.InvoiceNumber,
PONumber: i.PONumber,
VATNumber: i.VATNumber,
DiscountInCents: i.DiscountInCents,
SubtotalInCents: i.SubtotalInCents,
TaxInCents: i.TaxInCents,
TotalInCents: i.TotalInCents,
BalanceInCents: i.BalanceInCents,
Currency: i.Currency,
DueOn: i.DueOn,
CreatedAt: i.CreatedAt,
UpdatedAt: i.UpdatedAt,
AttemptNextCollectionAt: i.AttemptNextCollectionAt,
ClosedAt: i.ClosedAt,
Type: i.Type,
Origin: i.Origin,
TaxType: i.TaxType,
TaxRegion: i.TaxRegion,
TaxRate: i.TaxRate,
NetTerms: i.NetTerms,
CollectionMethod: i.CollectionMethod,
LineItems: i.LineItems,
Transactions: i.Transactions,
CreditPayments: i.CreditPayments,
}
Transactions(inv.Transactions).Sort()
return inv
}
// OfflinePayment is a payment received outside of Recurly (e.g. ACH/Wire).
type OfflinePayment struct {
XMLName xml.Name `xml:"transaction"`
InvoiceNumber int `xml:"-"`
PaymentMethod string `xml:"payment_method"`
CollectedAt NullTime `xml:"collected_at,omitempty"`
Amount int `xml:"amount_in_cents,omitempty"`
Description string `xml:"description,omitempty"`
}
// InvoiceRefund is used to refund invoices.
type InvoiceRefund struct {
XMLName xml.Name `xml:"invoice"`
AmountInCents NullInt `xml:"amount_in_cents,omitempty"` // If left empty the remaining refundable amount will be refunded
RefundMethod string `xml:"refund_method,omitempty"`
ExternalRefund NullBool `xml:"external_refund,omitempty"`
CreditCustomerNotes string `xml:"credit_customer_notes,omitempty"`
PaymentMethod string `xml:"payment_method,omitempty"`
Description string `xml:"description,omitempty"`
RefundedAt NullTime `xml:"refunded_at,omitempty"`
}
// CollectInvoice is used as the request body for collecting an invoice.
type CollectInvoice struct {
XMLName xml.Name `xml:"invoice"`
TransactionType string `xml:"transaction_type,omitempty"` // Optional transaction type. Currently accepts "moto"
BillingInfo *Billing `xml:"billing_info,omitempty"`
}
// InvoiceLineItemsRefund is used to refund one or more line items on an invoice.
type InvoiceLineItemsRefund struct {
XMLName xml.Name `xml:"invoice"`
LineItems []VoidLineItem `xml:"line_items>adjustment"`
InvoiceRefund
}
// VoidLineItem is an individual line item to refund.
type VoidLineItem struct {
XMLName xml.Name `xml:"adjustment"`
UUID string `xml:"uuid"` // Adjustment UUID
Quantity int `xml:"quantity"`
Prorate NullBool `xml:"prorate,omitempty"`
}
var _ InvoicesService = &invoicesImpl{}
// invoicesImpl implements InvoicesService.
type invoicesImpl serviceImpl
func (s *invoicesImpl) List(opts *PagerOptions) Pager {
return s.client.newPager("GET", "/invoices", opts)
}
func (s *invoicesImpl) ListAccount(accountCode string, opts *PagerOptions) Pager {
path := fmt.Sprintf("/accounts/%s/invoices", accountCode)
return s.client.newPager("GET", path, opts)
}
func (s *invoicesImpl) Get(ctx context.Context, invoiceNumber int) (*Invoice, error) {
path := fmt.Sprintf("/invoices/%d", invoiceNumber)
req, err := s.client.newRequest("GET", path, nil)
if err != nil {
return nil, err
}
var dst Invoice
if _, err := s.client.do(ctx, req, &dst); err != nil {
if e, ok := err.(*ClientError); ok && e.Response.StatusCode == http.StatusNotFound {
return nil, nil
}
return nil, err
}
return &dst, nil
}
func (s *invoicesImpl) GetPDF(ctx context.Context, invoiceNumber int, language string) (*bytes.Buffer, error) {
switch language {
case "English", "Danish", "German", "Spanish", "French", "Hindi",
"Japanese", "Dutch", "Portuguese", "Russian", "Turkish", "Chinese":
default:
language = "English"
}
path := fmt.Sprintf("/invoices/%d", invoiceNumber)
req, err := s.client.newRequest("GET", path, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/pdf")
req.Header.Set("Accept-Language", language)
b := new(bytes.Buffer)
if _, err := s.client.do(ctx, req, b); err != nil {
if e, ok := err.(*ClientError); ok && e.Response.StatusCode == http.StatusNotFound {
return nil, nil
}
return nil, err
}
return b, nil
}
func (s *invoicesImpl) Preview(ctx context.Context, accountCode string) (*Invoice, error) {
path := fmt.Sprintf("/accounts/%s/invoices/preview", accountCode)
req, err := s.client.newRequest("POST", path, nil)
if err != nil {
return nil, err
}
var dst InvoiceCollection
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return dst.ChargeInvoice, nil
}
func (s *invoicesImpl) Create(ctx context.Context, accountCode string, invoice Invoice) (*Invoice, error) {
path := fmt.Sprintf("/accounts/%s/invoices", accountCode)
req, err := s.client.newRequest("POST", path, invoice)
if err != nil {
return nil, err
}
var dst InvoiceCollection
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return dst.ChargeInvoice, nil
}
func (s *invoicesImpl) Collect(ctx context.Context, invoiceNumber int, collectInvoice CollectInvoice) (*Invoice, error) {
path := fmt.Sprintf("/invoices/%d/collect", invoiceNumber)
req, err := s.client.newRequest("PUT", path, collectInvoice)
if err != nil {
return nil, err
}
var dst Invoice
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *invoicesImpl) MarkPaid(ctx context.Context, invoiceNumber int) (*Invoice, error) {
path := fmt.Sprintf("/invoices/%d/mark_successful", invoiceNumber)
req, err := s.client.newRequest("PUT", path, nil)
if err != nil {
return nil, err
}
var dst Invoice
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *invoicesImpl) MarkFailed(ctx context.Context, invoiceNumber int) (*Invoice, error) {
path := fmt.Sprintf("/invoices/%d/mark_failed", invoiceNumber)
req, err := s.client.newRequest("PUT", path, nil)
if err != nil {
return nil, err
}
var dst InvoiceCollection
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return dst.ChargeInvoice, nil
}
func (s *invoicesImpl) RefundVoidLineItems(ctx context.Context, invoiceNumber int, refund InvoiceLineItemsRefund) (*Invoice, error) {
for i := range refund.LineItems {
refund.LineItems[i].UUID = sanitizeUUID(refund.LineItems[i].UUID)
}
path := fmt.Sprintf("/invoices/%d/refund", invoiceNumber)
req, err := s.client.newRequest("POST", path, refund)
if err != nil {
return nil, err
}
var dst Invoice
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *invoicesImpl) RefundVoidOpenAmount(ctx context.Context, invoiceNumber int, refund InvoiceRefund) (*Invoice, error) {
path := fmt.Sprintf("/invoices/%d/refund", invoiceNumber)
req, err := s.client.newRequest("POST", path, refund)
if err != nil {
return nil, err
}
var dst Invoice
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *invoicesImpl) VoidCreditInvoice(ctx context.Context, invoiceNumber int) (*Invoice, error) {
path := fmt.Sprintf("/invoices/%d/void", invoiceNumber)
req, err := s.client.newRequest("PUT", path, nil)
if err != nil {
return nil, err
}
var dst Invoice
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *invoicesImpl) RecordPayment(ctx context.Context, offlinePayment OfflinePayment) (*Transaction, error) {
path := fmt.Sprintf("/invoices/%d/transactions", offlinePayment.InvoiceNumber)
req, err := s.client.newRequest("POST", path, offlinePayment)
if err != nil {
return nil, err
}
var dst Transaction
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}