如何从Cloud Function for Firebase获取条带化付款费用“成功”?

时间:2018-12-01 08:43:15

标签: node.js angular firebase google-cloud-functions stripe-payments

我正在研究一个带有角度6,Stripe元素和Google Firebase(具有Cloud Functions)的项目。都是我新手!

在我的生命中,我无法弄清楚如何退还“某物”,表明付款已成功。 Stripe API文档,声明仅在出现“错误”时才返回错误调用...

我可以看到Firebase中的充电对象已成功对卡进行充电。

我可以用来查询此内容并将“状态:已付款”值返回到前端...,以便使用* ngIf显示确认/失败消息吗?

我知道我在这里想念一些简陋的东西...!我真的很感谢这些家伙的帮助。

index.js(云功能)

const functions = require('firebase-functions');
const admin = require('firebase-admin')

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

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

exports.stripeCharge = functions.database
.ref('/payments/{paymentId}')
.onWrite((change, context) => {
const payment = change.after.val();

const paymentId = context.params.paymentId;

// checks if payment exists or if it has already been charged
if (!payment || payment.charge) {
  return
}

return admin.database()
  .ref('/users/')
  .once('value')
  .then(snapshot => {
    return snapshot.val()
  })

  .then(customer => {
    const amount = payment.amount;
    const idempotency_key = paymentId; // prevent duplicate charges
    const source = payment.token.id;
    const currency = 'gbp';
    const charge = { amount, currency, source };

    return stripe.charges.create(charge, { idempotency_key });
  })

  .then(charge => {
    admin.database()
      .ref(`/payments/${paymentId}/charge`)
      .set(charge)
      return true;
  })
});

Payment.service.ts

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';

@Injectable()
export class PaymentService { 

constructor(private db: AngularFireDatabase) {}

// save the token to firebase, triggering the cloud function
processPayment(token: any, amount) {
  const payment = { token, amount }
  return this.db.list('/payments/').push(payment)
  }
}

payment.component.ts(这是我用于结帐的onSubmit处理程序)

async onSubmit(form: NgForm) {
//this.paymentProcess = true;
const { token, error } = await stripe.createToken(this.card, {
  name: this.contactName,
  email: this.contactEmail
});

if (error) {  
  console.log('Something is wrong:', error);
} else {
  console.log('Success!', token);
    this.paymentSvc.processPayment(token, this.amount);
  }
  this.card.clear();
}

1 个答案:

答案 0 :(得分:0)

您应按如下所示修改Cloud Function代码,以返回异步.set(charge)方法返回的承诺。

exports.stripeCharge = functions.database
.ref('/payments/{paymentId}')
.onWrite((change, context) => {
const payment = change.after.val();

const paymentId = context.params.paymentId;

// checks if payment exists or if it has already been charged
if (!payment || payment.charge) {
  return
}

return admin.database()
  .ref('/users/')
  .once('value')
  .then(snapshot => {
    return snapshot.val()
  })

  .then(customer => {
    const amount = payment.amount;
    const idempotency_key = paymentId; // prevent duplicate charges
    const source = payment.token.id;
    const currency = 'gbp';
    const charge = { amount, currency, source };

    return stripe.charges.create(charge, { idempotency_key });
  })

  .then(charge => {
    return admin.database()  <-- Here return !!!!!!
      .ref(`/payments/${paymentId}/charge`)
      .set(charge);
      //return true;    <-- Here don't return !!!!!!
  })
});

为了“将'status:paid'的值返回到前端..”,只需将listener设置为/payments/${paymentId}/charge路径,并在收费状态正确后立即设置价值,请更新您的前端。


最后,请注意:

  ...
  .then(charge => {
    admin.database()
      .ref(`/payments/${paymentId}/charge`)
      .set(charge)
      return true;
  })

您正在将值true返回到Cloud Function平台,(表明该功能可以终止)set()异步操作完成之前。