exports.id = 6911;
exports.ids = [6911];
exports.modules = {

/***/ 797877:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {

"use strict";

/*
Similar to the public profile, but also get additional information
that shouldn't be made available to anybody who knows this account_id.

Only call this for the account_id of the signed in user.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
var pool_1 = __importDefault(__webpack_require__(541808));
function getPrivateProfile(account_id, noCache) {
    var _a, _b, _c, _d, _e;
    if (noCache === void 0) { noCache = false; }
    return __awaiter(this, void 0, void 0, function () {
        var pool, rows, is_admin, is_anonymous;
        return __generator(this, function (_f) {
            switch (_f.label) {
                case 0:
                    pool = (0, pool_1.default)(noCache ? undefined : "medium");
                    return [4 /*yield*/, pool.query("SELECT first_name, last_name, profile, name, groups, email_address, passports FROM accounts WHERE account_id=$1", [account_id])];
                case 1:
                    rows = (_f.sent()).rows;
                    if (rows.length == 0) {
                        throw Error("no account with id ".concat(account_id));
                    }
                    is_admin = !!((_a = rows[0].groups) === null || _a === void 0 ? void 0 : _a.includes("admin"));
                    is_anonymous = !rows[0].email_address && !rows[0].passports;
                    return [2 /*return*/, {
                            account_id: account_id,
                            first_name: (_b = rows[0].first_name) !== null && _b !== void 0 ? _b : "Anonymous",
                            last_name: (_c = rows[0].last_name) !== null && _c !== void 0 ? _c : "User",
                            image: (_d = rows[0].profile) === null || _d === void 0 ? void 0 : _d.image,
                            color: (_e = rows[0].profile) === null || _e === void 0 ? void 0 : _e.color,
                            name: rows[0].name,
                            is_admin: is_admin,
                            is_anonymous: is_anonymous,
                        }];
            }
        });
    });
}
exports["default"] = getPrivateProfile;
//# sourceMappingURL=private.js.map

/***/ }),

/***/ 840336:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getServerSettings = void 0;
var server_settings_1 = __webpack_require__(526177);
Object.defineProperty(exports, "getServerSettings", ({ enumerable: true, get: function () { return server_settings_1.getServerSettings; } }));
//# sourceMappingURL=index.js.map

/***/ }),

