0
votes

I am a newbie to all the technologies mentioned. So, I apologize in advance if this question shouldn't be here.

So I am trying to write the firestore equivalent of the example stripe-firebase realtime database-cloud functions integration provided by firebase. It is located here: https://github.com/firebase/functions-samples/blob/master/stripe/functions/index.js

I successfully converted the exports.createStripeCustomer function. But I am having trouble with addPaymentSource.

This is what I have so far:

  exports.addPaymentSource = functions.firestore.document('Users/{userId}/paymentSources/{paymentID}').onWrite((change, context) => {
  let newPaymentSource = change.after.data();
  let token = newPaymentSource.token;

  return functions.firestore.document(`Users/${context.params.userId}`).get('customer_data')
      .then((snapshot) => {
        return snapshot.val();
      }).then((customer) => {
        return stripe.customers.createSource(customer, {newPaymentSource});
      }).then((response) => {
        return change.after.ref.parent.set(response);
      }, (error) => {
        return change.after.ref.parent.child('error').set(userFacingMessage(error));
      }).then(() => {
        return reportError(error, {user: context.params.userId});
      });
 });

Deployment happens successfully, but the function fails with the following error.

TypeError: functions.firestore.document(...).get is not a function at exports.addPaymentSource.functions.firestore.document.onWrite (/user_code/index.js:73:77) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) at /var/tmp/worker/worker.js:728:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)

Clearly 'get' doesn't seem to be a function. I got this function from the following reference document. https://firebase.google.com/docs/reference/functions/functions.firestore.DocumentSnapshot

I seem to be missing something. I have been stuck on this for a couple of days. So, any help would be greatly appreciated. Thanks in advance.

2
Not very familiar with firestore but have you looked at these docs: firebase.google.com/docs/reference/js/… It seems that you may be able to get a ref to a location in your DB using something like firebase.database().ref(`Users/${context.params.userId}`) and then read the matching resource using .on().psmvac
Thanks for your response. I am unfortunately not using the realtime database :( I am trying to find the firestore equivalent of that function call pretty much.baxter
FYI: u can integrate w Stripe directly on the client (web sdk). The Admin SDK is not required at all. ...just making sure ur not over-complicating things... Stripe inserts an iFrame in your /pay page and hands u a token to store alongside the new order. It's PCI compliant.Ronnie Royston

2 Answers

0
votes

use firebase-admin

const admin = require('firebase-admin');
exports.addPaymentSource = functions.firestore.document('Users/{userId}/paymentSources/{paymentID}').onWrite((change, context) => {
  let newPaymentSource = change.after.data();
  let token = newPaymentSource.token;

  return admin.firestore().doc(`Users/${context.params.userId}`).get('customer_data')
      // what you have to do
      });
 });
0
votes

The functions package is used for triggering functions. You can use the following methods:

  • onCreate
  • onUpdate
  • onDelete
  • onWrite

(more here: https://firebase.google.com/docs/firestore/extend-with-functions)

To use the get() method, you'll want to use the firebase-admin package - you can then access firestore from admin.firestore()

(firebase-admin: https://www.npmjs.com/package/firebase-admin)