import Stripe from "stripe" ; import { getServerSettings } from "@cocalc/server/settings" ; const apiVersion = "2020-03-02" ; interface StripeWithPublishableKey extends Stripe { publishable_key : string ; } let stripe = undefined ; let key = "" ; let last = 0 ; export default async function getConn ( ) { if ( stripe != null && new Date ( ) . valueOf ( ) - last <= 1000 * 60 ) { return stripe ; } const { stripe_publishable_key , stripe_secret_key } = await getServerSettings ( ) ; if ( ! stripe_publishable_key ) { throw Error ( "stripe publishable key is not set -- billing functionality not available" ) ; } if ( ! stripe_secret_key ) { throw Error ( "stripe secret key is not set -- billing functionality not available" ) ; } if ( stripe == null || key != stripe_publishable_key + stripe_secret_key ) { key = stripe_publishable_key + stripe_secret_key ; stripe = new Stripe ( stripe_secret_key , { apiVersion , } ) as StripeWithPublishableKey ; stripe . publishable_key = stripe_publishable_key ; last = new Date ( ) . valueOf ( ) ; } return stripe ; }