-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscription_custom_price.go
155 lines (147 loc) · 7.23 KB
/
subscription_custom_price.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
package models
import (
"encoding/json"
"errors"
"strings"
)
// SubscriptionCustomPrice represents a SubscriptionCustomPrice struct.
// (Optional) Used in place of `product_price_point_id` to define a custom price point unique to the subscription
type SubscriptionCustomPrice struct {
// (Optional)
Name *string `json:"name,omitempty"`
// (Optional)
Handle *string `json:"handle,omitempty"`
// Required if using `custom_price` attribute.
PriceInCents SubscriptionCustomPricePriceInCents `json:"price_in_cents"`
// Required if using `custom_price` attribute.
Interval SubscriptionCustomPriceInterval `json:"interval"`
// Required if using `custom_price` attribute.
IntervalUnit IntervalUnit `json:"interval_unit"`
// (Optional)
TrialPriceInCents *SubscriptionCustomPriceTrialPriceInCents `json:"trial_price_in_cents,omitempty"`
// (Optional)
TrialInterval *SubscriptionCustomPriceTrialInterval `json:"trial_interval,omitempty"`
// (Optional)
TrialIntervalUnit *IntervalUnit `json:"trial_interval_unit,omitempty"`
// (Optional)
InitialChargeInCents *SubscriptionCustomPriceInitialChargeInCents `json:"initial_charge_in_cents,omitempty"`
// (Optional)
InitialChargeAfterTrial *bool `json:"initial_charge_after_trial,omitempty"`
// (Optional)
ExpirationInterval *SubscriptionCustomPriceExpirationInterval `json:"expiration_interval,omitempty"`
// (Optional)
ExpirationIntervalUnit *IntervalUnit `json:"expiration_interval_unit,omitempty"`
// (Optional)
TaxIncluded *bool `json:"tax_included,omitempty"`
AdditionalProperties map[string]any `json:"_"`
}
// MarshalJSON implements the json.Marshaler interface for SubscriptionCustomPrice.
// It customizes the JSON marshaling process for SubscriptionCustomPrice objects.
func (s SubscriptionCustomPrice) MarshalJSON() (
[]byte,
error) {
return json.Marshal(s.toMap())
}
// toMap converts the SubscriptionCustomPrice object to a map representation for JSON marshaling.
func (s SubscriptionCustomPrice) toMap() map[string]any {
structMap := make(map[string]any)
MapAdditionalProperties(structMap, s.AdditionalProperties)
if s.Name != nil {
structMap["name"] = s.Name
}
if s.Handle != nil {
structMap["handle"] = s.Handle
}
structMap["price_in_cents"] = s.PriceInCents.toMap()
structMap["interval"] = s.Interval.toMap()
structMap["interval_unit"] = s.IntervalUnit
if s.TrialPriceInCents != nil {
structMap["trial_price_in_cents"] = s.TrialPriceInCents.toMap()
}
if s.TrialInterval != nil {
structMap["trial_interval"] = s.TrialInterval.toMap()
}
if s.TrialIntervalUnit != nil {
structMap["trial_interval_unit"] = s.TrialIntervalUnit
}
if s.InitialChargeInCents != nil {
structMap["initial_charge_in_cents"] = s.InitialChargeInCents.toMap()
}
if s.InitialChargeAfterTrial != nil {
structMap["initial_charge_after_trial"] = s.InitialChargeAfterTrial
}
if s.ExpirationInterval != nil {
structMap["expiration_interval"] = s.ExpirationInterval.toMap()
}
if s.ExpirationIntervalUnit != nil {
structMap["expiration_interval_unit"] = s.ExpirationIntervalUnit
}
if s.TaxIncluded != nil {
structMap["tax_included"] = s.TaxIncluded
}
return structMap
}
// UnmarshalJSON implements the json.Unmarshaler interface for SubscriptionCustomPrice.
// It customizes the JSON unmarshaling process for SubscriptionCustomPrice objects.
func (s *SubscriptionCustomPrice) UnmarshalJSON(input []byte) error {
var temp subscriptionCustomPrice
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
err = temp.validate()
if err != nil {
return err
}
additionalProperties, err := UnmarshalAdditionalProperties(input, "name", "handle", "price_in_cents", "interval", "interval_unit", "trial_price_in_cents", "trial_interval", "trial_interval_unit", "initial_charge_in_cents", "initial_charge_after_trial", "expiration_interval", "expiration_interval_unit", "tax_included")
if err != nil {
return err
}
s.AdditionalProperties = additionalProperties
s.Name = temp.Name
s.Handle = temp.Handle
s.PriceInCents = *temp.PriceInCents
s.Interval = *temp.Interval
s.IntervalUnit = *temp.IntervalUnit
s.TrialPriceInCents = temp.TrialPriceInCents
s.TrialInterval = temp.TrialInterval
s.TrialIntervalUnit = temp.TrialIntervalUnit
s.InitialChargeInCents = temp.InitialChargeInCents
s.InitialChargeAfterTrial = temp.InitialChargeAfterTrial
s.ExpirationInterval = temp.ExpirationInterval
s.ExpirationIntervalUnit = temp.ExpirationIntervalUnit
s.TaxIncluded = temp.TaxIncluded
return nil
}
// subscriptionCustomPrice is a temporary struct used for validating the fields of SubscriptionCustomPrice.
type subscriptionCustomPrice struct {
Name *string `json:"name,omitempty"`
Handle *string `json:"handle,omitempty"`
PriceInCents *SubscriptionCustomPricePriceInCents `json:"price_in_cents"`
Interval *SubscriptionCustomPriceInterval `json:"interval"`
IntervalUnit *IntervalUnit `json:"interval_unit"`
TrialPriceInCents *SubscriptionCustomPriceTrialPriceInCents `json:"trial_price_in_cents,omitempty"`
TrialInterval *SubscriptionCustomPriceTrialInterval `json:"trial_interval,omitempty"`
TrialIntervalUnit *IntervalUnit `json:"trial_interval_unit,omitempty"`
InitialChargeInCents *SubscriptionCustomPriceInitialChargeInCents `json:"initial_charge_in_cents,omitempty"`
InitialChargeAfterTrial *bool `json:"initial_charge_after_trial,omitempty"`
ExpirationInterval *SubscriptionCustomPriceExpirationInterval `json:"expiration_interval,omitempty"`
ExpirationIntervalUnit *IntervalUnit `json:"expiration_interval_unit,omitempty"`
TaxIncluded *bool `json:"tax_included,omitempty"`
}
func (s *subscriptionCustomPrice) validate() error {
var errs []string
if s.PriceInCents == nil {
errs = append(errs, "required field `price_in_cents` is missing for type `Subscription Custom Price`")
}
if s.Interval == nil {
errs = append(errs, "required field `interval` is missing for type `Subscription Custom Price`")
}
if s.IntervalUnit == nil {
errs = append(errs, "required field `interval_unit` is missing for type `Subscription Custom Price`")
}
if len(errs) == 0 {
return nil
}
return errors.New(strings.Join(errs, "\n"))
}