Developer
Developer

Reputation: 789

Customer is not created on stripe account

I am running this node.js code to create customer on stripe account function deploy successful but failed to create customer on stripe I am not getting what I am missing. Fire base functions folder is also not showing the function there.

const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    admin.initializeApp(functions.config().firebase);
    const stripe = require('stripe')("secret key here");
    var customer;

    stripe.customers.create(
      {
        email: '[email protected]',
      },
      {
        maxNetworkRetries: 2, 
      }
    );

Upvotes: 0

Views: 1821

Answers (1)

O. Jones
O. Jones

Reputation: 108641

When you use APIs to services (viz. stripe.com and firebase) outside your complete control, you must check for errors. If you're using a service incorrectly the error will explain, or at least hint, what you're doing wrong.

The stripe-node API documentation suggests you invoke stripe.customer.create() as an awaited function, like this:

const customer = await stripe.customers.create({
         email: '[email protected]',
});

This is easy if you call it from an async function. You should use this sort of code in your async function to check for errors back from stripe.com.

try {
   const customer = await stripe.customers.create({
         email: '[email protected]',
    });
    /* here's your customer object */
}
catch (error) {
    console.error ('stripe', error);
}

If you do not call it from an async function, you can wait for results using the Promises scheme.

    stripe.customers.create({
         email: '[email protected]',
       })
    .then ( function (customer) {
             /* here's your customer object */
       })
    .catch ( function (error) {
        console.error ('stripe', error);
       });

If you haven't yet figured out how async/await or Promises work, the time has come for you do to that.

Upvotes: 3

Related Questions