列出Nodejs中的所有条带费用

时间:2016-03-21 18:22:04

标签: javascript node.js stripe-payments

我们正在从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中实现它?

1 个答案:

答案 0 :(得分:4)

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);