Last active
May 5, 2019 20:55
-
-
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
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
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