Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/client/stripe.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6stripe payments api via backend hub7*/89import { callback2 } from "@cocalc/util/async-utils";10import * as message from "@cocalc/util/message";11import { PurchaseInfo } from "@cocalc/util/licenses/purchase/types";12import { HubClient } from "./hub";1314export class StripeClient {15private call_api: typeof HubClient.prototype.call;1617constructor(call_api) {18this.call_api = call_api;19}2021private async call(mesg): Promise<any> {22return await callback2(this.call_api, {23message: mesg,24error_event: true,25timeout: 15,26});27}2829// gets custormer info (if any) and stripe public api key30// for this user, if they are logged in31public async get_customer(): Promise<{32stripe_publishable_key?: string;33customer: any;34}> {35const mesg = await this.call(message.stripe_get_customer());36if (mesg == null) {37// evidently this happened -- see38// https://github.com/sagemathinc/cocalc/issues/371139throw Error("mesg must be defined");40} else {41return {42stripe_publishable_key: mesg.stripe_publishable_key,43customer: mesg.customer,44};45}46}4748public async create_source(token: string): Promise<any> {49return await this.call(message.stripe_create_source({ token }));50}5152public async delete_source(card_id: string): Promise<any> {53return await this.call(message.stripe_delete_source({ card_id }));54}5556public async update_source(card_id: string, info: any): Promise<any> {57return await this.call(message.stripe_update_source({ card_id, info }));58}5960public async set_default_source(card_id: string): Promise<any> {61return await this.call(message.stripe_set_default_source({ card_id }));62}6364// gets list of past stripe charges for this account.65public async get_charges(opts: {66limit?: number; // between 1 and 100 (default: 10)67ending_before?: any;68starting_after?: any;69}): Promise<any> {70return (71await this.call(72message.stripe_get_charges({73limit: opts.limit,74ending_before: opts.ending_before,75starting_after: opts.starting_after,76})77)78).charges;79}8081public async create_subscription(opts: {82plan: string;83quantity?: number;84coupon_id?: string;85}): Promise<any> {86if (opts.quantity == null) {87opts.quantity = 1;88}89return await this.call(message.stripe_create_subscription(opts));90}9192public async cancel_subscription(opts: {93subscription_id: string;94at_period_end?: boolean; // default is *true*95}) {96if (opts.at_period_end == null) {97opts.at_period_end = true;98}99return await this.call(message.stripe_cancel_subscription(opts));100}101102public async update_subscription(opts: {103subscription_id: string;104quantity?: number; // if given, must be >= number of projects105coupon_id?: string;106projects?: string[]; // ids of projects that subscription applies to (TOD: what?)107plan?: string;108}) {109return await this.call(message.stripe_update_subscription(opts));110}111112// gets list of past stripe charges for this account.113public async get_subscriptions(opts: {114limit?: number; // between 1 and 100 (default: 10)115ending_before?: any; // see https://stripe.com/docs/api/node#list_subscriptions116starting_after?: any;117}) {118return (await this.call(message.stripe_get_subscriptions(opts)))119.subscriptions;120}121122// Gets the coupon for this account. Returns an error if invalid123// https://stripe.com/docs/api#retrieve_coupon124public async get_coupon(coupon_id: string) {125return (await this.call(message.stripe_get_coupon({ coupon_id }))).coupon;126}127128// gets list of invoices for this account.129public async get_invoices(opts: {130limit?: number; // between 1 and 100 (default: 10)131ending_before?: any; // see https://stripe.com/docs/api/node#list_charges132starting_after?: any;133}) {134return (135await this.call(136message.stripe_get_invoices({137limit: opts.limit,138ending_before: opts.ending_before,139starting_after: opts.starting_after,140})141)142).invoices;143}144145public async admin_create_invoice_item(146opts:147| {148account_id: string;149email_address?: string;150amount?: number; // in US dollars -- if amount/description *not* given, then merely ensures user has stripe account and updats info about them151description?: string;152}153| {154account_id?: string;155email_address: string;156amount?: number;157description?: string;158}159) {160return await this.call(message.stripe_admin_create_invoice_item(opts));161}162163// Make it so the SMC user with the given email address has a corresponding stripe164// identity, even if they have never entered a credit card. May only be used by165// admin users.166public async admin_create_customer(167opts:168| { account_id: string; email_address?: string }169| { account_id?: string; email_address: string }170) {171return await this.admin_create_invoice_item(opts);172}173174// Purchase a license (technically this may or may not involve "stripe").175public async purchase_license(info: PurchaseInfo): Promise<string> {176return (await this.call(message.purchase_license({ info }))).resp;177}178179}180181182