0
votes

Hello guys I am new to Stripe, so I am having a question about how to create stripe customer through firebase cloud function. I have been read through the stripe standard integration and few tutorials. This example teaches you how to set up the firebase cloud function. The problem is that the example creates customer whenever the charge is being processed or the amount is entered. Stripe documentation says that it is fine to create the customer user without payment information. So my thought is whenever i create user for firebase, I trigger cloud function to create the stripe customer at the same time. Can anyone teach me how to do that exactly, and Also show me how to update the payment information bind to that customer. Thank you very much.

1

1 Answers

3
votes

This is actually a 2 part question, but I think it's a lot easier than you think. Here is some guidance on how to solve the problem in typescript.

Create the customer

To create the customer, through an create-auth-trigger do the following:

export const syncUserToStripe = functions.auth.user().onCreate(async (data, context) => 
  const stripe = new Stripe(<stripe-token>); // Initialize the stripe SDK
  const stripeCustomer = await stripe.customers.create({
        email: data.email
  }); // Now you have the stripe customer. Maybe you would like to save the stripeCustomer Id to database/firestore
  console.log(`Done syncing firestore user with id: ${data.uid} to Stripe. The stripe id is ${stripeCustomer.id}`);
);

Update payment information

The update payment is a two step rocket. First you need to collect the stripe token from your client. This is most easily obtained with something like checkout.jsfrom stripe (let's you securly collect the credit card). The actual implementation is pretty easy, but various depending on your front-end framework. The important part is that once you have a token, you can update the payment info on your backend, i.e. a cloud https function (you call this function, when you have the stripe token) The important part of the code could look like this:

export async function updateStripePayment(req: Request, res: Response): Promise<any> {
 const stripe = new Stripe(<stripe-token>); // Initialize the stripe SDK
 // Extract the stripe token from the header
 const stripeToken = req.header('StripeToken');
 // You also need to get the stripe customer id. Here I will get it from the header, but maybe it makes more sense for you to read it from firestore/realtime db.
 const stripeCustomerId = req.header('stripeCustomerId');
 // Now you can create a new payment source
 const newSource = await stripeAPI.customers.createSource(stripeCustomerId, {source: stripeToken});
 // And you can now optionally set it as default
 await stripeAPI.customers.update(stripeCustomerId, {default_source: newSource.id});
 res.status(200).json(`Sucessfully updated stripe payment info`);
}

General considerations

  • Consider what information you would like to store in you backend
  • When updating payment info, consider using express combined with middleware to authenticate/extract relevant info (so you can reuse code)
  • Use try catch on your code, to catch unexpected errors
  • Remember to do everything with stripe test keys. You can use firebase functions config environment to help with this on the backend.