-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.ts
117 lines (109 loc) · 3.52 KB
/
demo.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
import { Checkout, CheckoutOptions, CheckoutPopupOptions } from '.';
import './style.css';
const fsCheckout = new Checkout(
{
product_id: Number.parseInt(
(import.meta.env.VITE_PRODUCT_ID as string) ?? '0',
10
),
public_key: import.meta.env.VITE_PUBLIC_KEY as string,
sandbox: {
ctx: import.meta.env.VITE_SANDBOX_CTX as string,
token: import.meta.env.VITE_SANDBOX_TOKEN as string,
},
language: 'auto-beta',
user_token: import.meta.env.VITE_USER_TOKEN as string,
},
true,
import.meta.env.VITE_CHECKOUT_BASE_URL ?? undefined
);
document.addEventListener('DOMContentLoaded', () => {
function getLicensesAndFrequency() {
let licenses = 1;
const siteSelect = document.querySelector('#site');
if (siteSelect) {
licenses = Number.parseInt(
(siteSelect as HTMLSelectElement).value,
10
);
}
let billing_cycle: CheckoutOptions['billing_cycle'] = 'annual';
const freqSelect = document.querySelector('#frequency');
if (freqSelect) {
billing_cycle = (freqSelect as HTMLSelectElement).value as any;
}
return { licenses, billing_cycle };
}
function getEventLoggers(): Pick<
CheckoutPopupOptions,
| 'cancel'
| 'purchaseCompleted'
| 'success'
| 'track'
| 'afterOpen'
| 'afterClose'
| 'onExitIntent'
> {
const log = (event: string, ...args: any[]) => {
console.log(
`%c FSCheckout %c :: %c ${event} %c`,
'background: #C62828; color: white;',
'background: transparent;',
'background: #283593; color: white;',
'background: transparent;'
);
if (args.length) {
console.log(...args);
}
};
return {
cancel() {
log('cancel');
},
purchaseCompleted(data) {
log('purchaseCompleted', data);
},
success(data) {
log('success', data);
},
track(event, data) {
log('track', event, data);
},
afterOpen() {
log('afterOpen');
},
afterClose() {
log('afterClose');
},
onExitIntent() {
log('exitIntent');
},
};
}
const plans = {
'#plan-1': import.meta.env.VITE_PLAN_ONE as string,
'#plan-2': import.meta.env.VITE_PLAN_TWO as string,
'#plan-3': import.meta.env.VITE_PLAN_THREE as string,
};
Object.entries(plans).forEach(([selector, planId]) => {
if (!planId) {
document.querySelector(selector)?.remove();
} else {
document.querySelector(selector)?.addEventListener('click', (e) => {
e.preventDefault();
fsCheckout.open({
plan_id: Number.parseInt(planId, 10),
...getLicensesAndFrequency(),
...getEventLoggers(),
});
});
}
});
console.log(
'%cCheckout API available as %cfsCheckout%c global variable',
'font-size: 20px; ',
'font-size: 20px; background-color: #B71C1C; color: white;',
'font-size: 20px; background-color: transparent; '
);
(window as any).fsCheckout = fsCheckout;
});