forked from blacklightcms/recurly
/
adjustments.go
150 lines (142 loc) · 5.91 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
package recurly
import "encoding/xml"
// Adjustment state constants
const (
AdjustmentStatePending = "pending"
AdjustmentStateInvoied = "invoiced"
)
// Adjustment works with charges and credits on a given account.
type Adjustment struct {
AccountCode string
InvoiceNumber int
SubscriptionUUID string
UUID string
State string
Description string
AccountingCode string
ProductCode string
Origin string
UnitAmountInCents int
Quantity int
OriginalAdjustmentUUID string
DiscountInCents int
TaxInCents int
TotalInCents int
Currency string
Taxable NullBool
TaxCode string
TaxType string
TaxRegion string
TaxRate float64
TaxExempt NullBool
TaxDetails []TaxDetail
StartDate NullTime
EndDate NullTime
CreatedAt NullTime
UpdatedAt NullTime
}
// 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 {
v := struct {
XMLName xml.Name `xml:"adjustment"`
Description string `xml:"description,omitempty"`
AccountingCode string `xml:"accounting_code,omitempty"`
ProductCode string `xml:"product_code,omitempty"`
UnitAmountInCents int `xml:"unit_amount_in_cents"`
Quantity int `xml:"quantity,omitempty"`
Currency string `xml:"currency,omitempty"` // Required for some operations
TaxCode string `xml:"tax_code,omitempty"`
TaxExempt NullBool `xml:"tax_exempt,omitempty"`
StartDate NullTime `xml:"start_date,omitempty"`
EndDate NullTime `xml:"end_date,omitempty"`
}{
Description: a.Description,
AccountingCode: a.AccountingCode,
ProductCode: a.ProductCode,
UnitAmountInCents: a.UnitAmountInCents,
Quantity: a.Quantity,
Currency: a.Currency,
TaxCode: a.TaxCode,
TaxExempt: a.TaxExempt,
StartDate: a.StartDate,
EndDate: a.EndDate,
}
return e.Encode(v)
}
// UnmarshalXML unmarshal a coupon redemption object. Minaly converts href links
// for coupons and accounts to CouponCode and AccountCodes.
func (a *Adjustment) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v struct {
XMLName xml.Name `xml:"adjustment"`
AccountCode hrefString `xml:"account,omitempty"` // Read only
InvoiceNumber hrefInt `xml:"invoice,omitempty"` // Read only
SubscriptionUUID hrefString `xml:"subscription,omitempty"` // Read only
UUID string `xml:"uuid,omitempty"`
State string `xml:"state,omitempty"`
Description string `xml:"description,omitempty"`
AccountingCode string `xml:"accounting_code,omitempty"`
ProductCode string `xml:"product_code,omitempty"`
Origin string `xml:"origin,omitempty"`
UnitAmountInCents int `xml:"unit_amount_in_cents"`
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"`
Taxable NullBool `xml:"taxable,omitempty"`
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"`
StartDate NullTime `xml:"start_date,omitempty"`
EndDate NullTime `xml:"end_date,omitempty"`
CreatedAt NullTime `xml:"created_at,omitempty"`
UpdatedAt NullTime `xml:"updated_at,omitempty"`
}
if err := d.DecodeElement(&v, &start); err != nil {
return err
}
*a = Adjustment{
AccountCode: string(v.AccountCode),
InvoiceNumber: int(v.InvoiceNumber),
SubscriptionUUID: string(v.SubscriptionUUID),
UUID: v.UUID,
State: v.State,
Description: v.Description,
AccountingCode: v.AccountingCode,
ProductCode: v.ProductCode,
Origin: v.Origin,
UnitAmountInCents: v.UnitAmountInCents,
Quantity: v.Quantity,
OriginalAdjustmentUUID: v.OriginalAdjustmentUUID,
DiscountInCents: v.DiscountInCents,
TaxInCents: v.TaxInCents,
TotalInCents: v.TotalInCents,
Currency: v.Currency,
Taxable: v.Taxable,
TaxCode: v.TaxCode,
TaxType: v.TaxType,
TaxRegion: v.TaxRegion,
TaxRate: v.TaxRate,
TaxExempt: v.TaxExempt,
TaxDetails: v.TaxDetails,
StartDate: v.StartDate,
EndDate: v.EndDate,
CreatedAt: v.CreatedAt,
UpdatedAt: v.UpdatedAt,
}
return nil
}
// TaxDetail holds tax information and is embedded in an Adjustment.
// TaxDetails are a read only field, so theys houldn't marshall
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"`
}