Skip to content

Instantly share code, notes, and snippets.

@SimonHoiberg
Created June 19, 2020 14:54
Show Gist options
  • Save SimonHoiberg/a36514e0c555718c81317701aff2309a to your computer and use it in GitHub Desktop.
Save SimonHoiberg/a36514e0c555718c81317701aff2309a to your computer and use it in GitHub Desktop.
class StripeManager {
...
public static async createSubscription(customerID: string, paymentMethodID: string) {
const request = await fetch('https://your-endpoint/stripe/create-subscription', {
method: 'POST',
body: JSON.stringify({
customerID,
paymentMethodID,
}),
});
const subscription = await request.json() as IStripeSubscription;
if (subscription.status !== 'active') {
throw Error('Unable to upgrade. Please try again');
}
if (subscription.latest_invoice.payment_intent.status === 'requires_payment_method') {
throw Error('You credit card was declined. Please try again with another card.');
}
// Update your user in DB to store the subscriptionID and enable paid plan
// updateUserInDB() is *your* implementation of updating a user in the DB
updateUserInDB({
paymentMethodID,
hasPaidPlan: true,
subscriptionID: subscription.id,
});
return subscription;
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment