Skip to content

Instantly share code, notes, and snippets.

@nwaughachukwuma
Last active September 29, 2021 18:21
Show Gist options
  • Save nwaughachukwuma/98704cd2754bfa853ec8bf70717dd8d1 to your computer and use it in GitHub Desktop.
Save nwaughachukwuma/98704cd2754bfa853ec8bf70717dd8d1 to your computer and use it in GitHub Desktop.
Create stripe customer and store on user document
/**
* Create your stripe customer
*/
import * as functions from 'firebase-functions';
import { DocumentSnapshot } from "firebase-functions/lib/providers/firestore";
import * as Stripe from "stripe"
const secretKey = functions.config().stripe.key // ensure you have set the configuration
const stripe = new Stripe(secretKey)
export async function createStripeCustomer(snap: DocumentSnapshot) {
const customer = {
email: snap.get('email'),
phone: snap.get('phoneNumber'),
name: snap.get('displayName')
}
const stripeCustomer = await createCustomer(customer)
if (stripeCustomer) {
const stripeInfo = {
customerId: stripeCustomer.id
}
try {
await snap.ref.update({ stripeInfo })
} catch (err) {
console.error(err)
}
}
}
interface Customer {
email: string
displayName: string
phone: string
description?: string
metadata?: any
}
async function createCustomer(customer: Customer) {
let cust: any = null
try {
cust = await stripe.customers.create(customer)
} catch (err) {
throw err
}
return cust
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment