Stripe未收到Firebase充电功能

时间:2018-01-12 20:31:25

标签: javascript firebase ionic-framework stripe-payments

我正在使用离子应用程序上的信用卡收费。如果我检查Stripe网站,则只会成功创建令牌。没有收费的事件。当我检查Firebase时,一切似乎都在工作(之前我有错误消息“我们与Stripe的连接发生了错误”,通过升级到付费版本Blaze消除了)。

我已经构建了以下代码,以便您可以看到事件的顺序。

以下是事件序列:

1)我在这里获取令牌并传递给payByStripe函数:

checkOut() {
  let alert = this.alertCtrl.create({
    title: 'card information',
    cssClass:'alert-css',
    inputs: [{
      name: 'cardNumber',
      placeholder: 'card number',
    },
    {
      name: 'expMonth',
      placeholder: 'expMonth',
    },
    {
      name: 'expYear',
      placeholder: 'expYear',
    },
    {
      name: 'cvc',
      placeholder: 'cvc',
    }],
    buttons: [{
      text: 'Cancel',
      role: 'cancel',
      handler: data => {
        console.log('Cancel clicked');
      }
    },
    {
      text: 'PAY',
      handler: data => {
        this.stripe.setPublishableKey('pk_test_abcdef1BC');
        let card = {
          number: data.cardNumber,
          expMonth: data.expMonth,
          expYear: data.expYear,
          cvc: data.cvc
        };
        this.stripe.createCardToken(card)
        .then(token => {
          this.goodsData.payByStripe(this.totalMoniesPlusTaxAndCharge,this.userEmail,token);
            let alert = this.alertCtrl.create({
              title: 'Charged',
              cssClass:'alert-css',
              subTitle: 'Successful Charge!',
              buttons: ['Dismiss']
            });
            alert.present();
          }, error => {
            this.loading.dismiss();
            let alert = this.alertCtrl.create({
              title: 'ERROR',
              cssClass:'alert-css',
              subTitle: JSON.stringify(error),
              buttons: ['Dismiss']
            });
            alert.present();
          })
        .catch(error => console.log(JSON.stringify(error)) );
      }
    }]
  });
  alert.present();
} 

2)在这里,我将费用推送到Firebase,云计算功能将在其中处理:

payByStripe(amount,email,token): firebase.database.Reference {
  return firebase.database().ref('/stripe_customers/charges/').push({
    amount:amount,
    email:email,
    token:token
  });
}

3)以下是云功能正在做的事情:

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

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

const stripe = require('stripe')(functions.config().stripe.token),
      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/charges/').onWrite(event => {
  const val = event.data.val();
  // This onWrite will trigger whenever anything is written to the path, so
  // noop if the charge was deleted, errored out, or the Stripe API returned a result (id exists) 
  if (val === null || val.id || val.error) return null;
  // Look up the Stripe customer id written in createStripeCustomer

    // Create a charge using the pushId as the idempotency key, protecting against double charges 
    const amount = val.amount;
    const email = val.email;
    const token = val.token;

    stripe.charges.create({
      amount: amount,
      currency: "usd",
      source: token,
      description: "Charge for " + email
    }).then(response => {
      // If the result is successful, write it back to the database
      return event.data.adminRef.set(response);
    }, error => {
      // We want to capture errors and render them in a user-friendly way, while
      // still logging an exception with Stackdriver
      return event.data.adminRef.child('error').set(userFacingMessage(error)).then(() => {
        return reportError(error, {user: event.params.userId});
      });
    });
});
// [END chargecustomer]]

1 个答案:

答案 0 :(得分:1)

我非常确定这是Firebase特定的错误,因此如果原因不清楚,您可能希望与他们联系 - 尽管拥有更多Firebase经验的其他人可能会有更有用的答案!