-
Notifications
You must be signed in to change notification settings - Fork 1
/
metered_component.go
202 lines (194 loc) · 10.8 KB
/
metered_component.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
package models
import (
"encoding/json"
"errors"
"strings"
)
// MeteredComponent represents a MeteredComponent struct.
type MeteredComponent struct {
// A name for this component that is suitable for showing customers and displaying on billing statements, ie. "Minutes".
Name string `json:"name"`
// The name of the unit of measurement for the component. It should be singular since it will be automatically pluralized when necessary. i.e. “message”, which may then be shown as “5 messages” on a subscription’s component line-item
UnitName string `json:"unit_name"`
// A description for the component that will be displayed to the user on the hosted signup page.
Description *string `json:"description,omitempty"`
// A unique identifier for your use that can be used to retrieve this component is subsequent requests. Must start with a letter or number and may only contain lowercase letters, numbers, or the characters '.', ':', '-', or '_'.
Handle *string `json:"handle,omitempty"`
// Boolean flag describing whether a component is taxable or not.
Taxable *bool `json:"taxable,omitempty"`
// The identifier for the pricing scheme. See [Product Components](https://help.chargify.com/products/product-components.html) for an overview of pricing schemes.
PricingScheme PricingScheme `json:"pricing_scheme"`
// (Not required for ‘per_unit’ pricing schemes) One or more price brackets. See [Price Bracket Rules](https://maxio-chargify.zendesk.com/hc/en-us/articles/5405020625677#price-bracket-rules) for an overview of how price brackets work for different pricing schemes.
Prices []Price `json:"prices,omitempty"`
// The type of credit to be created when upgrading/downgrading. Defaults to the component and then site setting if one is not provided.
// Available values: `full`, `prorated`, `none`.
UpgradeCharge Optional[CreditType] `json:"upgrade_charge"`
// The type of credit to be created when upgrading/downgrading. Defaults to the component and then site setting if one is not provided.
// Available values: `full`, `prorated`, `none`.
DowngradeCredit Optional[CreditType] `json:"downgrade_credit"`
PricePoints []ComponentPricePointItem `json:"price_points,omitempty"`
// The amount the customer will be charged per unit when the pricing scheme is “per_unit”. For On/Off Components, this is the amount that the customer will be charged when they turn the component on for the subscription. The price can contain up to 8 decimal places. i.e. 1.00 or 0.0012 or 0.00000065
UnitPrice *MeteredComponentUnitPrice `json:"unit_price,omitempty"`
// A string representing the tax code related to the component type. This is especially important when using the Avalara service to tax based on locale. This attribute has a max length of 10 characters.
TaxCode *string `json:"tax_code,omitempty"`
// (Only available on Relationship Invoicing sites) Boolean flag describing if the service date range should show for the component on generated invoices.
HideDateRangeOnInvoice *bool `json:"hide_date_range_on_invoice,omitempty"`
// deprecated May 2011 - use unit_price instead
PriceInCents *string `json:"price_in_cents,omitempty"`
DisplayOnHostedPage *bool `json:"display_on_hosted_page,omitempty"`
AllowFractionalQuantities *bool `json:"allow_fractional_quantities,omitempty"`
PublicSignupPageIds []int `json:"public_signup_page_ids,omitempty"`
// The numerical interval. i.e. an interval of ‘30’ coupled with an interval_unit of day would mean this component's default price point would renew every 30 days. This property is only available for sites with Multifrequency enabled.
Interval *int `json:"interval,omitempty"`
// A string representing the interval unit for this component's default price point, either month or day. This property is only available for sites with Multifrequency enabled.
IntervalUnit *IntervalUnit `json:"interval_unit,omitempty"`
AdditionalProperties map[string]any `json:"_"`
}
// MarshalJSON implements the json.Marshaler interface for MeteredComponent.
// It customizes the JSON marshaling process for MeteredComponent objects.
func (m MeteredComponent) MarshalJSON() (
[]byte,
error) {
return json.Marshal(m.toMap())
}
// toMap converts the MeteredComponent object to a map representation for JSON marshaling.
func (m MeteredComponent) toMap() map[string]any {
structMap := make(map[string]any)
MapAdditionalProperties(structMap, m.AdditionalProperties)
structMap["name"] = m.Name
structMap["unit_name"] = m.UnitName
if m.Description != nil {
structMap["description"] = m.Description
}
if m.Handle != nil {
structMap["handle"] = m.Handle
}
if m.Taxable != nil {
structMap["taxable"] = m.Taxable
}
structMap["pricing_scheme"] = m.PricingScheme
if m.Prices != nil {
structMap["prices"] = m.Prices
}
if m.UpgradeCharge.IsValueSet() {
if m.UpgradeCharge.Value() != nil {
structMap["upgrade_charge"] = m.UpgradeCharge.Value()
} else {
structMap["upgrade_charge"] = nil
}
}
if m.DowngradeCredit.IsValueSet() {
if m.DowngradeCredit.Value() != nil {
structMap["downgrade_credit"] = m.DowngradeCredit.Value()
} else {
structMap["downgrade_credit"] = nil
}
}
if m.PricePoints != nil {
structMap["price_points"] = m.PricePoints
}
if m.UnitPrice != nil {
structMap["unit_price"] = m.UnitPrice.toMap()
}
if m.TaxCode != nil {
structMap["tax_code"] = m.TaxCode
}
if m.HideDateRangeOnInvoice != nil {
structMap["hide_date_range_on_invoice"] = m.HideDateRangeOnInvoice
}
if m.PriceInCents != nil {
structMap["price_in_cents"] = m.PriceInCents
}
if m.DisplayOnHostedPage != nil {
structMap["display_on_hosted_page"] = m.DisplayOnHostedPage
}
if m.AllowFractionalQuantities != nil {
structMap["allow_fractional_quantities"] = m.AllowFractionalQuantities
}
if m.PublicSignupPageIds != nil {
structMap["public_signup_page_ids"] = m.PublicSignupPageIds
}
if m.Interval != nil {
structMap["interval"] = m.Interval
}
if m.IntervalUnit != nil {
structMap["interval_unit"] = m.IntervalUnit
}
return structMap
}
// UnmarshalJSON implements the json.Unmarshaler interface for MeteredComponent.
// It customizes the JSON unmarshaling process for MeteredComponent objects.
func (m *MeteredComponent) UnmarshalJSON(input []byte) error {
var temp meteredComponent
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
err = temp.validate()
if err != nil {
return err
}
additionalProperties, err := UnmarshalAdditionalProperties(input, "name", "unit_name", "description", "handle", "taxable", "pricing_scheme", "prices", "upgrade_charge", "downgrade_credit", "price_points", "unit_price", "tax_code", "hide_date_range_on_invoice", "price_in_cents", "display_on_hosted_page", "allow_fractional_quantities", "public_signup_page_ids", "interval", "interval_unit")
if err != nil {
return err
}
m.AdditionalProperties = additionalProperties
m.Name = *temp.Name
m.UnitName = *temp.UnitName
m.Description = temp.Description
m.Handle = temp.Handle
m.Taxable = temp.Taxable
m.PricingScheme = *temp.PricingScheme
m.Prices = temp.Prices
m.UpgradeCharge = temp.UpgradeCharge
m.DowngradeCredit = temp.DowngradeCredit
m.PricePoints = temp.PricePoints
m.UnitPrice = temp.UnitPrice
m.TaxCode = temp.TaxCode
m.HideDateRangeOnInvoice = temp.HideDateRangeOnInvoice
m.PriceInCents = temp.PriceInCents
m.DisplayOnHostedPage = temp.DisplayOnHostedPage
m.AllowFractionalQuantities = temp.AllowFractionalQuantities
m.PublicSignupPageIds = temp.PublicSignupPageIds
m.Interval = temp.Interval
m.IntervalUnit = temp.IntervalUnit
return nil
}
// meteredComponent is a temporary struct used for validating the fields of MeteredComponent.
type meteredComponent struct {
Name *string `json:"name"`
UnitName *string `json:"unit_name"`
Description *string `json:"description,omitempty"`
Handle *string `json:"handle,omitempty"`
Taxable *bool `json:"taxable,omitempty"`
PricingScheme *PricingScheme `json:"pricing_scheme"`
Prices []Price `json:"prices,omitempty"`
UpgradeCharge Optional[CreditType] `json:"upgrade_charge"`
DowngradeCredit Optional[CreditType] `json:"downgrade_credit"`
PricePoints []ComponentPricePointItem `json:"price_points,omitempty"`
UnitPrice *MeteredComponentUnitPrice `json:"unit_price,omitempty"`
TaxCode *string `json:"tax_code,omitempty"`
HideDateRangeOnInvoice *bool `json:"hide_date_range_on_invoice,omitempty"`
PriceInCents *string `json:"price_in_cents,omitempty"`
DisplayOnHostedPage *bool `json:"display_on_hosted_page,omitempty"`
AllowFractionalQuantities *bool `json:"allow_fractional_quantities,omitempty"`
PublicSignupPageIds []int `json:"public_signup_page_ids,omitempty"`
Interval *int `json:"interval,omitempty"`
IntervalUnit *IntervalUnit `json:"interval_unit,omitempty"`
}
func (m *meteredComponent) validate() error {
var errs []string
if m.Name == nil {
errs = append(errs, "required field `name` is missing for type `Metered Component`")
}
if m.UnitName == nil {
errs = append(errs, "required field `unit_name` is missing for type `Metered Component`")
}
if m.PricingScheme == nil {
errs = append(errs, "required field `pricing_scheme` is missing for type `Metered Component`")
}
if len(errs) == 0 {
return nil
}
return errors.New(strings.Join(errs, "\n"))
}