我不能使用Firebase云功能

时间:2018-05-14 16:22:54

标签: firebase stripe-payments google-cloud-functions

我有一个React Native应用程序,在firebase后端运行。我已经与Stripe集成了。令牌由客户端创建,firebase云功能使用该令牌创建费用。我使用Stripe中的测试键构建了应用并测试了付款。

我现在用实时键替换了测试键。

实时公钥正在React Native应用程序中运行,并且正在成功创建令牌。

这是在React Native应用程序中创建令牌代码的函数

import Stripe from 'react-native-stripe-api';

async payByCard() {
    const { user } = this.props;
    const uid = user.uid;
    const { number, exp_month, exp_year, cvc } = this.state;
    this.setState({ loading: true });
    const apiKey = 'pk_live_#######################';
    const client = new Stripe(apiKey);
    try {
      const token = await client.createToken({
         number,
         exp_month,
         exp_year,
         cvc,
      });
      this.props.addToken({ token }, uid);
    } catch (error) {
      this.setState({ error: error.message, loading: false });
    }
  }

然而,firebase云功能仍在使用密钥测试密钥。

这是创建充电的响亮功能。

import * as functions from 'firebase-functions';
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const stripe = require('stripe')(functions.config().stripe.testkey)

export const stripeCharge = functions.database
  .ref('/payments/{userUid}/{paymentUid}')
    .onWrite((change, context) => {
      const payment = change.after.val();
      const userUid = context.params.userUid;
      const paymentUid = context.params.paymentUid;

      if (!payment || payment.charge || !payment.pendingBasket) return;

      return admin.database()
        .ref(`/users/${userUid}`)
        .once('value')
        .then(snapshot => {
          return snapshot.val();
        })
        .then(customer => {
          const amount = Number(payment.pendingBasket.total * 100).toFixed(0)
          const idempotency_key = paymentUid;
          const source = payment.token.id;
          const currency = 'gbp';
          const description = `Athalens ${customer.address.FirstName} ${customer.address.LastName} - ${customer.address.PostCode}`
          const charge = {amount, currency, description, source};

          return stripe.charges.create(charge, { idempotency_key });
        }).catch((error) => {
          console.log('error 1 =' + error.message);
          admin.database()
            .ref(`/payments/${userUid}/${paymentUid}/status`)
            .set(error.message)
        })
        .then(charge => {
          admin.database()
            .ref(`/payments/${userUid}/${paymentUid}/charge`)
            .set(charge)
            if (charge.status === "succeeded") {
              customerOrders(userUid, paymentUid)
              photographerUid(userUid, paymentUid)
              clearBasket(userUid)
              confirmation(userUid, paymentUid);
            } else {
              decline(userUid, paymentUid)
            }
        }).catch((error) => {
          console.log('error 2 =' + error.message);
        })
    })

我正在做的将密钥上传到firebase的过程:

1. Williams-MBP:~ williamgoodhew$ cd /Users/williamgoodhew/projects/athalens/athalens_server_code/basket/functions

2. Williams-MBP:functions williamgoodhew$ firebase functions:config:set stripe.token=“sk_live_#################”

3. Williams-MBP:functions williamgoodhew$ firebase deploy --only functions

当我测试实时支付系统时,会创建一个令牌,但不会产生任何费用。我在云功能日志中收到以下错误:

No such token: tok_############; a similar object exists in live mode, but a test mode key was used to make this request.

1 个答案:

答案 0 :(得分:1)

我与Firebase有过联系,我的结局是一个愚蠢的错误。

在我的云功能中,我初始化了我的测试键“

  

const stripe = require('stripe')(functions.config()。stripe.testkey)

“而不是使用”stripe.token“。

我将stripe.testkey改为stripe.token。一切都很顺利。