CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/billing/stripe.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { redux } from "../app-framework";
7
import { BillingStore } from "./store";
8
9
declare global {
10
interface Window {
11
Stripe: any;
12
}
13
}
14
15
declare var $: any;
16
17
export interface Stripe {
18
elements: Function;
19
createToken: Function;
20
}
21
22
export interface StripeCard {
23
mount: Function;
24
}
25
26
let stripe: Stripe | undefined = undefined;
27
export async function loadStripe(): Promise<Stripe> {
28
if (stripe != null) return stripe;
29
try {
30
await $.getScript("https://js.stripe.com/v3/");
31
} catch (err) {
32
throw Error(
33
`Unable to load Stripe payment support; make sure your browser is not blocking https://js.stripe.com/v3/ -- ${err}`
34
);
35
}
36
const store: BillingStore = redux.getStore("billing");
37
if (store == null) {
38
throw Error("billing store not initialized");
39
}
40
const key: string | undefined = store.get("stripe_publishable_key");
41
if (!key) {
42
throw Error("stripe not configured -- publishable key not known");
43
}
44
return (stripe = window.Stripe(key));
45
}
46
47