I have this:
const paymentIntents = await stripe.paymentIntents.list({
stripeAccount: stripe_account
});
and it returns:
object: 'list',
data: [
{
id: 'pi_...',
object: 'payment_intent',
amount: 2500,
now I have two problems. 1. I need to access amount
. and 2. there is going to be dozens of records being returned, so I need to loop through all the results and actually add up the amounts
. here's what I've tried so far:
console.log(paymentIntents.amount)
console.log(paymentIntents.data.amount)
neither of those worked. and i wouldn't even know how to loop thorugh all those results. How can i do this?
CodePudding user response:
you can use reduce like this:
const paymentIntents = {object: 'list', data: [{
id: 'pi_...',
object: 'payment_intent',
amount: 2500}, {
id: 'pi_...',
object: 'payment_intent_2',
amount: 3500,
}]}
const amount = paymentIntents.data.reduce((previous, current, index, array) => {
const sum = current.amount previous.amount;
return sum;
});
CodePudding user response:
const paymentIntents = await stripe.paymentIntents.list({
stripeAccount: stripe_account
});
const total = paymentIntents.data.reduce(
(listTotal, line) => listTotal line.amount,
0
);
CodePudding user response:
Be aware that if you expect to come close to the limit of 100 results for a list request, you should make sure to handle pagination, for example using the autopagination supported by stripe-node
:
let total = 0
stripe.paymentIntents.list({limit: 5})
.autoPagingEach(function(pi) {
total = pi.amount
});
Note that this structure requires iteration, and precludes the use of reduce
-type solutions in the other answers. If your expected result set won't come close to the 100 item limit, then set your limit appropriately and don't worry about this.