-
Notifications
You must be signed in to change notification settings - Fork 6
/
subscription.go
241 lines (221 loc) · 8.18 KB
/
subscription.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
package gorecurly
import (
"encoding/xml"
"errors"
"time"
)
//Subscription Struct
type Subscription struct {
XMLName xml.Name `xml:"subscription"`
endpoint string
r *Recurly
Timeframe string `xml:"timeframe,omitempty"`
Account *AccountStub `xml:"account,omitempty"`
EmbedAccount *Account `xml:"-"`
Plan *PlanStub `xml:"plan,omitempty"`
PlanCode string `xml:"-"`
UUID string `xml:"uuid,omitempty"`
State string `xml:"state,omitempty"`
UnitAmountInCents int `xml:"unit_amount_in_cents,omitempty"`
CouponCode string `xml:"-"`
Currency string `xml:"currency,omitempty"`
Quantity string `xml:"quantity,omitempty"`
ActivatedAt *time.Time `xml:"activated_at,omitempty"`
CanceledAt RecurlyDate `xml:"canceled_at,omitempty"`
ExpiresAt RecurlyDate `xml:"expires_at,omitempty"`
CurrentPeriodStartedAt *time.Time `xml:"current_period_starts_at,omitempty"`
CurrentPeriodEndsAt *time.Time `xml:"currenct_period_ends_at,omitempty"`
RemainingBillingCycles string `xml:"remaining_billing_cycles,omitempty"`
TrialStartedAt RecurlyDate `xml:"trial_started_at,omitempty"`
TrialEndsAt RecurlyDate `xml:"trial_ends_at,omitempty"`
SubscriptionAddOns EmbedPlanAddOns `xml:"subscription_add_ons,omitempty"`
StartsAt *time.Time `xml:"-"`
FirstRenewalDate *time.Time `xml:"-"`
TotalBillingCycles string `xml:"total_billing_cycles,omitempty"`
}
type subscriptionCreate struct {
XMLName xml.Name `xml:"subscription"`
PlanCode string `xml:"plan_code,omitempty"`
CouponCode string `xml:"coupon_code,omitempty"`
Account *Account `xml:"account,omitempty"`
UnitAmountInCents int `xml:"unit_amount_in_cents,omitempty"`
Currency string `xml:"currency,omitempty"`
Quantity string `xml:"quantity,omitempty"`
SubscriptionAddOns EmbedPlanAddOns `xml:"subscription_add_ons,omitempty"`
TrialEndsAt *time.Time `xml:"trial_ends_at,omitempty"`
StartsAt *time.Time `xml:"starts_at,omitempty"`
FirstRenewalDate *time.Time `xml:"first_renewal_date,omitempty"`
TotalBillingCycles string `xml:"total_billing_cycles,omitempty"`
}
type subscriptionUpdate struct {
XMLName xml.Name `xml:"subscription"`
Timeframe string `xml:"timeframe,omitempty"`
PlanCode string `xml:"plan_code,omitempty"`
UnitAmountInCents int `xml:"unit_amount_in_cents,omitempty"`
Quantity string `xml:"quantity,omitempty"`
SubscriptionAddOns EmbedPlanAddOns `xml:"subscription_add_ons,omitempty"`
}
//Attach an existing account to the subscription before creating it
func (s *Subscription) AttachExistingAccount(a Account) (e error) {
if s.UUID != "" {
return RecurlyError{statusCode: 400, Description: "Subscription Already in Use and can't attach another account to it"}
}
s.EmbedAccount = new(Account)
s.EmbedAccount.AccountCode = a.AccountCode
//s.EmbedAccount.B = a.B
return
}
//Attach a new account object to a subscription. The account will be created along with the subscription
func (s *Subscription) AttachAccount(a Account) (e error) {
if s.UUID != "" {
return RecurlyError{statusCode: 400, Description: "Subscription Already in Use and can't attach another account to it"}
}
s.EmbedAccount = new(Account)
a.CreatedAt = nil
a.State = ""
//some more may need to be blanked out
a.HostedLoginToken = ""
s.EmbedAccount = &a
return
}
//Create an account
func (s *Subscription) Create() error {
if s.UUID != "" {
return RecurlyError{statusCode: 400, Description: "Subscription Already in Use"}
}
t := new(time.Time)
decode, err := s.TrialEndsAt.GetDate()
if err == nil {
t = &decode
}
sc := subscriptionCreate{
PlanCode: s.PlanCode,
Account: s.EmbedAccount,
Currency: s.Currency,
UnitAmountInCents: s.UnitAmountInCents,
CouponCode: s.CouponCode,
Quantity: s.Quantity,
SubscriptionAddOns: s.SubscriptionAddOns,
StartsAt: s.StartsAt,
TrialEndsAt: t,
FirstRenewalDate: s.FirstRenewalDate,
TotalBillingCycles: s.TotalBillingCycles,
}
//Hack here need to investigate why the create causes the return to append
s.SubscriptionAddOns = EmbedPlanAddOns{}
if err := s.r.doCreateReturn(sc, &s, s.endpoint); err == nil {
return nil
} else {
return err
}
return nil
}
//Update an account
func (s *Subscription) Update(now bool) error {
sub := subscriptionUpdate{
PlanCode: s.PlanCode,
UnitAmountInCents: s.UnitAmountInCents,
Quantity: s.Quantity,
SubscriptionAddOns: s.SubscriptionAddOns,
}
if now {
sub.Timeframe = "now"
} else {
sub.Timeframe = "renewal"
}
//Hack here need to investigate why the update causes the return to append
s.SubscriptionAddOns = EmbedPlanAddOns{}
return s.r.doUpdateReturn(sub, &s, s.endpoint+"/"+s.UUID)
}
//Reactivate a cancelled account
func (s *Subscription) Reactivate() error {
return s.r.doUpdateReturn(nil, &s, s.endpoint+"/"+s.UUID+"/reactivate")
}
//Cancel an account
func (s *Subscription) Cancel() error {
return s.r.doUpdateReturn(nil, &s, s.endpoint+"/"+s.UUID+"/cancel")
}
//Postpone an accounts renewal datetime
func (s *Subscription) Postpone(renewal time.Time) error {
return s.r.doUpdateReturn(nil, &s, s.endpoint+"/"+s.UUID+"/postpone?next_renewal_date=" + renewal.Format(time.RFC3339))
}
func (s *Subscription) terminate(refund string) error {
return s.r.doUpdateReturn(nil, &s, s.endpoint+"/"+s.UUID+"/terminate?refund="+refund)
}
//Terminate with full refund of the last charge for the current subscription term.
func (s *Subscription) TerminateWithFullRefund() error {
return s.terminate("full")
}
//Terminate and Prorates a refund based on the amount of time remaining in the current bill cycle.
func (s *Subscription) TerminateWithPartialRefund() error {
return s.terminate("partial")
}
//Terminate without refund
func (s *Subscription) Terminate() error {
return s.terminate("none")
}
//A struct to have embedded plan add ons
type EmbedPlanAddOns struct {
PlanAddOns []*EmbedPlanAddOn `xml:"subscription_add_on"`
}
//Either Insert or Update a new AddOn. Can only have One AddOnCode in slice once.
func (e *EmbedPlanAddOns) UpdateAddOns(a EmbedPlanAddOn) {
var found bool
for k,v := range e.PlanAddOns {
if v.AddOnCode == a.AddOnCode {
e.PlanAddOns[k].UnitAmountInCents = a.UnitAmountInCents
e.PlanAddOns[k].Quantity = a.Quantity
found = true
break
}
}
if !found {
newa := EmbedPlanAddOn{
Quantity: a.Quantity,
AddOnCode: a.AddOnCode,
UnitAmountInCents: a.UnitAmountInCents,
}
e.PlanAddOns = append(e.PlanAddOns, &newa)
}
/*if err, finder := e.GetAddOn(a.AddOnCode); err == nil {
finder.AddOnCode = a.AddOnCode
finder.Quantity = a.Quantity
finder.UnitAmountInCents = a.UnitAmountInCents
} else {
newa := EmbedPlanAddOn{
Quantity: a.Quantity,
AddOnCode: a.AddOnCode,
UnitAmountInCents: a.UnitAmountInCents,
}
e.PlanAddOns = append(e.PlanAddOns, newa)
}*/
}
//Delete AddOn by AddOn Code.
func (e *EmbedPlanAddOns) DeleteAddOn(code string) {
if len(e.PlanAddOns) > 0 {
addons := []*EmbedPlanAddOn{}
for _, addon := range e.PlanAddOns {
if addon.AddOnCode != code {
addons = append(addons, addon)
}
}
e.PlanAddOns = addons
}
}
//Return a pointer to an add on for manipulation.
func (e *EmbedPlanAddOns) GetAddOn(code string) (err error, a *EmbedPlanAddOn) {
for k, addon := range e.PlanAddOns {
if code == addon.AddOnCode {
a = e.PlanAddOns[k]
return
}
}
err = errors.New("Code does not exist in current array")
return
}
//An embedded plan add on
type EmbedPlanAddOn struct {
AddOnCode string `xml:"add_on_code,omitempty"`
Quantity int `xml:"quantity,omitempty"`
UnitAmountInCents int `xml:"unit_amount_in_cents,omitempty"`
}