Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stripe firebase functions to set default payment

I am trying to set the last card added to the stripe as the default via firebase functions though I can't seem to get it to work.

// Add a payment source (card) for a user by writing a stripe payment source token to Realtime database
exports.addPaymentSource = functions.database.ref('/users/{userId}/sources/{pushId}/token').onWrite(event => {
  const source = event.data.val();
  if (source === null) return null;
  return admin.database().ref(`/users/${event.params.userId}/customer_id`).once('value').then(snapshot => {
    return snapshot.val();
  }).then(customer => {
    return stripe.customers.createSource(customer, {source});
    return stripe.customers.update(customer, {default_source: source});
  }).then(response => {
      return event.data.adminRef.parent.set(response);
    }, error => {
      return event.data.adminRef.parent.child('error').set(userFacingMessage(error)).then(() => {
        // return reportError(error, {user: event.params.userId});
        consolg.log(error, {user: event.params.userId});
      });
  });
});
like image 962
Boss Nass Avatar asked Jul 30 '17 12:07

Boss Nass


People also ask

Can Firebase handle payments?

Stay organized with collections Save and categorize content based on your preferences. Using a few different Firebase features and Stripe, you can process payments in your web app without building your own server infrastructure.

What are Firebase cloud functions?

Cloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your JavaScript or TypeScript code is stored in Google's cloud and runs in a managed environment.

Does Firebase require credit?

Firebase also allows free setup with no credit card. The Spark plan. You don't get a "server" but you can run cloud functions for free. The Cloud functions do have some limitations.

Is Firebase free to use?

Firebase offers a no-cost tier pricing plan for all its products. For some products, usage continues at no cost no matter your level of use. For other products, if you need high levels of use, you'll need to switch your project to a paid-tier pricing plan. Learn more about Firebase pricing plans.


1 Answers

You're trying to return two things in this one function. That isn't going to work. It should create the source, but it won't update it.

return stripe.customers.createSource(customer, {source});
return stripe.customers.update(customer, {default_source: source});
like image 186
Notmfb Avatar answered Sep 29 '22 20:09

Notmfb