/***/ 946911:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {

"use strict";

/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.StripeClient = exports.Stripe = void 0;
var hof_1 = __webpack_require__(807108);
var awaiting_1 = __webpack_require__(862567);
var async_utils_1 = __webpack_require__(644181);
var message = __importStar(__webpack_require__(866976));
var upgrades_1 = __webpack_require__(874999);
var name_1 = __importDefault(__webpack_require__(676941));
var database_1 = __webpack_require__(207007);
var stripe_1 = __importDefault(__webpack_require__(476229));
exports.Stripe = stripe_1.default;
var private_1 = __importDefault(__webpack_require__(797877));
var connection_1 = __importDefault(__webpack_require__(578608));
var sales_tax_1 = __webpack_require__(515767);
var logger_1 = __importDefault(__webpack_require__(227554));
var logger = (0, logger_1.default)("stripe-client");
function get_string_field(mesg, name) {
    if (mesg == null)
        throw Error("invalid message; must not be null");
    var x = mesg[name];
    if (typeof x != "string")
        throw Error("mesg[".concat(name, "] must be a string"));
    return x;
}
function get_nonnull_field(mesg, name) {
    if (mesg == null)
        throw Error("invalid message; must not be null");
    var x = mesg[name];
    if (x == null)
        throw Error("mesg[".concat(name, "] must be defined"));
    return x;
}
var StripeClient = /** @class */ (function () {
    function StripeClient(client) {
        if (!client.account_id) {
            throw Error("account_id must be specified");
        }
        if (!client.database) {
            // TODO: Importing this at the top level doesn't work with
            // next.js right now for some reason.   I'm sure this problem
            // will vanish when @cocalc/database melts away or is rewritten
            // in typescript...
            client.database = (0, database_1.db)();
        }
        // Set defaults for some attributes of client, in case not specified
        if (client.dbg == null) {
            client.dbg = function (f) {
                return function () {
                    var args = [];
                    for (var _i = 0; _i < arguments.length; _i++) {
                        args[_i] = arguments[_i];
                    }
                    return logger.debug.apply(logger, __spreadArray(["Stripe(account_id=".concat(client.account_id, ")"), f], __read(args), false));
                };
            };
        }
        if (client.push_to_client == null) {
            client.push_to_client = function () { }; // no op
        }
        if (client.error_to_client == null) {
            client.error_to_client = function () { }; // no op
        }
        if (client.assert_user_is_in_group == null) {
            client.assert_user_is_in_group = function () { }; // no op
        }
        this.client = client;
        this.get_customer_id = (0, hof_1.reuseInFlight)(this.get_customer_id.bind(this));
    }
    StripeClient.prototype.dbg = function (f) {
        return this.client.dbg("stripe.".concat(f));
    };
    // Returns the stripe customer id for this account from our database,
    // or undefined if there is no known stripe customer id.
    // Throws an error if something goes wrong.
    // If called multiple times simultaneously, only does one DB query.
    StripeClient.prototype.get_customer_id = function () {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, account_id, stripe_customer_id;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("get_customer_id");
                        dbg();
                        if (this.stripe_customer_id != null) {
                            dbg("using cached this.stripe_customer_id");
                            return [2 /*return*/, this.stripe_customer_id];
                        }
                        account_id = this.client.account_id;
                        if (account_id == null) {
                            throw Error("You must be signed in to use billing related functions.");
                        }
                        dbg("getting stripe_customer_id from database...");
                        return [4 /*yield*/, (0, async_utils_1.callback2)(this.client.database.get_stripe_customer_id, { account_id: account_id })];
                    case 1:
                        stripe_customer_id = _a.sent();
                        if (stripe_customer_id != null) {
                            // cache it, since it won't change.
                            this.stripe_customer_id = stripe_customer_id;
                        }
                        return [2 /*return*/, stripe_customer_id];
                }
            });
        });
    };
    // Raise an exception if user is not yet registered with stripe.
    StripeClient.prototype.need_customer_id = function () {
        return __awaiter(this, void 0, void 0, function () {
            var customer_id;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.dbg("need_customer_id")();
                        return [4 /*yield*/, this.get_customer_id()];
                    case 1:
                        customer_id = _a.sent();
                        if (customer_id == null) {
                            throw Error("stripe customer not defined");
                        }
                        return [2 /*return*/, customer_id];
                }
            });
        });
    };
    StripeClient.prototype.stripe_api_pager_options = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = {};
                        return [4 /*yield*/, this.need_customer_id()];
                    case 1: return [2 /*return*/, (_a.customer = _b.sent(),
                            _a.limit = mesg.limit,
                            _a.ending_before = mesg.ending_before,
                            _a.starting_after = mesg.starting_after,
                            _a)];
                }
            });
        });
    };
    StripeClient.prototype.get_customer = function (customer_id) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("get_customer");
                        if (!(customer_id == null)) return [3 /*break*/, 2];
                        dbg("getting customer id");
                        return [4 /*yield*/, this.need_customer_id()];
                    case 1:
                        customer_id = _a.sent();
                        _a.label = 2;
                    case 2:
                        dbg("now getting stripe customer object");
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 3: return [4 /*yield*/, (_a.sent()).customers.retrieve(customer_id)];
                    case 4: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    StripeClient.prototype.handle_mesg = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var f, resp, err_1, error;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _a.trys.push([0, 4, , 5]);
                        if (mesg.event.slice(0, 7) != "stripe_") {
                            throw Error("mesg event must start with stripe_");
                        }
                        f = this["mesg_".concat(mesg.event.slice(7))];
                        if (!(f == null)) return [3 /*break*/, 1];
                        throw Error("no such message type ".concat(mesg.event));
                    case 1: return [4 /*yield*/, f.bind(this)(mesg)];
                    case 2:
                        resp = _a.sent();
                        if (resp == null) {
                            resp = {};
                        }
                        resp.id = mesg.id;
                        this.client.push_to_client(resp);
                        _a.label = 3;
                    case 3: return [3 /*break*/, 5];
                    case 4:
                        err_1 = _a.sent();
                        error = void 0;
                        if (err_1.stack != null) {
                            error = err_1.stack.split("\n")[0];
                        }
                        else {
                            error = "".concat(err_1);
                        }
                        this.dbg("handle_mesg")("Error", error, err_1.stack);
                        this.client.error_to_client({ id: mesg.id, error: error });
                        return [3 /*break*/, 5];
                    case 5: return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.mesg_get_customer = function (_mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, _a, _b;
            var _c;
            return __generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        dbg = this.dbg("mesg_get_customer");
                        dbg("get information from stripe: subscriptions, payment methods, etc.");
                        _b = (_a = message).stripe_customer;
                        _c = {};
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 1:
                        _c.stripe_publishable_key = (_d.sent()).publishable_key;
                        return [4 /*yield*/, this.update_database()];
                    case 2: 
                    // note -- we explicitly put the "publishable_key" property there...
                    return [2 /*return*/, _b.apply(_a, [(_c.customer = _d.sent(),
                                _c)])];
                }
            });
        });
    };
    StripeClient.prototype.mesg_create_source = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, token, customer_id;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("mesg_create_source");
                        dbg("create a payment method (credit card) in stripe for this user");
                        token = get_string_field(mesg, "token");
                        dbg("looking up customer");
                        return [4 /*yield*/, this.get_customer_id()];
                    case 1:
                        customer_id = _a.sent();
                        if (!(customer_id == null)) return [3 /*break*/, 3];
                        return [4 /*yield*/, this.create_new_stripe_customer_from_card_token(token)];
                    case 2:
                        _a.sent();
                        return [3 /*break*/, 5];
                    case 3: return [4 /*yield*/, this.add_card_to_existing_stripe_customer(token)];
                    case 4:
                        _a.sent();
                        _a.label = 5;
                    case 5: return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.create_new_stripe_customer_from_card_token = function (token) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, is_anonymous, r, email, description, x, customer_id;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("create_new_stripe_customer_from_card_token");
                        dbg("create new stripe customer from card token");
                        return [4 /*yield*/, (0, private_1.default)(this.client.account_id)];
                    case 1:
                        is_anonymous = (_a.sent()).is_anonymous;
                        if (is_anonymous) {
                            throw Error("anonymous users are not allowed to create a stripe customer");
                        }
                        dbg("get identifying info about user");
                        return [4 /*yield*/, (0, async_utils_1.callback2)(this.client.database.get_account, {
                                columns: ["email_address", "first_name", "last_name"],
                                account_id: this.client.account_id,
                            })];
                    case 2:
                        r = _a.sent();
                        email = r.email_address;
                        description = (0, name_1.default)(r.first_name, r.last_name);
                        dbg("they are ".concat(description, " with email ").concat(email));
                        dbg("creating stripe customer");
                        x = {
                            source: token,
                            description: description,
                            name: description,
                            email: email,
                            metadata: {
                                account_id: this.client.account_id,
                            },
                        };
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 3: return [4 /*yield*/, (_a.sent()).customers.create(x)];
                    case 4:
                        customer_id = (_a.sent())
                            .id;
                        dbg("success; now save customer_id to database");
                        return [4 /*yield*/, (0, async_utils_1.callback2)(this.client.database.set_stripe_customer_id, {
                                account_id: this.client.account_id,
                                customer_id: customer_id,
                            })];
                    case 5:
                        _a.sent();
                        return [4 /*yield*/, this.update_database()];
                    case 6:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.add_card_to_existing_stripe_customer = function (token) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, customer_id;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("add_card_to_existing_stripe_customer");
                        dbg("add card to existing stripe customer");
                        return [4 /*yield*/, this.need_customer_id()];
                    case 1:
                        customer_id = _a.sent();
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 2: return [4 /*yield*/, (_a.sent()).customers.createSource(customer_id, { source: token })];
                    case 3:
                        _a.sent();
                        return [4 /*yield*/, this.update_database()];
                    case 4:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.mesg_delete_source = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, card_id, customer_id;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("mesg_delete_source");
                        dbg("delete a payment method for this user");
                        card_id = get_string_field(mesg, "card_id");
                        return [4 /*yield*/, this.get_customer_id()];
                    case 1:
                        customer_id = _a.sent();
                        if (customer_id == null)
                            throw Error("no customer information so can't delete source");
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 2: return [4 /*yield*/, (_a.sent()).customers.deleteSource(customer_id, card_id)];
                    case 3:
                        _a.sent();
                        return [4 /*yield*/, this.update_database()];
                    case 4:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.mesg_set_default_source = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, card_id, customer_id;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("mesg_set_default_source");
                        dbg("set a payment method for this user to be the default");
                        card_id = get_string_field(mesg, "card_id");
                        return [4 /*yield*/, this.need_customer_id()];
                    case 1:
                        customer_id = _a.sent();
                        dbg("now setting the default source in stripe");
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 2: return [4 /*yield*/, (_a.sent()).customers.update(customer_id, {
                            default_source: card_id,
                        })];
                    case 3:
                        _a.sent();
                        return [4 /*yield*/, this.update_database()];
                    case 4:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    // update_database queries stripe for customer record, stores
    // it in the database, and  returns the new customer record
    // if it exists
    StripeClient.prototype.update_database = function () {
        return __awaiter(this, void 0, void 0, function () {
            var customer_id, _a, _b;
            var _c;
            return __generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        this.dbg("update_database")();
                        return [4 /*yield*/, this.get_customer_id()];
                    case 1:
                        customer_id = _d.sent();
                        if (customer_id == null)
                            return [2 /*return*/];
                        _a = async_utils_1.callback2;
                        _b = [this.client.database.stripe_update_customer];
                        _c = {
                            account_id: this.client.account_id
                        };
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 2: return [4 /*yield*/, _a.apply(void 0, _b.concat([(_c.stripe = _d.sent(),
                                _c.customer_id = customer_id,
                                _c)]))];
                    case 3: return [2 /*return*/, _d.sent()];
                }
            });
        });
    };
    StripeClient.prototype.mesg_update_source = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, card_id, info;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("mesg_update_source");
                        dbg("modify a payment method");
                        card_id = get_string_field(mesg, "card_id");
                        info = get_nonnull_field(mesg, "info");
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 1: return [4 /*yield*/, (_a.sent()).sources.update(card_id, info)];
                    case 2:
                        _a.sent();
                        return [4 /*yield*/, this.update_database()];
                    case 3:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.sales_tax = function (customer_id) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, (0, sales_tax_1.stripe_sales_tax)(customer_id, this.dbg("sales_tax"))];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
    StripeClient.prototype.mesg_create_subscription = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, plan, schema, customer_id, quantity, tax_percent, options, _a, coupon, coupon_history;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        dbg = this.dbg("mesg_create_subscription");
                        dbg("create a subscription for this user, using some billing method");
                        plan = get_string_field(mesg, "plan");
                        schema = (__webpack_require__(47896).PROJECT_UPGRADES.subscription)[plan.split("-")[0]];
                        if (schema == null)
                            throw Error("unknown plan -- '".concat(plan, "'"));
                        return [4 /*yield*/, this.need_customer_id()];
                    case 1:
                        customer_id = _b.sent();
                        quantity = mesg.quantity ? mesg.quantity : 1;
                        dbg("determine applicable tax");
                        return [4 /*yield*/, this.sales_tax(customer_id)];
                    case 2:
                        tax_percent = _b.sent();
                        options = {
                            customer: customer_id,
                            payment_behavior: "error_if_incomplete",
                            items: [{ quantity: quantity, plan: plan }],
                            coupon: mesg.coupon_id,
                            cancel_at_period_end: schema.cancel_at_period_end,
                            tax_percent: tax_percent
                                ? Math.round(tax_percent * 100 * 100) / 100
                                : undefined,
                        };
                        dbg("add customer subscription to stripe");
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 3: return [4 /*yield*/, (_b.sent()).subscriptions.create(options)];
                    case 4:
                        _b.sent();
                        dbg("added subscription; now save info in our database about it...");
                        return [4 /*yield*/, this.update_database()];
                    case 5:
                        _b.sent();
                        if (!(options.coupon != null)) return [3 /*break*/, 8];
                        dbg("add coupon to customer history");
                        return [4 /*yield*/, this.validate_coupon(options.coupon)];
                    case 6:
                        _a = _b.sent(), coupon = _a.coupon, coupon_history = _a.coupon_history;
                        // SECURITY NOTE: incrementing a counter... subject to attack?
                        // I.e., use a coupon more times than should be able to?
                        // NOT a big worry since we never issue or use coupons anyways...
                        coupon_history[coupon.id] += 1;
                        return [4 /*yield*/, (0, async_utils_1.callback2)(this.client.database.update_coupon_history, {
                                account_id: this.client.account_id,
                                coupon_history: coupon_history,
                            })];
                    case 7:
                        _b.sent();
                        _b.label = 8;
                    case 8: return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.mesg_cancel_subscription = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, subscription_id;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("mesg_cancel_subscription");
                        dbg("cancel a subscription for this user");
                        subscription_id = get_string_field(mesg, "subscription_id");
                        // TODO/SECURITY: We should check that this subscription actually
                        // belongs to this user.  As it is, they could be cancelling somebody
                        // else's subscription!
                        dbg("cancel the subscription at stripe");
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 1: return [4 /*yield*/, (_a.sent()).subscriptions.update(subscription_id, {
                            cancel_at_period_end: mesg.at_period_end,
                        })];
                    case 2:
                        _a.sent();
                        return [4 /*yield*/, this.update_database()];
                    case 3:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.mesg_update_subscription = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, subscription_id, changes, _a, coupon, coupon_history;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        dbg = this.dbg("mesg_update_subscription");
                        dbg("edit a subscription for this user");
                        subscription_id = get_string_field(mesg, "subscription_id");
                        dbg("Update the subscription.");
                        changes = {
                            quantity: mesg.quantity,
                            plan: mesg.plan,
                            coupon: mesg.coupon_id,
                        };
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 1: return [4 /*yield*/, (_b.sent()).subscriptions.update(subscription_id, changes)];
                    case 2:
                        _b.sent();
                        return [4 /*yield*/, this.update_database()];
                    case 3:
                        _b.sent();
                        if (!(mesg.coupon_id != null)) return [3 /*break*/, 6];
                        return [4 /*yield*/, this.validate_coupon(mesg.coupon_id)];
                    case 4:
                        _a = _b.sent(), coupon = _a.coupon, coupon_history = _a.coupon_history;
                        coupon_history[coupon.id] += 1;
                        return [4 /*yield*/, (0, async_utils_1.callback2)(this.client.database.update_coupon_history, {
                                account_id: this.client.account_id,
                                coupon_history: coupon_history,
                            })];
                    case 5:
                        _b.sent();
                        _b.label = 6;
                    case 6: return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.mesg_get_subscriptions = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, options, subscriptions;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("mesg_get_subscriptions");
                        dbg("get a list of all the subscriptions that this customer has");
                        return [4 /*yield*/, this.stripe_api_pager_options(mesg)];
                    case 1:
                        options = _a.sent();
                        options.status = "all";
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 2: return [4 /*yield*/, (_a.sent()).subscriptions.list(options)];
                    case 3:
                        subscriptions = _a.sent();
                        return [2 /*return*/, message.stripe_subscriptions({ subscriptions: subscriptions })];
                }
            });
        });
    };
    StripeClient.prototype.mesg_get_coupon = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, coupon_id, coupon;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("mesg_get_coupon");
                        dbg("get the coupon with id=".concat(mesg.coupon_id));
                        coupon_id = get_string_field(mesg, "coupon_id");
                        return [4 /*yield*/, this.validate_coupon(coupon_id)];
                    case 1:
                        coupon = (_a.sent()).coupon;
                        return [2 /*return*/, message.stripe_coupon({ coupon: coupon })];
                }
            });
        });
    };
    // Checks these coupon criteria:
    // - Exists
    // - Is valid
    // - Used by this account less than the max per account (hard coded default is 1)
    StripeClient.prototype.validate_coupon = function (coupon_id) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, coupon, coupon_history, times_used;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("validate_coupon");
                        dbg("retrieve the coupon");
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 1: return [4 /*yield*/, (_a.sent()).coupons.retrieve(coupon_id)];
                    case 2:
                        coupon = _a.sent();
                        dbg("check account coupon_history");
                        return [4 /*yield*/, (0, async_utils_1.callback2)(this.client.database.get_coupon_history, {
                                account_id: this.client.account_id,
                            })];
                    case 3:
                        coupon_history = _a.sent();
                        if (!coupon.valid)
                            throw Error("Sorry! This coupon has expired.");
                        if (coupon_history == null) {
                            coupon_history = {};
                        }
                        times_used = coupon_history[coupon.id] != null ? coupon_history[coupon.id] : 0;
                        if (times_used >=
                            (coupon.metadata.max_per_account != null
                                ? coupon.metadata.max_per_account
                                : 1)) {
                            throw Error("You've already used this coupon.");
                        }
                        coupon_history[coupon.id] = times_used;
                        return [2 /*return*/, { coupon: coupon, coupon_history: coupon_history }];
                }
            });
        });
    };
    StripeClient.prototype.mesg_get_charges = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, options, charges;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("mesg_get_charges");
                        dbg("get a list of charges for this customer");
                        return [4 /*yield*/, this.stripe_api_pager_options(mesg)];
                    case 1:
                        options = _a.sent();
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 2: return [4 /*yield*/, (_a.sent()).charges.list(options)];
                    case 3:
                        charges = _a.sent();
                        return [2 /*return*/, message.stripe_charges({ charges: charges })];
                }
            });
        });
    };
    StripeClient.prototype.mesg_get_invoices = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, options, invoices;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("mesg_get_invoices");
                        dbg("get a list of invoices for this customer");
                        return [4 /*yield*/, this.stripe_api_pager_options(mesg)];
                    case 1:
                        options = _a.sent();
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 2: return [4 /*yield*/, (_a.sent()).invoices.list(options)];
                    case 3:
                        invoices = _a.sent();
                        return [2 /*return*/, message.stripe_invoices({ invoices: invoices })];
                }
            });
        });
    };
    // This is not actually used **YET**.
    StripeClient.prototype.mesg_admin_create_invoice_item = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, r, customer_id, email, description, conn, is_anonymous, x, customer;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("mesg_admin_create_invoice_item");
                        dbg();
                        return [4 /*yield*/, (0, awaiting_1.callback)(this.client.assert_user_is_in_group.bind(this.client), "admin")];
                    case 1:
                        _a.sent();
                        dbg("check for existing stripe customer_id");
                        return [4 /*yield*/, (0, async_utils_1.callback2)(this.client.database.get_account, {
                                columns: [
                                    "stripe_customer_id",
                                    "email_address",
                                    "first_name",
                                    "last_name",
                                    "account_id",
                                ],
                                account_id: mesg.account_id,
                                email_address: mesg.email_address,
                            })];
                    case 2:
                        r = _a.sent();
                        customer_id = r.stripe_customer_id;
                        email = r.email_address;
                        description = (0, name_1.default)(r.first_name, r.last_name);
                        mesg.account_id = r.account_id;
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 3:
                        conn = _a.sent();
                        if (!(customer_id != null)) return [3 /*break*/, 5];
                        dbg("already signed up for stripe -- sync local user account with stripe");
                        return [4 /*yield*/, (0, async_utils_1.callback2)(this.client.database.stripe_update_customer, {
                                account_id: mesg.account_id,
                                stripe: conn,
                                customer_id: customer_id,
                            })];
                    case 4:
                        _a.sent();
                        return [3 /*break*/, 9];
                    case 5:
                        dbg("create stripe entry for this customer");
                        return [4 /*yield*/, (0, private_1.default)(mesg.account_id)];
                    case 6:
                        is_anonymous = (_a.sent()).is_anonymous;
                        if (is_anonymous) {
                            throw Error("anonymous users are not allowed to create a stripe customer");
                        }
                        x = {
                            description: description,
                            email: email,
                            metadata: {
                                account_id: mesg.account_id,
                            },
                        };
                        return [4 /*yield*/, conn.customers.create(x)];
                    case 7:
                        customer = _a.sent();
                        customer_id = customer.id;
                        dbg("store customer id in our database");
                        return [4 /*yield*/, (0, async_utils_1.callback2)(this.client.database.set_stripe_customer_id, {
                                account_id: mesg.account_id,
                                customer_id: customer_id,
                            })];
                    case 8:
                        _a.sent();
                        _a.label = 9;
                    case 9:
                        if (!(mesg.amount != null && mesg.description != null)) {
                            dbg("no amount or no description, so not creating an invoice");
                            return [2 /*return*/];
                        }
                        dbg("now create the invoice item");
                        return [4 /*yield*/, conn.invoiceItems.create({
                                customer: customer_id,
                                amount: mesg.amount * 100,
                                currency: "usd",
                                description: mesg.description,
                            })];
                    case 10:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.mesg_get_available_upgrades = function (_mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, customer, stripe_data, projects, _a, excess, available, total;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        dbg = this.dbg("mesg_get_available_upgrades");
                        dbg("get stripe customer data");
                        return [4 /*yield*/, this.get_customer()];
                    case 1:
                        customer = _b.sent();
                        if (customer == null || customer.subscriptions == null) {
                            // no upgrades since not even a stripe account.
                            return [2 /*return*/, message.available_upgrades({
                                    total: {},
                                    excess: {},
                                    available: {},
                                })];
                        }
                        stripe_data = customer.subscriptions.data;
                        dbg("get user project upgrades");
                        return [4 /*yield*/, (0, async_utils_1.callback2)(this.client.database.get_user_project_upgrades, {
                                account_id: this.client.account_id,
                            })];
                    case 2:
                        projects = _b.sent();
                        _a = (0, upgrades_1.available_upgrades)(stripe_data, projects), excess = _a.excess, available = _a.available;
                        total = (0, upgrades_1.get_total_upgrades)(stripe_data);
                        return [2 /*return*/, message.available_upgrades({
                                total: total,
                                excess: excess,
                                available: available,
                            })];
                }
            });
        });
    };
    StripeClient.prototype.mesg_remove_all_upgrades = function (mesg) {
        return __awaiter(this, void 0, void 0, function () {
            var dbg;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("mesg_remove_all_upgrades");
                        dbg();
                        if (this.client.account_id == null)
                            throw Error("you must be signed in");
                        return [4 /*yield*/, (0, async_utils_1.callback2)(this.client.database.remove_all_user_project_upgrades, {
                                account_id: this.client.account_id,
                                projects: mesg.projects,
                            })];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.mesg_sync_site_license_subscriptions = function () {
        return __awaiter(this, void 0, void 0, function () {
            var dbg;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("mesg_sync_site_license_subscriptions");
                        dbg();
                        if (this.client.account_id == null)
                            throw Error("you must be signed in");
                        return [4 /*yield*/, this.client.database.sync_site_license_subscriptions(this.client.account_id)];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.cancelEverything = function () {
        var _a, _b;
        return __awaiter(this, void 0, void 0, function () {
            var dbg, customer_id, conn, customer, payment_methods, payment_methods_1, payment_methods_1_1, id, e_1_1, subscriptions, subscriptions_1, subscriptions_1_1, id, e_2_1;
            var e_1, _c, e_2, _d;
            return __generator(this, function (_e) {
                switch (_e.label) {
                    case 0:
                        dbg = this.dbg("cancelEverything");
                        return [4 /*yield*/, this.get_customer_id()];
                    case 1:
                        customer_id = _e.sent();
                        dbg("customer_id = ", customer_id);
                        if (customer_id == null) {
                            // nothing to do, since no stripe info about this user.
                            return [2 /*return*/];
                        }
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 2:
                        conn = _e.sent();
                        dbg("Cancel the credit cards");
                        return [4 /*yield*/, this.get_customer(customer_id)];
                    case 3:
                        customer = _e.sent();
                        payment_methods = (_a = customer.sources) === null || _a === void 0 ? void 0 : _a.data;
                        if (!(payment_methods != null)) return [3 /*break*/, 11];
                        _e.label = 4;
                    case 4:
                        _e.trys.push([4, 9, 10, 11]);
                        payment_methods_1 = __values(payment_methods), payment_methods_1_1 = payment_methods_1.next();
                        _e.label = 5;
                    case 5:
                        if (!!payment_methods_1_1.done) return [3 /*break*/, 8];
                        id = payment_methods_1_1.value.id;
                        return [4 /*yield*/, conn.customers.deleteSource(customer_id, id)];
                    case 6:
                        _e.sent();
                        _e.label = 7;
                    case 7:
                        payment_methods_1_1 = payment_methods_1.next();
                        return [3 /*break*/, 5];
                    case 8: return [3 /*break*/, 11];
                    case 9:
                        e_1_1 = _e.sent();
                        e_1 = { error: e_1_1 };
                        return [3 /*break*/, 11];
                    case 10:
                        try {
                            if (payment_methods_1_1 && !payment_methods_1_1.done && (_c = payment_methods_1.return)) _c.call(payment_methods_1);
                        }
                        finally { if (e_1) throw e_1.error; }
                        return [7 /*endfinally*/];
                    case 11:
                        dbg("Cancel the subscriptions (at period end)");
                        subscriptions = (_b = customer.subscriptions) === null || _b === void 0 ? void 0 : _b.data;
                        if (!(subscriptions != null)) return [3 /*break*/, 19];
                        _e.label = 12;
                    case 12:
                        _e.trys.push([12, 17, 18, 19]);
                        subscriptions_1 = __values(subscriptions), subscriptions_1_1 = subscriptions_1.next();
                        _e.label = 13;
                    case 13:
                        if (!!subscriptions_1_1.done) return [3 /*break*/, 16];
                        id = subscriptions_1_1.value.id;
                        return [4 /*yield*/, conn.subscriptions.update(id, {
                                cancel_at_period_end: true,
                            })];
                    case 14:
                        _e.sent();
                        _e.label = 15;
                    case 15:
                        subscriptions_1_1 = subscriptions_1.next();
                        return [3 /*break*/, 13];
                    case 16: return [3 /*break*/, 19];
                    case 17:
                        e_2_1 = _e.sent();
                        e_2 = { error: e_2_1 };
                        return [3 /*break*/, 19];
                    case 18:
                        try {
                            if (subscriptions_1_1 && !subscriptions_1_1.done && (_d = subscriptions_1.return)) _d.call(subscriptions_1);
                        }
                        finally { if (e_2) throw e_2.error; }
                        return [7 /*endfinally*/];
                    case 19:
                        dbg("Sync the database to indicate that everything is canceled.");
                        return [4 /*yield*/, this.update_database()];
                    case 20:
                        _e.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.getPaymentMethods = function () {
        return __awaiter(this, void 0, void 0, function () {
            var dbg, customer, conn;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        dbg = this.dbg("get_sources");
                        dbg("get a list of all the payment sources that this customer has");
                        return [4 /*yield*/, this.need_customer_id()];
                    case 1:
                        customer = _a.sent();
                        return [4 /*yield*/, (0, connection_1.default)()];
                    case 2:
                        conn = _a.sent();
                        return [4 /*yield*/, conn.paymentMethods.list({ customer: customer, type: "card" })];
                    case 3:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.setDefaultSource = function (default_source) {
        return __awaiter(this, void 0, void 0, function () {
            var conn, _a, _b;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0: return [4 /*yield*/, (0, connection_1.default)()];
                    case 1:
                        conn = _c.sent();
                        _b = (_a = conn.customers).update;
                        return [4 /*yield*/, this.need_customer_id()];
                    case 2: return [4 /*yield*/, _b.apply(_a, [_c.sent(), {
                                default_source: default_source,
                            }])];
                    case 3:
                        _c.sent();
                        return [4 /*yield*/, this.update_database()];
                    case 4:
                        _c.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.deletePaymentMethod = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            var conn, _a, _b;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0: return [4 /*yield*/, (0, connection_1.default)()];
                    case 1:
                        conn = _c.sent();
                        _b = (_a = conn.customers).deleteSource;
                        return [4 /*yield*/, this.need_customer_id()];
                    case 2: return [4 /*yield*/, _b.apply(_a, [_c.sent(), id])];
                    case 3:
                        _c.sent();
                        return [4 /*yield*/, this.update_database()];
                    case 4:
                        _c.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.createPaymentMethod = function (token) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.mesg_create_source({ token: token })];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    StripeClient.prototype.cancelSubscription = function (id) {
        return __awaiter(this, void 0, void 0, function () {
            var conn;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, (0, connection_1.default)()];
                    case 1:
                        conn = _a.sent();
                        return [4 /*yield*/, conn.subscriptions.update(id, { cancel_at_period_end: true })];
                    case 2:
                        _a.sent();
                        return [4 /*yield*/, this.update_database()];
                    case 3:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    return StripeClient;
}());
exports.StripeClient = StripeClient;
//# sourceMappingURL=client.js.map

/***/ }),

/***/ 578608:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {

"use strict";

/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
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.
*/
var stripe_1 = __importDefault(__webpack_require__(476229));
var settings_1 = __webpack_require__(840336);
// See https://stripe.com/docs/api/versioning
var apiVersion = "2020-03-02";
var stripe = undefined;
var key = "";
var last = 0;
function getConn() {
    return __awaiter(this, void 0, void 0, function () {
        var _a, stripe_publishable_key, stripe_secret_key;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    if (stripe != null && new Date().valueOf() - last <= 1000 * 60) {
                        return [2 /*return*/, stripe];
                    }
                    return [4 /*yield*/, (0, settings_1.getServerSettings)()];
                case 1:
                    _a = _b.sent(), stripe_publishable_key = _a.stripe_publishable_key, stripe_secret_key = _a.stripe_secret_key;
                    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: apiVersion,
                        });
                        stripe.publishable_key = stripe_publishable_key;
                        last = new Date().valueOf();
                    }
                    return [2 /*return*/, stripe];
            }
        });
    });
}
exports["default"] = getConn;
//# sourceMappingURL=connection.js.map

/***/ }),

/***/ 515767:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {

"use strict";

/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.stripe_sales_tax = void 0;
/*
Compute sales tax for a given customer in WA state.
*/
var sales_tax_1 = __importDefault(__webpack_require__(853307));
var connection_1 = __importDefault(__webpack_require__(578608));
function stripe_sales_tax(customer_id, dbg) {
    var _a;
    return __awaiter(this, void 0, void 0, function () {
        var conn, customer, zip, _b, _c, x, tax;
        var e_1, _d;
        return __generator(this, function (_e) {
            switch (_e.label) {
                case 0: return [4 /*yield*/, (0, connection_1.default)()];
                case 1:
                    conn = _e.sent();
                    return [4 /*yield*/, conn.customers.retrieve(customer_id)];
                case 2:
                    customer = _e.sent();
                    if (customer.deleted) {
                        // mainly have this check so Typescript is happy
                        dbg("customer was deleted");
                        return [2 /*return*/, 0];
                    }
                    if (customer.default_source == null) {
                        dbg("no default source");
                        return [2 /*return*/, 0];
                    }
                    zip = undefined;
                    if (customer.sources != null) {
                        try {
                            for (_b = __values(customer.sources.data), _c = _b.next(); !_c.done; _c = _b.next()) {
                                x = _c.value;
                                if (x.id == customer.default_source) {
                                    zip = (_a = x.address_zip) === null || _a === void 0 ? void 0 : _a.slice(0, 5); // as any due to bug in Stripe's types
                                    break;
                                }
                            }
                        }
                        catch (e_1_1) { e_1 = { error: e_1_1 }; }
                        finally {
                            try {
                                if (_c && !_c.done && (_d = _b.return)) _d.call(_b);
                            }
                            finally { if (e_1) throw e_1.error; }
                        }
                    }
                    if (zip == null) {
                        return [2 /*return*/, 0];
                    }
                    tax = (0, sales_tax_1.default)(zip);
                    dbg("tax: ", tax);
                    return [2 /*return*/, tax];
            }
        });
    });
}
exports.stripe_sales_tax = stripe_sales_tax;
//# sourceMappingURL=sales-tax.js.map

/***/ }),

/***/ 676941:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
var misc_1 = __webpack_require__(854006);
function stripeName(firstName, lastName) {
    return (0, misc_1.trunc_middle)("".concat(firstName !== null && firstName !== void 0 ? firstName : "", " ").concat(lastName !== null && lastName !== void 0 ? lastName : ""), 200);
}
exports["default"] = stripeName;
//# sourceMappingURL=name.js.map

/***/ }),

/***/ 853307:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

/*
Sales tax for WA state.

Of course, this shouldn't exist much longer, since it's the sort of thing
that stripe automates.
*/
Object.defineProperty(exports, "__esModule", ({ value: true }));
// WA state sales tax rates, as of July 11 2017.
// Generated via scripts/sales_tax.py
function salesTax(zip) {
    var _a;
    return (_a = WA_sales_tax[zip]) !== null && _a !== void 0 ? _a : 0;
}
exports["default"] = salesTax;
var WA_sales_tax = {
    98001: 0.099,
    98002: 0.086,
    98003: 0.1,
    98004: 0.1,
    98005: 0.1,
    98006: 0.086,
    98007: 0.1,
    98008: 0.1,
    98009: 0.1,
    98010: 0.086,
    98011: 0.1,
    98012: 0.077,
    98013: 0.086,
    98014: 0.086,
    98015: 0.1,
    98019: 0.077,
    98020: 0.1,
    98021: 0.077,
    98022: 0.079,
    98023: 0.1,
    98024: 0.086,
    98025: 0.086,
    98026: 0.1,
    98027: 0.086,
    98028: 0.1,
    98029: 0.086,
    98030: 0.1,
    98031: 0.1,
    98032: 0.1,
    98033: 0.1,
    98034: 0.1,
    98035: 0.1,
    98036: 0.103,
    98037: 0.103,
    98038: 0.086,
    98039: 0.1,
    98040: 0.1,
    98041: 0.1,
    98042: 0.086,
    98043: 0.1,
    98045: 0.086,
    98046: 0.104,
    98047: 0.093,
    98050: 0.086,
    98051: 0.086,
    98052: 0.086,
    98053: 0.086,
    98054: 0.086,
    98055: 0.1,
    98056: 0.1,
    98057: 0.1,
    98058: 0.086,
    98059: 0.086,
    98061: 0.09,
    98062: 0.1,
    98063: 0.1,
    98064: 0.1,
    98065: 0.086,
    98068: 0.08,
    98070: 0.086,
    98071: 0.1,
    98072: 0.077,
    98073: 0.1,
    98074: 0.086,
    98075: 0.086,
    98077: 0.077,
    98082: 0.104,
    98083: 0.1,
    98087: 0.103,
    98089: 0.1,
    98092: 0.086,
    98093: 0.1,
    98101: 0.101,
    98102: 0.101,
    98103: 0.101,
    98104: 0.101,
    98105: 0.101,
    98106: 0.1,
    98107: 0.101,
    98108: 0.1,
    98109: 0.101,
    98110: 0.09,
    98111: 0.101,
    98112: 0.101,
    98113: 0.101,
    98114: 0.101,
    98115: 0.101,
    98116: 0.101,
    98117: 0.101,
    98118: 0.101,
    98119: 0.101,
    98121: 0.101,
    98122: 0.101,
    98124: 0.101,
    98125: 0.1,
    98126: 0.1,
    98127: 0.101,
    98129: 0.101,
    98131: 0.1,
    98132: 0.1,
    98133: 0.1,
    98134: 0.101,
    98136: 0.101,
    98138: 0.1,
    98139: 0.101,
    98141: 0.101,
    98144: 0.1,
    98145: 0.101,
    98146: 0.1,
    98148: 0.1,
    98151: 0.1,
    98154: 0.101,
    98155: 0.1,
    98158: 0.1,
    98160: 0.1,
    98161: 0.101,
    98164: 0.101,
    98165: 0.101,
    98166: 0.1,
    98168: 0.1,
    98170: 0.1,
    98171: 0.1,
    98174: 0.101,
    98175: 0.101,
    98177: 0.1,
    98178: 0.1,
    98181: 0.101,
    98184: 0.101,
    98185: 0.101,
    98188: 0.1,
    98189: 0.1,
    98190: 0.1,
    98191: 0.101,
    98194: 0.101,
    98195: 0.101,
    98198: 0.1,
    98199: 0.101,
    98201: 0.089,
    98203: 0.077,
    98204: 0.097,
    98205: 0.097,
    98206: 0.097,
    98207: 0.097,
    98208: 0.077,
    98213: 0.097,
    98220: 0.085,
    98221: 0.081,
    98222: 0.081,
    98223: 0.077,
    98224: 0.086,
    98225: 0.079,
    98226: 0.079,
    98227: 0.087,
    98228: 0.087,
    98229: 0.079,
    98230: 0.079,
    98231: 0.085,
    98232: 0.085,
    98233: 0.081,
    98235: 0.085,
    98236: 0.087,
    98237: 0.079,
    98238: 0.081,
    98239: 0.087,
    98240: 0.085,
    98241: 0.081,
    98243: 0.081,
    98244: 0.079,
    98245: 0.081,
    98247: 0.085,
    98248: 0.079,
    98249: 0.087,
    98250: 0.081,
    98251: 0.086,
    98252: 0.077,
    98253: 0.087,
    98255: 0.085,
    98256: 0.089,
    98257: 0.081,
    98258: 0.077,
    98259: 0.091,
    98260: 0.087,
    98261: 0.081,
    98262: 0.079,
    98263: 0.085,
    98264: 0.085,
    98266: 0.085,
    98267: 0.085,
    98270: 0.077,
    98271: 0.077,
    98272: 0.077,
    98273: 0.081,
    98274: 0.081,
    98275: 0.097,
    98276: 0.085,
    98277: 0.087,
    98278: 0.087,
    98279: 0.081,
    98280: 0.081,
    98281: 0.079,
    98282: 0.077,
    98283: 0.079,
    98284: 0.077,
    98286: 0.081,
    98287: 0.089,
    98288: 0.086,
    98290: 0.077,
    98291: 0.091,
    98292: 0.077,
    98293: 0.089,
    98294: 0.077,
    98295: 0.085,
    98296: 0.077,
    98297: 0.081,
    98303: 0.079,
    98304: 0.078,
    98305: 0.084,
    98310: 0.085,
    98311: 0.09,
    98312: 0.085,
    98314: 0.09,
    98315: 0.09,
    98320: 0.085,
    98321: 0.079,
    98322: 0.09,
    98323: 0.079,
    98324: 0.084,
    98325: 0.09,
    98326: 0.084,
    98327: 0.079,
    98328: 0.079,
    98329: 0.079,
    98330: 0.078,
    98331: 0.084,
    98332: 0.079,
    98333: 0.079,
    98335: 0.079,
    98336: 0.078,
    98337: 0.09,
    98338: 0.079,
    98339: 0.09,
    98340: 0.09,
    98342: 0.09,
    98343: 0.084,
    98344: 0.079,
    98345: 0.09,
    98346: 0.09,
    98348: 0.079,
    98349: 0.079,
    98350: 0.084,
    98351: 0.079,
    98352: 0.093,
    98353: 0.09,
    98354: 0.099,
    98355: 0.078,
    98356: 0.078,
    98357: 0.084,
    98358: 0.09,
    98359: 0.079,
    98360: 0.079,
    98361: 0.078,
    98362: 0.084,
    98363: 0.084,
    98364: 0.09,
    98365: 0.09,
    98366: 0.09,
    98367: 0.09,
    98368: 0.09,
    98370: 0.09,
    98371: 0.099,
    98372: 0.093,
    98373: 0.093,
    98374: 0.093,
    98375: 0.093,
    98376: 0.09,
    98377: 0.078,
    98378: 0.09,
    98380: 0.085,
    98381: 0.084,
    98382: 0.084,
    98383: 0.09,
    98384: 0.09,
    98385: 0.079,
    98386: 0.09,
    98387: 0.079,
    98388: 0.079,
    98390: 0.093,
    98391: 0.079,
    98392: 0.09,
    98393: 0.09,
    98394: 0.079,
    98395: 0.079,
    98396: 0.079,
    98397: 0.079,
    98398: 0.079,
    98401: 0.101,
    98402: 0.099,
    98403: 0.101,
    98404: 0.099,
    98405: 0.093,
    98406: 0.093,
    98407: 0.099,
    98408: 0.099,
    98409: 0.099,
    98411: 0.101,
    98412: 0.101,
    98413: 0.101,
    98415: 0.101,
    98416: 0.101,
    98417: 0.101,
    98418: 0.101,
    98419: 0.101,
    98421: 0.099,
    98422: 0.099,
    98424: 0.099,
    98430: 0.093,
    98431: 0.093,
    98433: 0.093,
    98438: 0.093,
    98439: 0.093,
    98442: 0.093,
    98443: 0.099,
    98444: 0.093,
    98445: 0.093,
    98446: 0.079,
    98447: 0.099,
    98448: 0.099,
    98450: 0.099,
    98455: 0.099,
    98460: 0.099,
    98464: 0.099,
    98465: 0.099,
    98466: 0.099,
    98467: 0.099,
    98471: 0.101,
    98477: 0.101,
    98481: 0.101,
    98490: 0.101,
    98492: 0.101,
    98493: 0.099,
    98496: 0.099,
    98497: 0.099,
    98498: 0.093,
    98499: 0.093,
    98501: 0.079,
    98502: 0.079,
    98503: 0.087,
    98504: 0.088,
    98505: 0.087,
    98506: 0.079,
    98507: 0.088,
    98508: 0.088,
    98509: 0.089,
    98511: 0.089,
    98512: 0.079,
    98513: 0.079,
    98516: 0.079,
    98520: 0.088,
    98522: 0.078,
    98524: 0.085,
    98526: 0.088,
    98527: 0.08,
    98528: 0.079,
    98530: 0.079,
    98531: 0.078,
    98532: 0.078,
    98533: 0.078,
    98535: 0.088,
    98536: 0.088,
    98537: 0.08,
    98538: 0.078,
    98539: 0.078,
    98540: 0.079,
    98541: 0.085,
    98542: 0.078,
    98544: 0.078,
    98546: 0.085,
    98547: 0.08,
    98548: 0.085,
    98550: 0.088,
    98552: 0.088,
    98554: 0.08,
    98555: 0.085,
    98556: 0.079,
    98557: 0.085,
    98558: 0.079,
    98559: 0.088,
    98560: 0.085,
    98561: 0.08,
    98562: 0.088,
    98563: 0.085,
    98564: 0.078,
    98565: 0.078,
    98566: 0.088,
    98568: 0.078,
    98569: 0.088,
    98570: 0.078,
    98571: 0.088,
    98572: 0.078,
    98575: 0.088,
    98576: 0.079,
    98577: 0.08,
    98579: 0.078,
    98580: 0.079,
    98581: 0.078,
    98582: 0.078,
    98583: 0.088,
    98584: 0.085,
    98585: 0.078,
    98586: 0.08,
    98587: 0.088,
    98588: 0.085,
    98589: 0.079,
    98590: 0.08,
    98591: 0.078,
    98592: 0.085,
    98593: 0.078,
    98595: 0.088,
    98596: 0.078,
    98597: 0.079,
    98599: 0.088,
    98601: 0.077,
    98602: 0.07,
    98603: 0.078,
    98604: 0.077,
    98605: 0.07,
    98606: 0.077,
    98607: 0.077,
    98609: 0.078,
    98610: 0.077,
    98611: 0.078,
    98612: 0.076,
    98613: 0.07,
    98614: 0.08,
    98616: 0.078,
    98617: 0.07,
    98619: 0.07,
    98620: 0.07,
    98621: 0.076,
    98622: 0.077,
    98623: 0.07,
    98624: 0.08,
    98625: 0.078,
    98626: 0.078,
    98628: 0.07,
    98629: 0.077,
    98631: 0.08,
    98632: 0.076,
    98635: 0.07,
    98637: 0.08,
    98638: 0.076,
    98639: 0.077,
    98640: 0.08,
    98641: 0.08,
    98642: 0.077,
    98643: 0.076,
    98644: 0.08,
    98645: 0.078,
    98647: 0.076,
    98648: 0.077,
    98649: 0.078,
    98650: 0.07,
    98651: 0.077,
    98660: 0.077,
    98661: 0.084,
    98662: 0.077,
    98663: 0.084,
    98664: 0.084,
    98665: 0.084,
    98666: 0.084,
    98667: 0.084,
    98668: 0.084,
    98670: 0.07,
    98671: 0.077,
    98672: 0.07,
    98673: 0.07,
    98674: 0.077,
    98675: 0.077,
    98682: 0.077,
    98683: 0.084,
    98684: 0.084,
    98685: 0.077,
    98686: 0.077,
    98687: 0.084,
    98801: 0.082,
    98802: 0.082,
    98807: 0.084,
    98811: 0.082,
    98812: 0.078,
    98813: 0.077,
    98814: 0.081,
    98815: 0.082,
    98816: 0.081,
    98817: 0.082,
    98819: 0.081,
    98821: 0.082,
    98822: 0.082,
    98823: 0.078,
    98824: 0.079,
    98826: 0.082,
    98827: 0.081,
    98828: 0.082,
    98829: 0.081,
    98830: 0.077,
    98831: 0.082,
    98832: 0.079,
    98833: 0.081,
    98834: 0.081,
    98836: 0.082,
    98837: 0.079,
    98840: 0.077,
    98841: 0.077,
    98843: 0.078,
    98844: 0.081,
    98845: 0.078,
    98846: 0.081,
    98847: 0.082,
    98848: 0.078,
    98849: 0.081,
    98850: 0.078,
    98851: 0.079,
    98852: 0.082,
    98853: 0.079,
    98855: 0.081,
    98856: 0.081,
    98857: 0.077,
    98858: 0.078,
    98859: 0.081,
    98860: 0.079,
    98862: 0.081,
    98901: 0.079,
    98902: 0.081,
    98903: 0.079,
    98904: 0.079,
    98907: 0.082,
    98908: 0.079,
    98909: 0.081,
    98920: 0.079,
    98921: 0.079,
    98922: 0.08,
    98923: 0.079,
    98925: 0.08,
    98926: 0.08,
    98929: 0.08,
    98930: 0.079,
    98932: 0.079,
    98933: 0.079,
    98934: 0.08,
    98935: 0.07,
    98936: 0.079,
    98937: 0.078,
    98938: 0.079,
    98939: 0.079,
    98940: 0.08,
    98941: 0.08,
    98942: 0.079,
    98943: 0.08,
    98944: 0.079,
    98946: 0.08,
    98947: 0.079,
    98948: 0.079,
    98950: 0.08,
    98951: 0.079,
    98952: 0.079,
    98953: 0.079,
    99001: 0.088,
    99003: 0.081,
    99004: 0.081,
    99005: 0.081,
    99006: 0.076,
    99008: 0.08,
    99009: 0.076,
    99011: 0.088,
    99012: 0.081,
    99013: 0.076,
    99014: 0.088,
    99016: 0.081,
    99017: 0.078,
    99018: 0.081,
    99019: 0.081,
    99020: 0.081,
    99021: 0.081,
    99022: 0.081,
    99023: 0.081,
    99025: 0.081,
    99026: 0.076,
    99027: 0.081,
    99029: 0.08,
    99030: 0.081,
    99031: 0.081,
    99032: 0.077,
    99033: 0.078,
    99034: 0.076,
    99036: 0.081,
    99037: 0.081,
    99039: 0.081,
    99040: 0.076,
    99101: 0.076,
    99102: 0.078,
    99103: 0.079,
    99104: 0.078,
    99105: 0.077,
    99107: 0.077,
    99109: 0.076,
    99110: 0.076,
    99111: 0.078,
    99113: 0.078,
    99114: 0.076,
    99115: 0.078,
    99116: 0.077,
    99117: 0.08,
    99118: 0.077,
    99119: 0.076,
    99121: 0.077,
    99122: 0.076,
    99123: 0.079,
    99124: 0.077,
    99125: 0.077,
    99126: 0.076,
    99128: 0.078,
    99129: 0.076,
    99130: 0.078,
    99131: 0.076,
    99133: 0.078,
    99134: 0.08,
    99135: 0.079,
    99136: 0.078,
    99137: 0.076,
    99138: 0.077,
    99139: 0.076,
    99140: 0.077,
    99141: 0.076,
    99143: 0.078,
    99144: 0.08,
    99146: 0.077,
    99147: 0.08,
    99148: 0.076,
    99149: 0.078,
    99150: 0.077,
    99151: 0.076,
    99152: 0.076,
    99153: 0.076,
    99154: 0.08,
    99155: 0.077,
    99156: 0.076,
    99157: 0.076,
    99158: 0.078,
    99159: 0.077,
    99160: 0.077,
    99161: 0.078,
    99163: 0.078,
    99164: 0.078,
    99165: 0.078,
    99166: 0.077,
    99167: 0.076,
    99169: 0.077,
    99170: 0.078,
    99171: 0.078,
    99173: 0.076,
    99174: 0.078,
    99176: 0.078,
    99179: 0.078,
    99180: 0.076,
    99181: 0.076,
    99185: 0.08,
    99201: 0.088,
    99202: 0.088,
    99203: 0.081,
    99204: 0.088,
    99205: 0.081,
    99206: 0.081,
    99207: 0.088,
    99208: 0.081,
    99209: 0.088,
    99210: 0.088,
    99211: 0.088,
    99212: 0.081,
    99213: 0.088,
    99214: 0.088,
    99215: 0.088,
    99216: 0.081,
    99217: 0.081,
    99218: 0.081,
    99219: 0.088,
    99220: 0.088,
    99223: 0.081,
    99224: 0.081,
    99228: 0.088,
    99251: 0.088,
    99252: 0.088,
    99256: 0.088,
    99258: 0.088,
    99260: 0.088,
    99299: 0.088,
    99301: 0.08,
    99302: 0.086,
    99320: 0.08,
    99321: 0.079,
    99322: 0.07,
    99323: 0.081,
    99324: 0.087,
    99326: 0.077,
    99328: 0.081,
    99329: 0.081,
    99330: 0.08,
    99333: 0.078,
    99335: 0.08,
    99336: 0.08,
    99337: 0.08,
    99338: 0.08,
    99341: 0.077,
    99343: 0.08,
    99344: 0.077,
    99345: 0.08,
    99346: 0.08,
    99347: 0.079,
    99348: 0.081,
    99349: 0.079,
    99350: 0.07,
    99352: 0.08,
    99353: 0.08,
    99354: 0.086,
    99356: 0.07,
    99357: 0.079,
    99359: 0.081,
    99360: 0.081,
    99361: 0.081,
    99362: 0.081,
    99363: 0.081,
    99371: 0.077,
    99401: 0.077,
    99402: 0.077,
    99403: 0.079,
};
//# sourceMappingURL=sales-tax.js.map

/***/ }),

/***/ 874999:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {

/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var PROJECT_UPGRADES = (__webpack_require__(47896).PROJECT_UPGRADES);
var misc = __webpack_require__(854006);
// This is used by the frontend in r_account.  It's also used by the backend
// to double check the claims of the frontend.
// stripe_subscriptions_data = stripe_customer?.subscriptions?.data
function get_total_upgrades(stripe_subscriptions_data) {
    var e_1, _a;
    var subs = stripe_subscriptions_data;
    if (subs == null) {
        return {};
    }
    // always running isn't in any of the subscriptions and I don't want to edit benefits
    // for all of them, so just put a zero 0 (since this whole old upgrades thing
    // will eventually go away). https://github.com/sagemathinc/cocalc/issues/4802
    var total = { always_running: 0 };
    try {
        for (var subs_1 = __values(subs), subs_1_1 = subs_1.next(); !subs_1_1.done; subs_1_1 = subs_1.next()) {
            var sub = subs_1_1.value;
            if (sub.status != "active" && sub.status != "trialing") {
                // not yet paid for or no longer available.
                continue;
            }
            var info = PROJECT_UPGRADES.subscription[sub.plan.id.split("-")[0]];
            if (info == null) {
                // there are now some subscriptions that have nothing to do with "upgrades" (e.g., for licenses).
                continue;
            }
            var benefits = info.benefits;
            // TODO: add a quantity third field to map_sum to make this not so naive...
            for (var q = 0; q < sub.quantity; q++) {
                total = misc.map_sum(total, benefits);
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (subs_1_1 && !subs_1_1.done && (_a = subs_1.return)) _a.call(subs_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return total;
}
exports.get_total_upgrades = get_total_upgrades;
//
// INPUT:
//    subscriptions = {standard:2, premium:1, course:2, ...}
//    projects = {project_id:{cores:1, network:1, ...}, ...}
//
// OUTPUT:
//     {available:{cores:10, network:3, ...},   excess:{project_id:{cores:2, ...}}  }
//
function available_upgrades(stripe_subscriptions_data, projects) {
    var e_2, _a, _b;
    var project_id, upgrades;
    var available = get_total_upgrades(stripe_subscriptions_data); // start with amount available being your quota
    var excess = {}; // nothing exceeds quota
    // sort projects by project_id so that excess will be well defined
    var v = [];
    for (project_id in projects) {
        upgrades = projects[project_id];
        v.push({ project_id: project_id, upgrades: upgrades });
    }
    v.sort(function (a, b) { return misc.cmp(a.project_id, b.project_id); });
    try {
        for (var v_1 = __values(v), v_1_1 = v_1.next(); !v_1_1.done; v_1_1 = v_1.next()) {
            _b = v_1_1.value, project_id = _b.project_id, upgrades = _b.upgrades;
            for (var prop in upgrades) {
                var curval = upgrades[prop];
                if (available[prop] == null) {
                    available[prop] = 0;
                } // ensure that available is defined for this prop
                if (curval <= available[prop]) {
                    // if the current value for this project is within what is left, just subtract it off
                    available[prop] -= curval;
                }
                else {
                    // otherwise, it goes over, so record by how much in excess, then set available to 0.
                    if (excess[project_id] == null) {
                        excess[project_id] = {};
                    }
                    excess[project_id][prop] = curval - available[prop];
                    available[prop] = 0;
                }
            }
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (v_1_1 && !v_1_1.done && (_a = v_1.return)) _a.call(v_1);
        }
        finally { if (e_2) throw e_2.error; }
    }
    return { available: available, excess: excess };
}
exports.available_upgrades = available_upgrades;
// INPUT: same as above, but also a single project_id
//
// OUTPUT:  Returns the maximum amount for each upgrade setting that a control
// (which starts at 0) for configuring that setting for the project should go up to.
//
//      {cores:2, network:1, disk_quota:2000, memory:1000}
//
//
function upgrade_maxes(stripe_subscriptions_data, projects, project_id) {
    var _a = available_upgrades(stripe_subscriptions_data, projects), available = _a.available, excess = _a.excess;
    var allocated = projects[project_id];
    var maxes = {};
    for (var param in available) {
        var avail = available[param];
        var max = PROJECT_UPGRADES.max_per_project[param]; // the maximum allowed for this param for any project
        var alloc = allocated[param] != null ? allocated[param] : 0; // how much has already been allocated to this project
        maxes[param] = Math.min(alloc + avail, max);
    }
    return maxes;
}
exports.upgrade_maxes = upgrade_maxes;
//# sourceMappingURL=upgrades.js.map

/***/ })

};
;