Skip to content

Instantly share code, notes, and snippets.

@LukeMwila
Last active May 5, 2019 20:55
Show Gist options
  • Save LukeMwila/d28aebe7ed67781347b70cf7b575b75c to your computer and use it in GitHub Desktop.
Save LukeMwila/d28aebe7ed67781347b70cf7b575b75c to your computer and use it in GitHub Desktop.
Function that creates customer account in Stripe and subscribes them to a product plan
import * as Stripe from "stripe";
/** Config */
const stripe = new Stripe('STRIPE_API_KEY'); // Stripe account secret key goes here
export async function createCustomerAndSubscribeToPlan(
stripeToken: string,
email: string,
productPlan: string
): Promise<any> {
// create a customer
const customer = await stripe.customers.create({
email,
source: stripeToken
});
// retrieve created customer id to add customer to subscription plan
const customerId = customer.id;
// create a subscription for the newly created customer
const subscription = await stripe.subscriptions.create({
customer: customerId,
items: [{ plan: productPlan }]
});
return subscription;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment