Я пытаюсь сделать прямой заряд для учетной записи, подключенной к полосе, и могу сделать это с помощью следующего
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;
})
});