Created
June 19, 2020 14:54
-
-
Save SimonHoiberg/a36514e0c555718c81317701aff2309a 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 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