Skip to content

Commit

Permalink
billing: automated billing & pay as you go usage billing (#1773)
Browse files Browse the repository at this point in the history
* billing: overusage

* handle expired subscriptions and null usage

* get subscription status

* timestamp to millis

* 0 on null usage

* usage: handle null usage

* default billing usage

* null usage

* overusage

* overusage

* overusage

* overusage

* overusage

* billing: debug

* billing: debug

* billing: connected prod products

* billing: added migration apis

* stripe.js to stripe.ts & usage report

* allow staging plan select

* tmp subscription item

* tmp switch to product id

* tmp: fixed billing cycle for tests

* billing: calculate overusage into upcoming invoice

* billing: deprecated property on products

* billing: added pay as you go items to update subscription

* billing: bill at the end of the cycle

* billing: upcoming invoice

* billing: fix

* billing: fix

* billing: clear usage on update only when product is metered

* billing: pay as you go lookup keys

* billing: restore plan select

* billing: report usage for each subscription item correctly

* billing: retrieve upcoming invoices directly from stripe

* billing: upcoming invoice

* billing: added stripeCustomerId to upcoming invoice api

* billing: fix upcoming invoice request!

* billing: display stripe upcoming invoice total

* billing: return subscription items

* billing: display invoice breakdown in billing page

* billing: display invoice breakdown in billing page

* billing: display invoice breakdown in billing page

* billing: better upcoming invoice

* billing: billing page

* billing: show prices and limits on upcoming invoice table

* billing: better units display

* billing: fix position of monthly price

* billing: fix position of monthly price

* billing: fix position of monthly price

* billing: address some ui comments

* billing: address some ui comments

* billing: address some ui comments

* billing: address some ui comments

* billing: address some ui comments

* billing: address some ui comments

* billing: reset billing cycle on subscription update

* billing: empty usage

* billing: empy usage

* billing: debug empty usage

* usage: report usage cron

* usage: fixed billing cycle for report usage testing

* usage: fixed pay as you go plans for staging

* usage: fix report usage query

* usage: fix report query

* usage: report usage fix

* usage: added ingest origin for billing usage call

* usage: report usage subscription items

* usage: report usage cron

* usage: report usage cron response

* usage: report quantity of usage as number

* usage: fix report timestamp & response

* usage: report usage response

* usage: cron: use api token for billing usage call

* usage: fix name of delivery product for usage report

* usage: remove test timestamps & update migration api to work in batches

* billing: migrate test products users to prod, in batches

* billing: migrate personal users to hacker api fix

* billing: do not migrate users with a canceled or inactive subscription

* billing: check if subscription exists on migrate api

* billing: added subscription items to migration sub update

* billing: added api to migrate Pro users to hacker plan + pay as you go

* billing: avoid migration apis to run forever

* billing: modify the migration apis to address review comments

* billing: skip user migration when subscription is canceled or missing

* billing: keep track of inactive subscriptions

* billing: fix query boolean

* billing: fix user check

* billing: added migration api to revert users to personal for testing

* billing: select null active subscription as well on migration

* billing: don't migrate if stripe customer is missing

* billing: added migrations log

* billing: exclude june users from personal migration

* billing: fix query

* fix conflicts

* billing: update pay as you go prices

* usage: restore charts
  • Loading branch information
gioelecerati authored Jul 31, 2023
1 parent 7465f7d commit 6a2da7f
Show file tree
Hide file tree
Showing 18 changed files with 1,755 additions and 367 deletions.
173 changes: 163 additions & 10 deletions packages/api/src/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
exports.products = {
prod_0: {
deprecated: true,
order: 0,
name: "Personal",
lookupKeys: ["price_0"],
Expand All @@ -8,22 +9,52 @@ exports.products = {
name: "Transcoding",
description: "Transcoding (minutes)",
price: 0.0,
limit: 1_000,
},
{
name: "Delivery",
description: "Delivery (minutes)",
price: 0.0,
limit: 1_000,
},
{
name: "Storage",
description: "Storage (minutes)",
price: 0.0,
limit: 1_000,
},
],
monthlyPrice: 0,
},
prod_1: {
deprecated: true,
order: 1,
name: "Pro",
lookupKeys: ["price_1"],
usage: [
{
name: "Transcoding",
description: "Transcoding (minutes)",
price: 0.005,
price: 0.0055,
limit: 3_000,
},
{
name: "Delivery",
description: "Delivery (minutes)",
price: 0.0005,
limit: 100_000,
},
{
name: "Storage",
description: "Storage (minutes)",
price: 0.0035,
limit: 10_000,
},
],
monthlyPrice: 0,
},
prod_2: {
deprecated: true,
order: 2,
name: "Business",
lookupKeys: ["price_2"],
Expand All @@ -34,28 +65,34 @@ exports.products = {
price: 0.0,
},
],
monthlyPrice: 0,
},
hacker_1: {
order: 3,
name: "Hacker",
lookupKeys: ["hacker_1"],
price: 0,
usage: [
{
name: "Transcoding",
description: "Transcoding (minutes)",
price: 0,
limit: 1_000,
},
{
name: "Delivery",
description: "Delivery (minutes)",
price: 0,
limit: 1_000,
},
{
name: "Storage",
description: "Storage (minutes)",
price: 0,
limit: 1_000,
},
],
monthlyPrice: 0,
},
growth_1: {
order: 4,
Expand All @@ -65,19 +102,24 @@ exports.products = {
{
name: "Transcoding",
description: "Transcoding (minutes)",
price: 0,
price: 0.0055,
limit: 3_000,
},
{
name: "Delivery",
description: "Delivery (minutes)",
price: 0,
price: 0.0005,
limit: 100_000,
},
{
name: "Storage",
description: "Storage (minutes)",
price: 0,
price: 0.0035,
limit: 10_000,
},
],
monthlyPrice: 100,
payAsYouGo: true,
},
scale_1: {
order: 5,
Expand All @@ -87,19 +129,24 @@ exports.products = {
{
name: "Transcoding",
description: "Transcoding (minutes)",
price: 0,
price: 0.0055,
limit: 20_000,
},
{
name: "Delivery",
description: "Delivery (minutes)",
price: 0,
price: 0.0005,
limit: 500_000,
},
{
name: "Storage",
description: "Storage (minutes)",
price: 0,
price: 0.0035,
limit: 50_000,
},
],
monthlyPrice: 500,
payAsYouGo: true,
},
pay_as_you_go_1: {
order: 6,
Expand All @@ -109,21 +156,23 @@ exports.products = {
{
name: "Transcoding",
description: "Transcoding (minutes)",
price: 0.005,
price: 0.0055,
},
{
name: "Delivery",
description: "Delivery (minutes)",
price: 0.0004,
price: 0.0005,
},
{
name: "Storage",
description: "Storage (minutes)",
price: 0.003,
price: 0.0035,
},
],
monthlyPrice: 0,
},
prod_4: {
deprecated: true,
order: 7,
name: "Enterprise",
lookupKeys: ["enterprise_1"],
Expand All @@ -144,5 +193,109 @@ exports.products = {
price: 0,
},
],
monthlyPrice: 0,
},
prod_O9XuIjn7EqYRVW: {
order: 8,
name: "Hacker",
lookupKeys: ["hacker_1"],
usage: [
{
name: "Transcoding",
description: "Transcoding (minutes)",
price: 0,
limit: 1_000,
},
{
name: "Delivery",
description: "Delivery (minutes)",
price: 0,
limit: 1_000,
},
{
name: "Storage",
description: "Storage (minutes)",
price: 0,
limit: 1_000,
},
],
monthlyPrice: 0,
},
prod_O9XtHhI6rbTT1B: {
order: 9,
name: "Growth",
lookupKeys: ["growth_1"],
usage: [
{
name: "Transcoding",
description: "Transcoding (minutes)",
price: 0.0055,
limit: 3_000,
},
{
name: "Delivery",
description: "Delivery (minutes)",
price: 0.0005,
limit: 100_000,
},
{
name: "Storage",
description: "Storage (minutes)",
price: 0.0035,
limit: 10_000,
},
],
monthlyPrice: 100,
payAsYouGo: true,
},
prod_O9XtcfOSMjSD5L: {
order: 10,
name: "Scale",
lookupKeys: ["scale_1"],
usage: [
{
name: "Transcoding",
description: "Transcoding (minutes)",
price: 0.0055,
limit: 20_000,
},
{
name: "Delivery",
description: "Delivery (minutes)",
price: 0.0005,
limit: 500_000,
},
{
name: "Storage",
description: "Storage (minutes)",
price: 0.0035,
limit: 50_000,
},
],
monthlyPrice: 500,
payAsYouGo: true,
},
prod_O9XuWMU1Up6QKf: {
order: 11,
name: "Pay-As-You-Go",
lookupKeys: ["transcoding_usage", "tstreaming_usage", "tstorage_usage"],
usage: [
{
name: "Transcoding",
description: "Transcoding (minutes)",
price: 0.0055,
},
{
name: "Delivery",
description: "Delivery (minutes)",
price: 0.0005,
},
{
name: "Storage",
description: "Storage (minutes)",
price: 0.0035,
},
],
monthlyPrice: 0,
},
};
23 changes: 23 additions & 0 deletions packages/api/src/controllers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,29 @@ export async function sendgridEmail({
await SendgridMail.send(msg);
}

export async function sendgridEmailPaymentFailed({
email,
sendgridApiKey,
userId,
invoiceId,
}) {
const [supportName, supportEmail] = email;
const msg = {
text: `User ${userId} failed to pay invoice ${invoiceId}`,
from: {
email: supportEmail,
name: supportName,
},
reply_to: {
email: supportEmail,
name: supportName,
},
};

SendgridMail.setApiKey(sendgridApiKey);
await SendgridMail.send(msg);
}

export function sendgridValidateEmail(email: string, validationApiKey: string) {
if (!validationApiKey) {
return;
Expand Down
Loading

0 comments on commit 6a2da7f

Please sign in to comment.