fleet/website/api/controllers/customers/get-stripe-checkout-session-url.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

70 lines
2.2 KiB
JavaScript
Vendored

module.exports = {
friendlyName: 'Get Stripe checkout session url',
description: 'Creates a Stripe checkout session for a new Fleet Premium subscription and returns the URL',
inputs: {
quoteId: {
type: 'number',
required: true,
description: 'The quote to use (determines the price and number of hosts.)'
},
},
exits: {
success: {
description: 'A Stripe checkout session was successfully created for a new Fleet Premium subscription.'
}
},
fn: async function (inputs) {
// Configure Stripe
const stripe = require('stripe')(sails.config.custom.stripeSecret);
// Find the quote record that was created.
let quoteRecord = await Quote.findOne({id: inputs.quoteId});
if(!quoteRecord) {
throw new Error(`Consistency violation: The specified quote (${inputs.quoteId}) no longer seems to exist.`);
}
// What if the stripe customer id doesn't already exist on the user?
if (!this.req.me.stripeCustomerId) {
throw new Error(`Consistency violation: The logged-in user's (${this.req.me.emailAddress}) Stripe customer id has somehow gone missing!`);
}
// Create a new Stripe checkout session for this subscription.
let stripeCheckoutSession = await stripe.checkout.sessions.create({
customer: this.req.me.stripeCustomerId,
customer_update: {// eslint-disable-line camelcase
name: 'auto',
address: 'auto',
},
success_url: `${sails.config.custom.baseUrl}/customers/dashboard?order-complete`,// eslint-disable-line camelcase
line_items: [// eslint-disable-line camelcase
{
price: sails.config.custom.stripeSubscriptionPriceId,
quantity: quoteRecord.numberOfHosts,
},
],
mode: 'subscription',
billing_address_collection: 'required',// eslint-disable-line camelcase
tax_id_collection: {// eslint-disable-line camelcase
enabled: true,
required: 'if_supported'
}
});
// Return the url of the Stripe checkout session.
// Users will be taken to this URL via the handleSubmitting function of the <ajax-form> on the /customers/new-license page.
return stripeCheckoutSession.url;
}
};