列出Nodejs中的所有条带费用

List all stripe charges in Nodejs

我们正在从 Ruby 迁移到 NodeJS,我们本质上想要在 Node 中使用这样的函数:

starting_after = nil
charges = []
while true
  results = Stripe::Charge.all(limit: 100, starting_after: starting_after)
  break if results.data.length == 0
  charges = charges + results.data
  starting_after = results.data.last.id  
end

应该如何在 NodeJS 中实现这一点?

var stripe = require("stripe")(
  "sk_test_xxx"
);

function paginateCharges(last_id) {
  // Define request parameters
  var req_params = { limit: 3 };
  if (last_id !== null) { req_params['starting_after'] = last_id; }

  // Get events
  stripe.charges.list(
    req_params,
    function(err, charges) {

      // Do something with the returned values
      for (i = 0; i < charges.data.length; i++){
        console.log(charges.data[i].id);
      }

      // Check for more
      if (charges.has_more) {
        paginateCharges(charges["data"][charges["data"].length - 1].id);
      }
    }
  )
}

paginateCharges(null);