Skip to content

Instantly share code, notes, and snippets.

@SimonHoiberg
Last active June 19, 2020 18:08
Show Gist options
  • Save SimonHoiberg/2bbf21b640d3dedb1928e644992800ce to your computer and use it in GitHub Desktop.
Save SimonHoiberg/2bbf21b640d3dedb1928e644992800ce to your computer and use it in GitHub Desktop.
class StripeManager {
public static async createCustomer() {
try {
// Retrieve email and username of the currently logged in user.
// getUserFromDB() is *your* implemention of getting user info from the DB
const { email, username } = getUserFromDB();
if (!email || !username) {
throw Error('Email or username not found.');
}
const request = await fetch('https://your-endpoint/stripe/create-customer', {
method: 'POST',
body: JSON.stringify({
email,
username,
}),
});
const customer = (await request.json()) as IStripeCustomer;
// Update your user in DB to store the customerID
// updateUserInDB() is *your* implementation of updating a user in the DB
updateUserInDB({ customerID: customer.id });
return customer;
} catch (error) {
console.log('Failed to create customer');
console.log(error);
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment