Custom Payout Report with custom Payout ID
const stripe = require("stripe")(process.env.STRIPE_SECRET_TEST_KEY);
const ITEM_LIMIT = 100;
// Unix timestamp in seconds
let now = Math.round(Date.now()/1000).toString();
async function requestor(ending_before=undefined, balance={}) {
try {
let transactions = await stripe.balance.listTransactions({
limit: ITEM_LIMIT,
ending_before,
available_on: {
lte: now,
},
});
let data = transactions.data;
for(t in data){
if (data[t].status === "available" && data[t].type != "payout") {
let curr = data[t].currency;
let net = data[t].net;
balance[curr] = balance[curr] ? balance[curr] : 0;
balance[curr] += net;
}
}
if (transactions.has_more) {
requestor(data[0].id, balance);
} else {
// On the last page take the first available balance transaction.
// This is your pointer in the balance history list which you will need to pass as input to the requestor the next time you run the report.
let listPosition = null;
for (t in data) {
if (data[t].status === "available" && data[t].type != "payout") {
listPosition = data[t].id;
break;
}
}
return { balance, listPosition };
}
} catch (err) {
console.log('Failed');
}
}
// Everytime you run the report, store the listPosition in your DB, use it when you invoke the requestor the next time
// https://stripe.com/docs/api#pagination
const lastListPosition = "txn_1AXSOYJuxXf3u4Gt3Ynm0z7d";
const report = await requestor(lastListPosition);
// The sum in the balance will represent the avilable balance per currency.
// Use this to create the payout to your bank account:
// https://stripe.com/docs/api#create_payout
// setting your dynamic statement descriptor:
// https://stripe.com/docs/api#create_payout-statement_descriptor
const availableBalance = report.balance;
const payoutCurrency = "usd"
await stripe.payouts.create({
amount: availableBalance[payoutCurrency],
currency: payoutCurrency,
statement_descriptor: "MY CUSTOM PAYOUT ID",
});
no comments