Website: Update signup error handling when there is a Stripe related error. (#10042)
Closes: https://github.com/fleetdm/confidential/issues/1748 Changes: - Added an error that is thrown if a Stripe customer cannot be created for a new user. - Updated `signup.js` to create Stripe customers for new users before the new record is saved in the database. This helps prevent situations where users are shown an error message when they submit the signup form, but their account was actually created (without a `stripeCustomerId`).
This commit is contained in:
+16
-11
@@ -105,6 +105,21 @@ the account verification message.)`,
|
||||
throw 'emailAlreadyInUse';
|
||||
}
|
||||
|
||||
|
||||
if (!sails.config.custom.enableBillingFeatures) {
|
||||
throw new Error('The Stripe configuration variables (sails.config.custom.stripePublishableKey and sails.config.custom.stripeSecret) are missing!');
|
||||
}
|
||||
|
||||
// Create a new customer entry in the Stripe API for this user before we send a request to the cloud provisioner.
|
||||
let stripeCustomerId = await sails.helpers.stripe.saveBillingInfo.with({
|
||||
emailAddress: newEmailAddress
|
||||
})
|
||||
.timeout(5000)
|
||||
.retry()
|
||||
.intercept((error)=>{
|
||||
return new Error(`An error occurred when trying to create a Stripe Customer for a new user with the using the email address ${newEmailAddress}. The incomplete user record has not been saved in the database, and the user will be asked to try signing up again. Full error: ${error.raw}`);
|
||||
});
|
||||
|
||||
// Provisioning a Fleet sandbox instance for the new user. Note: Because this is the only place where we provision Sandbox instances, We'll provision a Sandbox instance BEFORE
|
||||
// creating the new User record. This way, if this fails, we won't save the new record to the database, and the user will see an error on the signup form asking them to try again.
|
||||
|
||||
@@ -177,6 +192,7 @@ the account verification message.)`,
|
||||
fleetSandboxURL: cloudProvisionerResponseData.URL,
|
||||
fleetSandboxExpiresAt,
|
||||
fleetSandboxDemoKey,
|
||||
stripeCustomerId,
|
||||
tosAcceptedByIp: this.req.ip
|
||||
}, sails.config.custom.verifyEmailAddresses? {
|
||||
emailProofToken: await sails.helpers.strings.random('url-friendly'),
|
||||
@@ -205,17 +221,6 @@ the account verification message.)`,
|
||||
sails.log.warn(`When a new user signed up, a lead/contact could not be verified in the CRM for this email address: ${newEmailAddress}. Raw error: ${err}`);
|
||||
return;
|
||||
});
|
||||
// If billing feaures are enabled, save a new customer entry in the Stripe API.
|
||||
// Then persist the Stripe customer id in the database.
|
||||
if (sails.config.custom.enableBillingFeatures) {
|
||||
let stripeCustomerId = await sails.helpers.stripe.saveBillingInfo.with({
|
||||
emailAddress: newEmailAddress
|
||||
}).timeout(5000).retry();
|
||||
await User.updateOne({id: newUserRecord.id})
|
||||
.set({
|
||||
stripeCustomerId
|
||||
});
|
||||
}
|
||||
// Store the user's new id in their session.
|
||||
this.req.session.userId = newUserRecord.id;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user