This repository has been archived by the owner on Feb 27, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbilling.ts
208 lines (152 loc) Β· 6.3 KB
/
billing.ts
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
import {
checkForValidSession,
commonInitializer,
commonRequestHeaders,
dateDiffInDays,
showNotification,
} from './utils.ts';
import LocalData from './local-data.ts';
document.addEventListener('app-loaded', async () => {
const user = await checkForValidSession();
const subscriptionInfo = document.getElementById('subscription-info') as HTMLDivElement;
const deleteAccountButton = document.getElementById('delete-account') as HTMLButtonElement;
function updatePayment(event: Event) {
event.preventDefault();
event.stopPropagation();
const updateUrl = user?.customerPortalUrl;
if (!updateUrl) {
showNotification('You need to reach out in order to update your subscription, sorry!', 'error');
return;
}
window.open(updateUrl, '_blank');
}
function cancelSubscription(event: Event) {
event.preventDefault();
event.stopPropagation();
const cancelUrl = user?.customerPortalUrl;
if (!cancelUrl) {
showNotification('You need to reach out in order to cancel your subscription, sorry!', 'error');
return;
}
window.open(cancelUrl, '_blank');
}
function resumeSubscription(event: Event) {
event.preventDefault();
event.stopPropagation();
const updateUrl = user?.customerPortalUrl;
if (!updateUrl) {
showNotification('You need to reach out in order to resume your subscription, sorry!', 'error');
return;
}
window.open(updateUrl, '_blank');
}
async function deleteAccount(event: Event) {
const { Swal } = window;
event.preventDefault();
event.stopPropagation();
const { isConfirmed } = await Swal.fire({
title: 'Are you sure?',
text: "You won't be able to recover your data!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: 'red',
confirmButtonText: 'Yes, delete it!',
});
if (isConfirmed) {
window.app.showLoading();
const headers = commonRequestHeaders;
const session = LocalData.get('session')!;
const body: { user_id: string; session_id: string; code?: string } = {
user_id: session.userId,
session_id: session.sessionId,
};
await fetch('/api/user', { method: 'DELETE', headers, body: JSON.stringify(body) });
window.app.hideLoading();
const { value: code } = await Swal.fire({
template: '#verification-code-modal',
focusConfirm: false,
allowEscapeKey: true,
preConfirm: () => {
const codeValue = (document.getElementById('verification-code-input') as HTMLInputElement).value;
if (!codeValue) {
showNotification('You need to submit a code!', 'error');
return false;
}
return codeValue;
},
willOpen: () => {
(document.getElementById('verification-code-input') as HTMLInputElement).value = '';
},
});
window.app.showLoading();
body.code = code;
await fetch('/api/user', { method: 'DELETE', headers, body: JSON.stringify(body) });
LocalData.clear();
window.location.reload();
}
}
function getValidSubscriptionHtmlElement(
{ isSubscriptionCanceled, isSubscriptionMonthly }: {
isSubscriptionCanceled: boolean;
isSubscriptionMonthly: boolean;
},
) {
const template = document.getElementById('valid-subscription') as HTMLTemplateElement;
const clonedElement = (template.content.firstElementChild as HTMLDivElement).cloneNode(true) as HTMLDivElement;
const paymentTextElement = clonedElement.querySelector('.subscription-value') as HTMLSpanElement;
paymentTextElement.textContent = isSubscriptionMonthly ? 'monthly' : 'yearly';
const notCanceledElement = clonedElement.querySelector('#subscription-is-not-canceled') as HTMLDivElement;
const canceledElement = clonedElement.querySelector('#subscription-is-canceled') as HTMLDivElement;
if (isSubscriptionCanceled) {
notCanceledElement.classList.add('hidden');
canceledElement.classList.remove('hidden');
}
return clonedElement;
}
function getInvalidSubscriptionHtmlElement() {
const template = document.getElementById('invalid-subscription') as HTMLTemplateElement;
const clonedElement = (template.content.firstElementChild as HTMLDivElement).cloneNode(true) as HTMLDivElement;
return clonedElement;
}
function getTrialSubscriptionHtmlElement() {
const template = document.getElementById('trial-subscription') as HTMLTemplateElement;
const clonedElement = (template.content.firstElementChild as HTMLDivElement).cloneNode(true) as HTMLDivElement;
return clonedElement;
}
function updateUI() {
const isSubscriptionValid = user?.status === 'active';
let trialDaysLeft = 30;
if (user?.subscription.expires_at) {
const trialExpirationDate = new Date(user?.subscription.expires_at);
trialDaysLeft = dateDiffInDays(new Date(), trialExpirationDate);
}
const isTrialing = user?.status === 'trial' && trialDaysLeft > 0;
const isSubscriptionCanceled = user?.status === 'inactive';
const isSubscriptionMonthly = Boolean(user?.subscription.isMonthly);
subscriptionInfo.replaceChildren();
if (isSubscriptionValid) {
const subscriptionElement = getValidSubscriptionHtmlElement({ isSubscriptionCanceled, isSubscriptionMonthly });
subscriptionInfo.appendChild(subscriptionElement);
const updateButton = document.getElementById('update-payment') as HTMLButtonElement;
const cancelButton = document.getElementById('cancel-subscription') as HTMLButtonElement;
const resumeButton = document.getElementById('resume-subscription') as HTMLButtonElement;
updateButton.addEventListener('click', updatePayment);
cancelButton.addEventListener('click', cancelSubscription);
resumeButton.addEventListener('click', resumeSubscription);
} else if (isTrialing) {
const subscriptionElement = getTrialSubscriptionHtmlElement();
subscriptionInfo.appendChild(subscriptionElement);
} else {
const subscriptionElement = getInvalidSubscriptionHtmlElement();
subscriptionInfo.appendChild(subscriptionElement);
}
}
function initializePage() {
updateUI();
commonInitializer();
}
if (window.app.isLoggedIn) {
initializePage();
}
deleteAccountButton.addEventListener('click', deleteAccount);
});