Skip to content

Instantly share code, notes, and snippets.

View SimonHoiberg's full-sized avatar

Simon Høiberg SimonHoiberg

View GitHub Profile
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.');
}
class StripeManager {
...
public static async getStripeCustomerID() {
// Retrieve the current customerID from the currently logged in user
// getUserFromDB() is *your* implemention of getting user info from the DB
const { customerID } = getUserFromDB();
if (!customerID) {
const customer = await this.createCustomer();
export interface IStripeSubscription {
id: string;
object: string;
application_fee_percent?: any;
billing_cycle_anchor: number;
billing_thresholds?: any;
cancel_at?: any;
cancel_at_period_end: boolean;
canceled_at?: any;
collection_method: string;
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,
}),
class StripeManager {
...
public static async handleSubscription(subscriptionID: string, end: boolean) {
const request = await fetch('https://your-endpoint/stripe/handle-subscription', {
method: 'POST',
body: JSON.stringify({
subscriptionID,
end,
class StripeManager {
...
public static async retrieveSubscription(subscriptionID: string) {
const request = await fetch('https://your-endpoint/stripe/retrieve-subscription', {
method: 'POST',
body: JSON.stringify({
subscriptionID,
}),
});
class StripeManager {
...
public static async updatePaymentMethod(customerID: string, paymentMethodID: string) {
await fetch('https://your-endpoint/stripe/update-payment-method', {
method: 'POST',
body: JSON.stringify({
customerID,
paymentMethodID,
}),
class StripeManager {
...
public static async retreivePaymentInfo(paymentMethodID: string) {
try {
const request = await fetch(
'https://your-endpoint/stripe/retrieve-payment-method',
{
method: 'POST',
body: JSON.stringify({
class StripeManager {
...
public static async retryInvoice(customerID: string, paymentMethodID: string, invoiceID: string) {
const request = await fetch('https://your-endpoint/stripe/retry-invoice', {
method: 'POST',
body: JSON.stringify({
customerID,
paymentMethodID,
invoiceID,
import { loadStripe } from '@stripe/stripe-js';
import {
CardNumberElement,
CardExpiryElement,
CardCvcElement,
Elements,
useStripe,
useElements,
} from '@stripe/react-stripe-js';