fleet/website/api/controllers/customers/view-dashboard.js
Eric c53332259f
Website: Update license dispenser form (#23838)
Closes: https://github.com/fleetdm/confidential/issues/7696

Changes:
- Added `stripe` as a dependency
- Updated the license dispenser form to take users to a stripe hosted
checkout page where they can provide their billing address and Tax ID
depending on their location.
- Updated the receive-from-stripe webhook to fulfill license dispenser
purchases made via stripe checkout
- Added a new action: get-stripe-checkout-session-url. This action
creates a Stripe Checkout session and returns the URL
- Updated the customer dashboard to have a link that users can visit to
update their billing information, add more hosts to their Fleet premium
license, or cancel their subscription.
- Added a new action: redirect-to-stripe-billing-portal. An action that
redirects users to a Stripe-hosted billing portal.
2024-11-25 13:55:56 -06:00

74 lines
2.4 KiB
JavaScript
Vendored

module.exports = {
friendlyName: 'View dashboard',
description: 'Display "Dashboard" page.',
exits: {
success: {
viewTemplatePath: 'pages/customers/dashboard'
},
redirect: {
description: 'The requesting user does not have a subscription, redirecting to the new license page.',
responseType: 'redirect',
},
},
fn: async function () {
const stripe = require('stripe')(sails.config.custom.stripeSecret);
const today = Date.now();
const oneYearInMs = (1000 * 60 * 60 * 24 * 365);
const oneYearAgoAt = today - oneYearInMs;
const thirtyDaysAgoAt = today - (1000 * 60 * 60 * 24 * 30);
const thirtyDaysFromNowAt = today + (1000 * 60 * 60 * 24 * 30);
let subscriptionHasBeenRecentlyRenewed = false;
let subscriptionExpiresSoon = false;
let subscriptionIsExpired = false;
// Get subscription Info
let thisSubscription = await Subscription.findOne({user: this.req.me.id});
// If the user does not have a subscription, then help them subscribe.
if(!thisSubscription) {
throw {redirect: '/customers/new-license'};
}
let stripeSubscriptionDetails = await stripe.subscriptions.retrieve(thisSubscription.stripeSubscriptionId);
let willSubscriptionRenew = true;
if(stripeSubscriptionDetails.cancel_at_period_end === true){
willSubscriptionRenew = false;
}
// If this subscription is over a year old, and was renewed in the past 30 days set subscriptionHasBeenRecentlyRenewed to true.
if(thisSubscription.createdAt <= oneYearAgoAt && (thisSubscription.nextBillingAt - oneYearInMs) >= thirtyDaysAgoAt) {
subscriptionHasBeenRecentlyRenewed = true;
}
// If this subscription will renew in the next 30 days, set subscriptionExpiresSoon to true.
if(thisSubscription.nextBillingAt <= thirtyDaysFromNowAt && willSubscriptionRenew){
subscriptionExpiresSoon = true;
}
// If this subscription is expired, set subscriptionIsExpired to true.
if(thisSubscription.nextBillingAt <= Date.now()){
subscriptionIsExpired = true;
}
// Respond with view.
return {
stripePublishableKey: sails.config.custom.enableBillingFeatures ? sails.config.custom.stripePublishableKey : undefined,
thisSubscription,
subscriptionExpiresSoon,
subscriptionHasBeenRecentlyRenewed,
willSubscriptionRenew,
subscriptionIsExpired,
};
}
};