gpt4 book ai didi

node.js - 如何从 Firebase 的云功能获取 strip 付款费用 'success'?

转载 作者:太空宇宙 更新时间:2023-11-04 01:39:04 24 4
gpt4 key购买 nike

我正在开发一个使用 Angular 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;
})
});

支付.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();
}

最佳答案

您应该按如下方式修改您的 Cloud Function 代码,以便返回异步 .set(charge) 方法返回的 Promise。

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;
})

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

关于node.js - 如何从 Firebase 的云功能获取 strip 付款费用 'success'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53569147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com