-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathBillingSubscription.ts
More file actions
149 lines (130 loc) · 4.67 KB
/
Copy pathBillingSubscription.ts
File metadata and controls
149 lines (130 loc) · 4.67 KB
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
import type {
BillingCredits,
BillingMoneyAmount,
BillingSubscriptionItemJSON,
BillingSubscriptionItemNextPayment,
BillingSubscriptionItemResource,
BillingSubscriptionItemSeats,
BillingSubscriptionJSON,
BillingSubscriptionNextPayment,
BillingSubscriptionPlanPeriod,
BillingSubscriptionResource,
BillingSubscriptionStatus,
CancelSubscriptionParams,
DeletedObjectJSON,
} from '@clerk/shared/types';
import { unixEpochToDate } from '@/utils/date';
import {
billingCreditsFromJSON,
billingMoneyAmountFromJSON,
billingPerUnitTotalTierFromJSON,
billingSubscriptionItemNextPaymentFromJSON,
billingSubscriptionNextPaymentFromJSON,
} from '../../utils';
import { Billing } from '../modules/billing/namespace';
import { BaseResource, BillingPlan, DeletedObject } from './internal';
export class BillingSubscription extends BaseResource implements BillingSubscriptionResource {
id!: string;
status!: Extract<BillingSubscriptionStatus, 'active' | 'past_due'>;
activeAt!: Date;
createdAt!: Date;
pastDueAt!: Date | null;
updatedAt!: Date | null;
nextPayment?: BillingSubscriptionNextPayment | null;
subscriptionItems!: BillingSubscriptionItemResource[];
eligibleForFreeTrial!: boolean;
payerId!: string;
constructor(data: BillingSubscriptionJSON) {
super();
this.fromJSON(data);
}
protected fromJSON(data: BillingSubscriptionJSON | null): this {
if (!data) {
return this;
}
this.id = data.id;
this.status = data.status;
this.createdAt = unixEpochToDate(data.created_at);
this.updatedAt = data.updated_at ? unixEpochToDate(data.updated_at) : null;
this.activeAt = unixEpochToDate(data.active_at);
this.pastDueAt = data.past_due_at ? unixEpochToDate(data.past_due_at) : null;
this.nextPayment =
data.next_payment === undefined
? undefined
: data.next_payment === null
? null
: billingSubscriptionNextPaymentFromJSON(data.next_payment);
this.subscriptionItems = (data.subscription_items || []).map(item => new BillingSubscriptionItem(item));
this.eligibleForFreeTrial = this.withDefault(data.eligible_for_free_trial, false);
this.payerId = data.payer_id;
return this;
}
}
export class BillingSubscriptionItem extends BaseResource implements BillingSubscriptionItemResource {
id!: string;
plan!: BillingPlan;
planPeriod!: BillingSubscriptionPlanPeriod;
priceId!: string;
status!: BillingSubscriptionStatus;
createdAt!: Date;
periodStart!: Date;
periodEnd!: Date | null;
canceledAt!: Date | null;
pastDueAt!: Date | null;
//TODO(@COMMERCE): Why can this be undefined ?
amount?: BillingMoneyAmount;
credit?: {
amount: BillingMoneyAmount;
};
seats?: BillingSubscriptionItemSeats;
credits?: BillingCredits;
nextPayment?: BillingSubscriptionItemNextPayment | null;
isFreeTrial!: boolean;
constructor(data: BillingSubscriptionItemJSON) {
super();
this.fromJSON(data);
}
protected fromJSON(data: BillingSubscriptionItemJSON | null): this {
if (!data) {
return this;
}
this.id = data.id;
this.plan = new BillingPlan(data.plan);
this.planPeriod = data.plan_period;
this.priceId = data.price_id;
this.status = data.status;
this.createdAt = unixEpochToDate(data.created_at);
this.pastDueAt = data.past_due_at ? unixEpochToDate(data.past_due_at) : null;
this.periodStart = unixEpochToDate(data.period_start);
this.periodEnd = data.period_end ? unixEpochToDate(data.period_end) : null;
this.canceledAt = data.canceled_at ? unixEpochToDate(data.canceled_at) : null;
this.amount = data.amount ? billingMoneyAmountFromJSON(data.amount) : undefined;
this.credit =
data.credit && data.credit.amount ? { amount: billingMoneyAmountFromJSON(data.credit.amount) } : undefined;
this.seats = data.seats
? {
quantity: data.seats.quantity,
...(data.seats.tiers ? { tiers: data.seats.tiers.map(billingPerUnitTotalTierFromJSON) } : {}),
}
: undefined;
this.credits = data.credits ? billingCreditsFromJSON(data.credits) : undefined;
this.nextPayment =
data.next_payment === undefined
? undefined
: data.next_payment === null
? null
: billingSubscriptionItemNextPaymentFromJSON(data.next_payment);
this.isFreeTrial = this.withDefault(data.is_free_trial, false);
return this;
}
public async cancel(params: CancelSubscriptionParams) {
const { orgId } = params;
const json = (
await BaseResource._fetch({
path: Billing.path(`/subscription_items/${this.id}`, { orgId }),
method: 'DELETE',
})
)?.response as unknown as DeletedObjectJSON;
return new DeletedObject(json);
}
}