Javascript 条纹:有可能产生具有幂等性的直接电荷吗
我正在尝试直接向一个连接到条带的帐户收费,我可以通过以下方式做到这一点Javascript 条纹:有可能产生具有幂等性的直接电荷吗,javascript,stripe-payments,idempotent,Javascript,Stripe Payments,Idempotent,我正在尝试直接向一个连接到条带的帐户收费,我可以通过以下方式做到这一点 const charge = { amount, currency, source} ; return stripe.charges.create( charge , {stripe_account: stripeVendorAccount}); 问题:有没有一种方法可以将幂等选项包含到电荷对象中?如果没有,避免重复收费的最佳方法是什么 我试过了,但没用 const charge = { amou
const charge = {
amount,
currency,
source} ;
return stripe.charges.create(
charge ,
{stripe_account: stripeVendorAccount});
问题:有没有一种方法可以将幂等选项包含到电荷对象中?如果没有,避免重复收费的最佳方法是什么
我试过了,但没用
const charge = {
amount,
currency,
source} ;
return stripe.charges.create(
charge ,
{stripe_account: stripeVendorAccount},
{idempotency_key });
编辑:这是我的功能
exports.stripeCharge = functions.database
.ref('/payments/{userId}/{paymentId}')
.onWrite( (change,context) => {
const payment = change.after.val();
const userId = context.params.userId;
const paymentId = context.params.paymentId;
const stripeVendorAccount = 'xx'
// checks if payment exists or if it has already been charged
if (!payment || payment.charge) return;
return admin.database()
.ref(`/users/${userId}`)
.once('value')
.then(snapshot => {
return snapshot.val();
})
.then(async customer => {
const amount = payment.amount;
const idempotency_key = paymentId; // prevent duplicate charges
const source = payment.token.id;
const currency = payment.currency;
const application_fee = payment.application_fee;
const description = payment.description;
//-------- destination charges
// const transfer_data = {destination: stripeVendorAccount};
// const charge = {
// amount,
// currency,
// description,
// transfer_data,
// source};
// return stripe.charges.create(charge , { idempotency_key });
// })
// .then(charge => {
// admin.database()
// .ref(`/payments/${userId}/${paymentId}/charge`)
// .set(charge);
// return true;
// })
//-------- destination charges
//-------- DIRECT charges
const charge = {
amount,
currency,
description,
application_fee,
source} ;
return stripe.charges.create(charge ,{stripe_account: stripeVendorAccount});
})
.then(charge => {
admin.database()
.ref(`/payments/${userId}/${paymentId}/charge`)
.set(charge);
return true;
})
});
我猜电荷的产生在定义上不是幂等的,因为电荷通常与一次购买有关。(如果用户连续三次发起购买同一商品,将产生三种不同的订单/费用)。
我不知道您当前在电子商务集成背后的业务逻辑实现。但是,如果您只是想避免为一个订单创建多个费用,那么应该使用唯一的id标识您的订单,并将其保存在数据库中。在您的收费方法中,从数据库查询该购买是否已经收费,如果没有,请创建一个。我只是想避免对该资源的多个POST请求不会改变结果-换句话说,防止对该卡重复收费。我已经发布了我的代码,您可以看到我正在传递paymentId作为目的地费用的幂等对象,但是我不能对直接费用做同样的操作,因为费用只接受一个选项对象,我必须传递stripeId