@cocalc/server
Version:
CoCalc server functionality: functions used by either the hub and the next.js server
49 lines (46 loc) • 1.83 kB
JavaScript
;
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
The stripe connection object, which communicates with the remote stripe server.
Configure via the admin panel in account settings of an admin user.
Throws an error if stripe is not configured.
Double checks with database once per minute to see if the keys have changed,
and if so will return new stripe object.
*/
const stripe_1 = __importDefault(require("stripe"));
const settings_1 = require("@cocalc/server/settings");
// See https://stripe.com/docs/api/versioning
const apiVersion = "2020-03-02";
let stripe = undefined;
let key = "";
let last = 0;
async function getConn() {
if (stripe != null && new Date().valueOf() - last <= 1000 * 60) {
return stripe;
}
const { stripe_publishable_key, stripe_secret_key } = await (0, settings_1.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_1.default(stripe_secret_key, {
apiVersion,
});
stripe.publishable_key = stripe_publishable_key;
last = new Date().valueOf();
}
return stripe;
}
exports.default = getConn;
//# sourceMappingURL=connection.js.map