How to get more customer sources using Stripe NodeJS SDK?

心不动则不痛 提交于 2019-12-24 09:00:22

问题


I am able to get customer sources only 1st 10 via get customer API:

# stripe.customers.retrieve

{
  "id": "cus_DE8HSMZ75l2Dgo",
  ...
  "sources": {
    "object": "list",
    "data": [

    ],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/customers/cus_DE8HSMZ75l2Dgo/sources"
  },
  ...
}

But how do I get more? Is the only way via an AJAX call? I was thinking there should be a function somewhere in the SDK?


回答1:


When you retrieve a Customer object via the API, Stripe will return the sources property which is a List object. The data property will be an array with up to 10 sources in it.

If you want the ability to get more sources than the 10 most recent ones, you will need to use Pagination. The idea is that you will first get a list of N objects (10 by default). Then you will request the next "page" from Stripe by asking for N objects again but using the parameter starting_after set to the id of the last object in the previous page. You will continue doing that until the has_more property in the page returned is false indicating you retrieved all the objects.

For example if your Customer has 35 sources, you would get the first page (10), then call list to get 10 more (20), then 10 more again (30) and then the last call would return only 5 sources (35) and has_more would be false.

To decrease the number of calls, you can also set limit to a higher value. The maximum value is 100 in that case.

Here's what the code would look like:

// list those cards 3 at a time
var listOptions = {limit: 3};
while(1) {
    var sources = await stripe.customers.listSources(
      customer.id,
      listOptions
    );

    var nbSourcesRetrieved = sources.data.length;
    var lastSourceId = sources.data[nbSourcesRetrieved - 1].id;
    console.log("Received " + nbSourcesRetrieved + " - last source: " + lastSourceId + " - has_more: " + sources.has_more);

    // Leave if we are done with pagination
    if(sources.has_more == false) {
      break;
    }

    // Store the last source id in the options for the next page
    listOptions['starting_after'] = lastSourceId;
}

You can see a full running example on Runkit here: https://runkit.com/5a6b26c0e3908200129fbb5d/5b49eabda462940012c33880




回答2:


Taking a quick look into the sources of the stripe-node package, it seems there is a stripe.customers.listSources method, which takes a customerId as parameter and requests to the correct url. I suppose it works similar to the listCards method. But I couldn't find it in the docs, so you have to treat it as an undocumented feature ... But maybe it's just an error in the docs. You could contact the support about it. We used stripe in an old project and they appreciated any input on their documentation.




回答3:


As of stripe-node 6.11.0, you may auto-paginate list methods, including customer sources. Stripe provides a few different APIs for this to aid with a variety of node versions and styles.

See the docs here

The important part to notice is .autoPagingEach:

await stripe.customers.listSources({ limit: 100 }).autoPagingEach(async (source) => {
  doSomethingWithYourSource(source)
})


来源:https://stackoverflow.com/questions/51335562/how-to-get-more-customer-sources-using-stripe-nodejs-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!