gpt4 book ai didi

javascript - Stripe : Is it possible to create a direct charge with idempotency

转载 作者:行者123 更新时间:2023-11-30 19:09:08 25 4
gpt4 key购买 nike

我想直接向一个 stripe 连接的账户收费,我可以通过以下方式做到这一点

const charge = {
amount,
currency,
source} ;

return stripe.charges.create(
charge ,
{stripe_account: stripeVendorAccount});

问题:有没有办法在 charge 对象中包含幂等性选项?如果不是,避免重复收费的最佳方法是什么?

我试过了,还是不行

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 标识您的订单并将其保存在数据库中。在您的收费方法中从数据库中查询是否已经对该购买收费,如果没有则创建一个。

关于javascript - Stripe : Is it possible to create a direct charge with idempotency,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58679816/

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