-
Notifications
You must be signed in to change notification settings - Fork 44
/
adjustments.go
222 lines (199 loc) · 8.49 KB
/
adjustments.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
package recurly
import (
"context"
"encoding/xml"
"fmt"
"net/http"
)
// AdjustmentsService manages the interactions for adjustments.
type AdjustmentsService interface {
// List returns a pager to paginate adjustments for an account. PagerOptions are
// used to optionally filter the results.
//
// https://dev.recurly.com/docs/list-an-accounts-adjustments
ListAccount(accountCode string, opts *PagerOptions) Pager
// Get retrieves an adjustment. If the add on does not exist,
// a nil adjustment and nil error are returned.
//
// NOTE: Link below is from v2.9, the last documented version showing
// this endpoint. The endpoint appears to still be valid in v2.19, waiting
// to hear back from Recurly support.
// https://dev.recurly.com/v2.9/docs/get-an-adjustment
Get(ctx context.Context, uuid string) (*Adjustment, error)
// Create creates a one-time charge on an account. Charges are not invoiced
// or collected immediately. Non-invoiced charges will automatically be
// invoiced when the account's subscription renews, or you trigger a
// collection by posting an invoice. Charges may be removed from an account
// if they have not been invoiced.
//
// For a charge, set a.UnitAmountInCents to a positive number.
// For a credit, set a.UnitAmountInCents to a negative amount.
//
// https://dev.recurly.com/docs/create-a-charge
// https://dev.recurly.com/docs/create-a-credit
Create(ctx context.Context, accountCode string, a Adjustment) (*Adjustment, error)
// Delete deletes an adjustment from an account. Only non-invoiced adjustments
// can be deleted.
//
// https://dev.recurly.com/docs/delete-an-adjustment
Delete(ctx context.Context, uuid string) error
}
// Adjustment state constants.
const (
AdjustmentStatePending = "pending"
AdjustmentStateInvoied = "invoiced"
)
// Revenue schedule type constants.
const (
RevenueScheduleTypeNever = "never"
RevenueScheduleTypeAtRangeStart = "at_range_start"
RevenueScheduleTypeAtInvoice = "at_invoice"
RevenueScheduleTypeEvenly = "evenly" // if 'end_date' is set
RevenueScheduleTypeAtRangeEnd = "at_range_end" // if 'end_date' is set
)
// Adjustment works with charges and credits on a given account.
//
// https://dev.recurly.com/docs/adjustment-object
type Adjustment struct {
XMLName xml.Name `xml:"adjustment"`
AccountCode string `xml:"-"` // Read only
InvoiceNumber int `xml:"-"` // Read only
SubscriptionUUID string `xml:"-"` // Read only
UUID string `xml:"uuid,omitempty"`
State string `xml:"state,omitempty"`
Description string `xml:"description,omitempty"`
AccountingCode string `xml:"accounting_code,omitempty"`
RevenueScheduleType string `xml:"revenue_schedule_type,omitempty"`
ProductCode string `xml:"product_code,omitempty"`
Origin string `xml:"origin,omitempty"`
UnitAmountInCents NullInt `xml:"unit_amount_in_cents,omitempty"`
Quantity int `xml:"quantity,omitempty"`
OriginalAdjustmentUUID string `xml:"original_adjustment_uuid,omitempty"`
DiscountInCents int `xml:"discount_in_cents,omitempty"`
TaxInCents int `xml:"tax_in_cents,omitempty"`
TotalInCents int `xml:"total_in_cents,omitempty"`
Currency string `xml:"currency"`
TaxCode string `xml:"tax_code,omitempty"`
TaxType string `xml:"tax_type,omitempty"`
TaxRegion string `xml:"tax_region,omitempty"`
TaxRate float64 `xml:"tax_rate,omitempty"`
TaxExempt NullBool `xml:"tax_exempt,omitempty"`
TaxDetails []TaxDetail `xml:"tax_details>tax_detail,omitempty"`
// The following are only valid with an `Avalara for Communications` integration
AvalaraTransactionType int `xml:"avalara_transaction_type,omitempty"`
AvalaraServiceType int `xml:"avalara_service_type,omitempty"`
StartDate NullTime `xml:"start_date,omitempty"`
EndDate NullTime `xml:"end_date,omitempty"`
CreatedAt NullTime `xml:"created_at,omitempty"`
UpdatedAt NullTime `xml:"updated_at,omitempty"`
}
// MarshalXML marshals only the fields needed for creating/updating adjustments
// with the recurly API.
func (a Adjustment) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.Encode(struct {
XMLName xml.Name `xml:"adjustment"`
Description string `xml:"description,omitempty"`
AccountingCode string `xml:"accounting_code,omitempty"`
RevenueScheduleType string `xml:"revenue_schedule_type,omitempty"`
ProductCode string `xml:"product_code,omitempty"`
Origin string `xml:"origin,omitempty"`
UnitAmountInCents NullInt `xml:"unit_amount_in_cents,omitempty"`
Quantity int `xml:"quantity,omitempty"`
Currency string `xml:"currency,omitempty"`
TaxCode string `xml:"tax_code,omitempty"`
TaxExempt NullBool `xml:"tax_exempt,omitempty"`
AvalaraTransactionType int `xml:"avalara_transaction_type,omitempty"`
AvalaraServiceType int `xml:"avalara_service_type,omitempty"`
StartDate NullTime `xml:"start_date,omitempty"`
EndDate NullTime `xml:"end_date,omitempty"`
}{
Description: a.Description,
AccountingCode: a.AccountingCode,
RevenueScheduleType: a.RevenueScheduleType,
ProductCode: a.ProductCode,
Origin: a.Origin,
UnitAmountInCents: a.UnitAmountInCents,
Quantity: a.Quantity,
Currency: a.Currency,
TaxCode: a.TaxCode,
TaxExempt: a.TaxExempt,
AvalaraServiceType: a.AvalaraServiceType,
AvalaraTransactionType: a.AvalaraTransactionType,
StartDate: a.StartDate,
EndDate: a.EndDate,
})
}
// UnmarshalXML unmarshal an adjustment.
func (a *Adjustment) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type adjustmentAlias Adjustment
var v struct {
XMLName xml.Name `xml:"adjustment"`
adjustmentAlias
AccountCode href `xml:"account,omitempty"`
InvoiceNumber hrefInt `xml:"invoice,omitempty"`
SubscriptionUUID href `xml:"subscription,omitempty"`
}
if err := d.DecodeElement(&v, &start); err != nil {
return err
}
*a = Adjustment(v.adjustmentAlias)
a.XMLName = v.XMLName
a.AccountCode = v.AccountCode.LastPartOfPath()
a.InvoiceNumber = v.InvoiceNumber.LastPartOfPath()
a.SubscriptionUUID = v.SubscriptionUUID.LastPartOfPath()
return nil
}
// TaxDetail holds tax information and is embedded in an Adjustment.
// TaxDetails are a read only field, so they shouldn't marshal.
type TaxDetail struct {
XMLName xml.Name `xml:"tax_detail"`
Name string `xml:"name,omitempty"`
Type string `xml:"type,omitempty"`
TaxRate float64 `xml:"tax_rate,omitempty"`
TaxInCents int `xml:"tax_in_cents,omitempty"`
Level string `xml:"level,omitempty"`
Billable NullBool `xml:"billable,omitempty"`
}
var _ AdjustmentsService = &adjustmentsImpl{}
// adjustmentsImpl implements AdjustmentsService.
type adjustmentsImpl serviceImpl
func (s *adjustmentsImpl) ListAccount(accountCode string, opts *PagerOptions) Pager {
path := fmt.Sprintf("/accounts/%s/adjustments", accountCode)
return s.client.newPager("GET", path, opts)
}
func (s *adjustmentsImpl) Get(ctx context.Context, uuid string) (*Adjustment, error) {
path := fmt.Sprintf("/adjustments/%s", sanitizeUUID(uuid))
req, err := s.client.newRequest("GET", path, nil)
if err != nil {
return nil, err
}
var dst Adjustment
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 *adjustmentsImpl) Create(ctx context.Context, accountCode string, a Adjustment) (*Adjustment, error) {
path := fmt.Sprintf("/accounts/%s/adjustments", accountCode)
req, err := s.client.newRequest("POST", path, a)
if err != nil {
return nil, err
}
var dst Adjustment
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *adjustmentsImpl) Delete(ctx context.Context, uuid string) error {
path := fmt.Sprintf("/adjustments/%s", sanitizeUUID(uuid))
req, err := s.client.newRequest("DELETE", path, nil)
if err != nil {
return err
}
_, err = s.client.do(ctx, req, nil)
return err
}