stripe firebase functions to set default payment

Viewed 377

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});
      });
  });
});
1 Answers
Related