Angular6-如何检查Stripe付款是否成功

时间:2018-12-21 17:18:23

标签: angular firebase google-cloud-firestore angular6 stripe-payments

我正在使用Angular6,正在我的应用程序中实现Stripe付款,并使用Google Cloud函数运行服务器端代码。我可以成功付款,但是,我正在为函数添加更多逻辑。在此功能中,我正在处理付款。当我onWrite将文档/payments/${this.userId}/payments收集到集合时,我的云功能就会运行。

.then代码块中,我想检查Stripe付款是否成功,并且只有在成功的情况下,才使用适当的文档更新数据库中用户的详细信息。但是,我不确定这是否是最好的方法。

此外,当前,我正在使用onSnapshot监听将要返回Stripe收费对象的文档中的更改,但是,一旦成功,我就不想监听这些值的更改,直到再付款。

如何实现这种行为?

processPayment(token: any, amount: number){

    const payment = { token, amount };
  this.paymentCollection.doc(`${this.userId}`).collection('payments').add({
            payment
        })
    .then(docRef => {
        this.docRefId = docRef.id;
        //if payment successful then add to the db of purchased lessons
        console.log(this.docRefId);
        this.db.collection("payments/"+`${this.userId}`+"/payments").doc(`${this.docRefId}`)
        .onSnapshot(function(doc) {
            if(doc.data().status === "succeeded"){

            const updatedBalance = this.balance - amount;;

            let writeBatch = firebase.firestore().batch();

            //CAN'T SEE CART CONTENTS

            for(let buyable of this.cartContents){
                let purchasedLessonsRef = this.afs.firestore.doc(`/purchased_lessons/${this.userId}/lessons/${buyable.b_id}`);
                writeBatch.update(purchasedLessonsRef, buyable);
            }

            //Update balance ref
            let userBalanceRef = this.afs.firestore.doc(`/users/${this.userId}`);
            writeBatch.update(userBalanceRef, {balance: updatedBalance});

            this.docRefId = null;

            return writeBatch.commit().then(function () {
                console.log("commiting batch")
            });
        }
    });

    console.log("finished function");
    })
.catch(error => console.error("Error adding document: ", error))

}

0 个答案:

没有答案