1

I've got a firebase function that calls the Stripe API to create a user, which works fine. However, I can't get a valid response back from the function in my Flutter app. Whatever I try, the response data is always null, even when the function returns a response, not an error. This code is pretty much the same as the example form the docs. Can anyone spot where I' going wrong?

The function

exports.stripeSignup = functions.https.onCall(async (data, context) => {
  const uid = context.auth.uid;
  console.log(`Received data ${JSON.stringify(data)}`);

  stripe.accounts.create({
    type: 'custom',
    business_type: 'individual',
    individual: {
      email: data.email,
      first_name: data.first_name,
      last_name: data.last_name,
    },
    country: 'GB',
    email: data.email,
    requested_capabilities: ['card_payments', 'transfers']
  })
    .then((account) => {
      console.log(`Stripe account: ${account.id}`);
      return {
        stripeId: account.id
      };
    })
    .catch((err) => {
      console.log(`Err: ${JSON.stringify(err)}`);
      return null;
    });
});
final HttpsCallable callable =
        CloudFunctions.instance.getHttpsCallable(functionName: 'stripeSignup');

    try {
      final HttpsCallableResult res = await callable.call(<String, dynamic>{
        'first_name': firstname,
        'last_name': lastname,
        'dob': dob,
        'email': email,
        'phone': phone,
      });

      print('Stripe response - ${res.data}');
      String stripeId = res.data['stripeId'];
      return stripeId;
    } on CloudFunctionsException catch (e) {
      return null;
    }

1 Answer 1

3

I think that you should return your Promises chain, as follows:

exports.stripeSignup = functions.https.onCall(async (data, context) => {
  const uid = context.auth.uid;
  console.log(`Received data ${JSON.stringify(data)}`);

  return stripe.accounts.create({    // <--  Note the return here
    type: 'custom',
    business_type: 'individual',
    individual: {
      email: data.email,
      first_name: data.first_name,
      last_name: data.last_name,
    },
    country: 'GB',
    email: data.email,
    requested_capabilities: ['card_payments', 'transfers']
  })
  .then((account) => {
    console.log(`Stripe account: ${account.id}`);
    return {
      stripeId: account.id
    };
  })
  .catch((err) => {
    // Here you should handle errors as explained in the doc
    // https://firebase.google.com/docs/functions/callable#handle_errors
    console.log(`Err: ${JSON.stringify(err)}`);
    return null;
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.