I would like to create a stripe account during the user registration in meteor and adjusted Accounts.onCreateUser for that purpose with a promise.
Accounts.onCreateUser((options, user) => {
if (user.services.facebook) {
const { first_name, last_name, email } = user.services.facebook;
user.profile = {}
user.profile.first_name = first_name
user.profile.last_name = last_name
}
else{
user.profile = options.profile
}
user.stripe = {}
return new Promise((resolve,reject) => {
stripe.customers.create({
description: user.profile.first_name + ' ' + user.profile.last_name
},function(err,response){
if (!err) {
user.stripe.id = response.id
resolve(user);
} else {
reject('Could not create user');
}
});
})
});
While the user gets properly created in stripe, the user document in the meteor mongo database only contains the userid but no other field.
Am I using the promise wrong? Any help would be appreciated!
Because onCreateUser
runs on the server, we can wrap the Stripe call in a Fiber using Meteor.wrapAsync
.
Fibers allow async code to run as though it was synchronous, but only on the server. (Here's a great presentation on what Fibers are and why Meteor uses them)
With wrapAsync
the code looks like this:
Accounts.onCreateUser((options, user) => {
if (user.services.facebook) {
const { first_name, last_name, email } = user.services.facebook;
user.profile = {}
user.profile.first_name = first_name
user.profile.last_name = last_name
} else {
user.profile = options.profile
}
user.stripe = {};
const createStripeCustomer = Meteor.wrapAsync(stripe.customers.create,stripe.customers);
const response = createStripeCustomer({
description: user.profile.first_name + ' ' + user.profile.last_name
});
user.stripe.id = response.id
return user;
});