Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
1.3 kB
20
Indexable
const stripe = require('stripe')('your_stripe_secret_key_here');
const { WebClient } = require('@slack/web-api');

const slackToken = 'your_slack_bot_oauth_token_here';
const channelId = 'your_slack_channel_id_here';
const web = new WebClient(slackToken);

async function calculateMrr() {
    const subscriptions = await stripe.subscriptions.list({status: 'active'});
    let mrr = 0.0;

    for (let subscription of subscriptions.data) {
        if (subscription.plan.interval === 'month') {
            mrr += (subscription.plan.amount / 100.0);
        } else if (subscription.plan.interval === 'year') {
            mrr += (subscription.plan.amount / 100.0) / 12.0;
        } else if (subscription.plan.interval === 'week') {
            mrr += (subscription.plan.amount / 100.0) * 4.0;
        } else if (subscription.plan.interval === 'day') {
            mrr += (subscription.plan.amount / 100.0) * 30.0;
        }
    }

    return mrr;
}

async function postMrrToSlack() {
    const mrr = await calculateMrr();
    const message = `Today's Monthly Recurring Revenue: $${mrr}`;

    try {
        await web.chat.postMessage({ channel: channelId, text: message });
    } catch (error) {
        console.error(error);
    }
}

postMrrToSlack();