smc-hub
Version:
CoCalc: Backend webserver component
861 lines • 42.5 kB
JavaScript
"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;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stripe_name = exports.StripeClient = void 0;
var hof_1 = require("async-await-utils/hof");
var awaiting_1 = require("awaiting");
// STOPGAP FIX: relative dirs necessary for manage service
var async_utils_1 = require("smc-util/async-utils");
var misc_1 = require("smc-util/misc");
var message = __importStar(require("smc-util/message"));
var upgrades_1 = require("smc-util/upgrades");
var connection_1 = require("./connection");
var sales_tax_1 = require("./sales-tax");
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[" + 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[" + name + "] must be defined");
return x;
}
var StripeClient = /** @class */ (function () {
function StripeClient(client) {
this.client = client;
var conn = connection_1.get_stripe();
if (conn == null)
throw Error("stripe billing not configured");
this.conn = conn;
this.get_customer_id = hof_1.reuseInFlight(this.get_customer_id.bind(this));
}
StripeClient.prototype.dbg = function (f) {
return this.client.dbg("stripe." + 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*/, 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*/, this.conn.customers.retrieve(customer_id)];
case 3: 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_" + mesg.event.slice(7)];
if (!(f == null)) return [3 /*break*/, 1];
throw Error("no such message type " + 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 = "" + 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 = {
stripe_publishable_key: this.conn.publishable_key
};
return [4 /*yield*/, this.update_database()];
case 1:
// 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, 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");
dbg("get identifying info about user");
return [4 /*yield*/, async_utils_1.callback2(this.client.database.get_account, {
columns: ["email_address", "first_name", "last_name"],
account_id: this.client.account_id,
})];
case 1:
r = _a.sent();
email = r.email_address;
description = stripe_name(r.first_name, r.last_name);
dbg("they are " + description + " with email " + email);
dbg("creating stripe customer");
x = {
source: token,
description: description,
name: description,
email: email,
metadata: {
account_id: this.client.account_id,
},
};
return [4 /*yield*/, this.conn.customers.create(x)];
case 2:
customer_id = (_a.sent()).id;
dbg("success; now save customer_id to database");
return [4 /*yield*/, async_utils_1.callback2(this.client.database.set_stripe_customer_id, {
account_id: this.client.account_id,
customer_id: customer_id,
})];
case 3:
_a.sent();
return [4 /*yield*/, this.update_database()];
case 4:
_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*/, this.conn.customers.createSource(customer_id, { source: token })];
case 2:
_a.sent();
return [4 /*yield*/, this.update_database()];
case 3:
_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*/, this.conn.customers.deleteSource(customer_id, card_id)];
case 2:
_a.sent();
return [4 /*yield*/, this.update_database()];
case 3:
_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*/, this.conn.customers.update(customer_id, {
default_source: card_id,
})];
case 2:
_a.sent();
return [4 /*yield*/, this.update_database()];
case 3:
_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;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.dbg("update_database")();
return [4 /*yield*/, this.get_customer_id()];
case 1:
customer_id = _a.sent();
if (customer_id == null)
return [2 /*return*/];
return [4 /*yield*/, async_utils_1.callback2(this.client.database.stripe_update_customer, {
account_id: this.client.account_id,
stripe: this.conn,
customer_id: customer_id,
})];
case 2: return [2 /*return*/, _a.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*/, this.conn.sources.update(card_id, info)];
case 1:
_a.sent();
return [4 /*yield*/, this.update_database()];
case 2:
_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*/, 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 = require("smc-util/schema").PROJECT_UPGRADES.subscription[plan.split("-")[0]];
if (schema == null)
throw Error("unknown plan -- '" + 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*/, this.conn.subscriptions.create(options)];
case 3:
_b.sent();
dbg("added subscription; now save info in our database about it...");
return [4 /*yield*/, this.update_database()];
case 4:
_b.sent();
if (!(options.coupon != null)) return [3 /*break*/, 7];
dbg("add coupon to customer history");
return [4 /*yield*/, this.validate_coupon(options.coupon)];
case 5:
_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*/, async_utils_1.callback2(this.client.database.update_coupon_history, {
account_id: this.client.account_id,
coupon_history: coupon_history,
})];
case 6:
_b.sent();
_b.label = 7;
case 7: 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");
// This also returns the subscription, which lets
// us easily get the metadata of all projects associated to this subscription.
return [4 /*yield*/, this.conn.subscriptions.update(subscription_id, {
cancel_at_period_end: mesg.at_period_end,
})];
case 1:
// This also returns the subscription, which lets
// us easily get the metadata of all projects associated to this subscription.
_a.sent();
return [4 /*yield*/, this.update_database()];
case 2:
_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*/, this.conn.subscriptions.update(subscription_id, changes)];
case 1:
_b.sent();
return [4 /*yield*/, this.update_database()];
case 2:
_b.sent();
if (!(mesg.coupon_id != null)) return [3 /*break*/, 5];
return [4 /*yield*/, this.validate_coupon(mesg.coupon_id)];
case 3:
_a = _b.sent(), coupon = _a.coupon, coupon_history = _a.coupon_history;
coupon_history[coupon.id] += 1;
return [4 /*yield*/, async_utils_1.callback2(this.client.database.update_coupon_history, {
account_id: this.client.account_id,
coupon_history: coupon_history,
})];
case 4:
_b.sent();
_b.label = 5;
case 5: return [2 /*return*/];
}
});
});
};
StripeClient.prototype.mesg_get_subscriptions = function (mesg) {
return __awaiter(this, void 0, void 0, function () {
var dbg, customer_id, 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.need_customer_id()];
case 1:
customer_id = _a.sent();
return [4 /*yield*/, this.stripe_api_pager_options(mesg)];
case 2:
options = _a.sent();
options.status = "all";
options.customer = customer_id;
return [4 /*yield*/, this.conn.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=" + 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*/, this.conn.coupons.retrieve(coupon_id)];
case 1:
coupon = _a.sent();
dbg("check account coupon_history");
return [4 /*yield*/, async_utils_1.callback2(this.client.database.get_coupon_history, {
account_id: this.client.account_id,
})];
case 2:
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*/, this.conn.charges.list(options)];
case 2:
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*/, this.conn.invoices.list(options)];
case 2:
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, x, customer;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
dbg = this.dbg("mesg_admin_create_invoice_item");
return [4 /*yield*/, 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*/, 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 = stripe_name(r.first_name, r.last_name);
mesg.account_id = r.account_id;
if (!(customer_id != null)) return [3 /*break*/, 4];
dbg("already signed up for stripe -- sync local user account with stripe");
return [4 /*yield*/, async_utils_1.callback2(this.client.database.stripe_update_customer, {
account_id: mesg.account_id,
stripe: this.conn,
customer_id: customer_id,
})];
case 3:
_a.sent();
return [3 /*break*/, 7];
case 4:
dbg("create stripe entry for this customer");
x = {
description: description,
email: email,
metadata: {
account_id: mesg.account_id,
},
};
return [4 /*yield*/, this.conn.customers.create(x)];
case 5:
customer = _a.sent();
customer_id = customer.id;
dbg("store customer id in our database");
return [4 /*yield*/, async_utils_1.callback2(this.client.database.set_stripe_customer_id, {
account_id: mesg.account_id,
customer_id: customer_id,
})];
case 6:
_a.sent();
_a.label = 7;
case 7:
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*/, this.conn.invoiceItems.create({
customer: customer_id,
amount: mesg.amount * 100,
currency: "usd",
description: mesg.description,
})];
case 8:
_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*/, async_utils_1.callback2(this.client.database.get_user_project_upgrades, {
account_id: this.client.account_id,
})];
case 2:
projects = _b.sent();
_a = upgrades_1.available_upgrades(stripe_data, projects), excess = _a.excess, available = _a.available;
total = 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*/, 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*/];
}
});
});
};
return StripeClient;
}());
exports.StripeClient = StripeClient;
function stripe_name(first_name, last_name) {
return misc_1.trunc_middle((first_name !== null && first_name !== void 0 ? first_name : "") + " " + (last_name !== null && last_name !== void 0 ? last_name : ""), 200);
}
exports.stripe_name = stripe_name;
//# sourceMappingURL=client.js.map