-
Notifications
You must be signed in to change notification settings - Fork 6
/
subscriptions_test.go
319 lines (287 loc) · 7.29 KB
/
subscriptions_test.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package invoiced
import (
"encoding/json"
"fmt"
"testing"
)
func TestUnMarshalSubscriptionObject(t *testing.T) {
s := `{
"id": 595,
"customer": 15444,
"amount": 10.99,
"plan": "starter",
"cycles": null,
"quantity": 1,
"start_date": 1420391704,
"period_start": 1446657304,
"period_end": 1449249304,
"status": "active",
"addons": [
{
"id": 3,
"plan": "ipad-license",
"quantity": 11,
"created_at": 1420391704
}
],
"discounts": [],
"taxes": [],
"url": "https://dundermifflin.invoiced.com/subscriptions/o2mAd2wWVfYy16XZto7xHwXX",
"created_at": 1420391704,
"metadata": {},
"prorate": true,
"contract_renewal_mode": "manual",
"ship_to": {
"address1": "123 Main St",
"address2": "Ste 100",
"attention_to": "Regina Smith",
"city": "Austin",
"country": "US",
"name": "Company Name",
"postal_code": "78730",
"state": "TX"
}
}`
so := new(Subscription)
err := json.Unmarshal([]byte(s), so)
if err != nil {
t.Fatal(err)
}
fmt.Println("Subscription plan -> ", so.Plan)
if so.Id != 595 {
t.Fatal("Subscription has incorrect id")
}
if so.Customer != 15444 {
t.Fatal("Subscription has incorrect type")
}
fmt.Println("so.plan ", so.Plan)
if so.Plan != "starter" {
t.Fatal("Subscription has incorrect plan2 -> " + so.Plan)
}
if so.Amount != 10.99 {
t.Fatal("Subscription has incorrect amount")
}
if so.Quantity != 1 {
t.Fatal("Subscription has incorrect quantity")
}
if so.StartDate != 1420391704 {
t.Fatal("Subscription has incorrect quantity")
}
if so.PeriodStart != 1446657304 {
t.Fatal("Subscription has incorrect periodstart")
}
if so.PeriodEnd != 1449249304 {
t.Fatal("Subscription has incorrect periodstart")
}
if so.Status != "active" {
t.Fatal("Subscription has incorrect status")
}
if so.Addons[0].Id != 3 {
t.Fatal("Subscription Addon 0 has incorrect status")
}
if so.Addons[0].Plan != "ipad-license" {
t.Fatal("Subscription Addon Client 0 has incorrect value")
}
if so.Addons[0].Quantity != 11 {
t.Fatal("Subscription Addon Quantity 0 has incorrect status")
}
if so.Addons[0].CreatedAt != 1420391704 {
t.Fatal("Quantity Addon CreatedAT has incorrect status")
}
if so.Url != "https://dundermifflin.invoiced.com/subscriptions/o2mAd2wWVfYy16XZto7xHwXX" {
t.Fatal("Url is incorrect")
}
if so.Prorate != true {
t.Fatal("Subscription has incorrect Prorate status")
}
if so.ContractRenewalMode != "manual" {
t.Fatal("Subscription Ahas incorrect ContractRenewalMode status")
}
if so.CreatedAt != 1420391704 {
t.Fatal("Subscription CreatedAt is incorrect")
}
}
func TestUnMarshalSubscriptionsPreview(t *testing.T) {
data := `{
"first_invoice": {
"attempt_count": null,
"autopay": null,
"balance": 0,
"chase": false,
"closed": false,
"created_at": null,
"csv_url": null,
"currency": "usd",
"customer": -1,
"date": 1571410119,
"discounts": [],
"draft": true,
"due_date": null,
"id": false,
"items": [
{
"amount": 49,
"catalog_item": null,
"created_at": null,
"description": "",
"discountable": true,
"discounts": [],
"id": false,
"metadata": {},
"name": "Starter",
"object": "line_item",
"plan": "starter",
"quantity": 1,
"taxable": true,
"taxes": [],
"type": "plan",
"unit_cost": 49
}
],
"metadata": {},
"name": "Starter",
"needs_attention": null,
"next_chase_on": null,
"next_payment_attempt": null,
"notes": null,
"number": null,
"object": "invoice",
"paid": false,
"payment_plan": null,
"payment_source": null,
"payment_terms": null,
"payment_url": null,
"pdf_url": null,
"purchase_order": null,
"ship_to": null,
"shipping": [],
"status": "draft",
"subscription": false,
"subtotal": 49,
"taxes": [],
"total": 49,
"url": null
},
"mrr": 49,
"recurring_total": 50
}`
so := new(SubscriptionPreview)
err := json.Unmarshal([]byte(data), so)
if err != nil {
t.Fatal(err)
}
if so.MRR != 49 {
t.Fatal("MRR does not match")
}
if so.FirstInvoice == nil {
t.Fatal("First Invoice should not be fatal")
}
if so.RecurringTotal != 50 {
t.Fatal("Recurring total should not ")
}
}
func TestSubscriptionPlanUnmarshall(t *testing.T) {
s := `{
"bill_in": "advance",
"bill_in_advance_days": 0,
"cancel_at_period_end": false,
"canceled_at": null,
"contract_period_end": 1627448399,
"contract_period_start": 1624856400,
"contract_renewal_cycles": null,
"contract_renewal_mode": "auto",
"created_at": 1624934799,
"customer": {
"ach_gateway_id": null,
"address1": null,
"address2": null,
"attention_to": null,
"autopay": false,
"autopay_delay_days": -1,
"avalara_entity_use_code": null,
"avalara_exemption_number": null,
"bill_to_parent": false,
"cc_gateway_id": null,
"chase": true,
"chasing_cadence": null,
"city": null,
"consolidated": false,
"country": "US",
"created_at": 1624930142,
"credit_hold": false,
"credit_limit": null,
"currency": "usd",
"email": null,
"id": 2321739,
"language": null,
"name": "Parag",
"next_chase_step": null,
"notes": null,
"number": "acme00209",
"owner": 831,
"parent_customer": null,
"payment_terms": "NET 14",
"phone": null,
"postal_code": null,
"state": null,
"tax_id": null,
"taxable": true,
"taxes": [],
"type": "company",
"object": "customer",
"statement_pdf_url": "https://tesla.sandbox.invoiced.com/statements/ReOax2A5W6bIt8V4paAmGvEn/pdf",
"sign_up_url": null,
"payment_source": null,
"sign_up_page": null,
"metadata": {}
},
"cycles": 1,
"description": null,
"id": 62241,
"mrr": 80900,
"paused": false,
"period_end": 1627448399,
"period_start": 1624856400,
"plan": {
"amount": 80900,
"catalog_item": null,
"created_at": 1624934793,
"currency": "usd",
"description": null,
"id": "model-z",
"interval": "month",
"interval_count": 1,
"name": "Model Z",
"notes": null,
"pricing_mode": "per_unit",
"quantity_type": "constant",
"tiers": null,
"object": "plan",
"metadata": {}
},
"quantity": 1,
"recurring_total": 80900,
"renewed_last": 1624856400,
"renews_next": 1627448400,
"snap_to_nth_day": null,
"start_date": 1624856400,
"status": "active",
"taxes": [],
"object": "subscription",
"url": "https://tesla.sandbox.invoiced.com/subscriptions/pE9pBoU0HmF6dyAXxyAYstOk",
"approval": null,
"payment_source": null,
"ship_to": null,
"metadata": {},
"addons": [],
"discounts": []
}`
so := new(Subscription)
err := json.Unmarshal([]byte(s), so)
if err != nil {
t.Fatal(err)
}
if so.Plan != "model-z" {
t.Fatal("Client id is incorrect")
}
}