条带创建用户firebase云功能

时间:2018-03-11 23:06:17

标签: ios firebase firebase-realtime-database reference stripe-payments

我正在尝试在为firebase创建用户时创建一个条带用户,我一直收到此错误(下面显示错误)。该功能的代码也显示如下。如果我需要发布数据库结构,我将这样做,我目前没有条带客户的任何结构(这可能是问题发生的地方)。如果有人可以提供帮助我会非常感激。

错误:

Error: Reference.child failed: First argument was an invalid path = "/stripe_customers/${data.uid}/customer_id". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
at Object.exports.validatePathString (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/core/util/validation.js:282:15)
at Object.exports.validateRootPathString (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/core/util/validation.js:293:13)
at Reference.child (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/api/Reference.js:72:30)
at Database.ref (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/cjs/src/api/Database.js:60:54)
at stripe.customers.create.then (/user_code/index.js:41:29)
at tryCatcher (/user_code/node_modules/stripe/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/user_code/node_modules/stripe/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/user_code/node_modules/stripe/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/user_code/node_modules/stripe/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/user_code/node_modules/stripe/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/user_code/node_modules/stripe/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/user_code/node_modules/stripe/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/user_code/node_modules/stripe/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:672:20)
at tryOnImmediate (timers.js:645:5)
at processImmediate [as _immediateCallback] (timers.js:617:5)

功能:

'use strict';


const functions = require('firebase-functions');
const admin = require('firebase-admin');
const logging = require('@google-cloud/logging')();

admin.initializeApp(functions.config().firebase);

const stripe = require('stripe')(functions.config().stripe.token);
const currency = functions.config().stripe.currency || 'USD';

//[START chargecustomer]
//charge the stripe customer whenever an amount is written to the realtime       database
exports.createStripeCharge = functions.database.ref('/stripe_customers/{userId}/charges/{id}').onWrite((event) => {
  const val = event.data.val();

  if (val === null || val.id || val.error) return null;

  return     admin.database().ref(`/stripe_customers/${event.params.userId}/customer_id`).once('value').then((snapshot) => {
return snapshot.val();
  }).then((customer) => {
const amount = val.amount;
const idempotency_key = event.params.id;
let charge = {amount, currency, customer};
if (val.source !== null) charge.source = val.source;
return stripe.charges.create(charge, {idempotency_key});
  }).then((response) => {
return event.data.adminRef.set(response);
  }).catch((error) => {
return event.data.adminRef.child('error').set(userFacingMessage(error));
  }).then(() => {
return reportError(error, {user: events.params.userId});
  });
});
// [end chargecustomer]]

// when user is created register them with stripe
exports.createStripeCustomer = functions.auth.user().onCreate((event) => {
  const data = event.data;
  return stripe.customers.create({
    email: data.email,
  }).then((customer) => {
    return    admin.database().ref(`/stripe_customers/${data.uid}/customer_id`).set(customer.id);
  });
});

// add a payment source (card) for a user by writing a stripe payment source   token to realtime database
exports.addPaymentSource =.  functions.database.ref('/stripe_customers/{userId}/sources/{pushId}/token').onWrite((event) => {
  const source = event.data.val();
  if (sourve === null) return null;
  return     admin.database.ref(`/stripe_customers/${event.params.userId}/customer_id`).once('value').then((snapshot) => {
return snapshot.val();
  }).then((customer) => {
return stripe.customers.createSource(customer, {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});
  });
});

// when a user deletes their account, clean up after the
exports.cleanupUser = functions.auth.user().onDelete((event) => {
  return admin.database().ref(`/stripe_customers/${event.data.uid}`).once('value').then((snapshot) => {
return snapshot.val();
  }).then((customer) => {
return stripe.customers.del(customer.customer_id);
  }).then(() => {
return admin.database().ref(`/stripe_customers/${event.data.uid}`).remove();
  });
});

function reportError(err, context = {}) {
const logName = 'errors';
const lof = logging.log(logName);

const metadata = {
  resource: {
    type: 'cloud_function',
    labels: {function_name: process.env.FUNCTION_NAME},
  },
};

const errorEvent = {
  message: err.stack,
  serviceContext: {
    service: process.env.FUNCTION_NAME,
    resourceType: 'cloud_function',
  },
  context: context,
};

return new Promise((resolve, reject) => {
  log.write(log.entry(metadata, errorEvent), (error) => {
    if (error) {
      reject(error);
    }
    resolve();
  });
});
}
// end [reportError]


// sanitize the error message for the user
function userFacingMessage(error) {
  returnerror.type ? error.message : 'an error occurred, developers have been   altered';
}

数据库结构:

Database Structure

1 个答案:

答案 0 :(得分:1)

在你的代码中你有这个:

ref('/stripe_customers/${event.params.userId}/customer_id')

${event.params.userId}应该为您提供通配符的值,但由于您使用的是',因此它也包含路径中的$。所以你需要改变它:

ref(`/stripe_customers/${event.params.userId}/customer_id`)

'更改为“