Nothing Special   »   [go: up one dir, main page]

Skip to content

Instantly share code, notes, and snippets.

@anshulxyz
Forked from vktr/rule.js
Last active May 7, 2022 15:48
Show Gist options
  • Save anshulxyz/10b0ba1cf091eca17376ab3c8797aae0 to your computer and use it in GitHub Desktop.
Save anshulxyz/10b0ba1cf091eca17376ab3c8797aae0 to your computer and use it in GitHub Desktop.
Add Stripe Customer Id to Auth0 user's metadata, and Auth0 id to Stripe Customer's metadata via custom rule
function (user, context, callback) {
user.app_metadata = user.app_metadata || {};
// Checking if the Auth0 User already has a stripe customer id with it's metadata
if ('stripe_customer_id' in user.app_metadata) {
console.log(user);
return callback(null, user, context);
}
var stripe = require('stripe')('sk_test_tyd8...');
// Dict for creating Stripe Customer, with Auth0 User id
var customer = {
name: user.name,
email: user.email,
metadata: {
'auth0_user_id': user.user_id,
},
};
stripe.customers.create(customer, function (err, customer) {
if (err) {
return callback(err);
}
user.app_metadata.stripe_customer_id = customer.id;
auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
.then(function () {
console.log(user);
callback(null, user, context);
})
.catch(function (err) {
callback(err);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment