Last active
June 19, 2020 18:08
-
-
Save SimonHoiberg/2bbf21b640d3dedb1928e644992800ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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