Node.js 如何使用Stripe NodeJS SDK获得更多客户资源?

Node.js 如何使用Stripe NodeJS SDK获得更多客户资源?,node.js,stripe-payments,Node.js,Stripe Payments,我只能通过get customer API获取前10个客户来源: # stripe.customers.retrieve { "id": "cus_DE8HSMZ75l2Dgo", ... "sources": { "object": "list", "data": [ ], "has_more": false, "total_count": 0, "url": "/v1/customers/cus_DE8HSMZ75l2Dgo/s

我只能通过get customer API获取前10个客户来源:

# stripe.customers.retrieve

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

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

但我怎样才能得到更多呢?是通过AJAX调用的唯一方法吗?我在想SDK中应该有一个函数

快速查看stripe节点包的属性,似乎有一个
stripe.customers.listSources
方法,它将
customerId
作为参数并请求正确的url。我想它的工作原理类似于。但是我在文档中找不到它,所以你必须将它视为一个未记录的功能。。。但也许这只是文档中的一个错误。您可以就此联系支持部门。我们在一个旧项目中使用了stripe,他们非常感谢对其文档的任何输入。

快速查看stripe节点包的详细信息,似乎有一个
stripe.customers.listSources
方法,该方法将
customerId
作为参数并请求正确的url。我想它的工作原理类似于。但是我在文档中找不到它,所以你必须将它视为一个未记录的功能。。。但也许这只是文档中的一个错误。您可以就此联系支持部门。我们在一个旧项目中使用了stripe,他们非常感谢对文档的任何输入。

当您通过API检索客户对象时,stripe将返回作为列表对象的
sources
属性。
data
属性将是一个数组,其中最多包含10个源

如果您想获得比最近10个资源更多的资源,您需要使用。其思想是,您将首先获得N个对象的列表(默认情况下为10个)。然后,您将通过再次请求N个对象,但使用参数
start\u after
设置为上一页中最后一个对象的id,从Stripe请求下一个“页面”。您将继续执行此操作,直到返回页面中的
has_more
属性为
false
表示您检索了所有对象

例如,如果您的客户有35个来源,您将获得第一页(10),然后通过呼叫列表再获得10个(20),然后再获得10个(30),最后一次呼叫将只返回5个来源(35),并且
has_more
将为false

要减少通话次数,还可以将
limit
设置为更高的值。在这种情况下,最大值为100

下面是代码的样子:

// 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;
}

您可以在Runkit上看到完整的运行示例:

当您通过API检索客户对象时,Stripe将返回作为列表对象的
sources
属性。
data
属性将是一个数组,其中最多包含10个源

如果您想获得比最近10个资源更多的资源,您需要使用。其思想是,您将首先获得N个对象的列表(默认情况下为10个)。然后,您将通过再次请求N个对象,但使用参数
start\u after
设置为上一页中最后一个对象的id,从Stripe请求下一个“页面”。您将继续执行此操作,直到返回页面中的
has_more
属性为
false
表示您检索了所有对象

例如,如果您的客户有35个来源,您将获得第一页(10),然后通过呼叫列表再获得10个(20),然后再获得10个(30),最后一次呼叫将只返回5个来源(35),并且
has_more
将为false

要减少通话次数,还可以将
limit
设置为更高的值。在这种情况下,最大值为100

下面是代码的样子:

// 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;
}

您可以在Runkit上看到完整的运行示例:

从stripe node 6.11.0开始,您可以自动分页列表方法,包括客户源。Stripe为此提供了一些不同的API,以帮助实现各种节点版本和样式

见文件

需要注意的重要部分是。AutoPagineAch

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

从stripe node 6.11.0开始,您可以自动分页列表方法,包括客户源。Stripe为此提供了一些不同的API,以帮助实现各种节点版本和样式

见文件

需要注意的重要部分是。AutoPagineAch

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

但是这使用了listCards API,在我的理解中,卡片和来源是不同的?@JiewMeng您可以简单地使用
listSources
,而我只是更新了示例。但是这使用了listCards API,在我的理解中,卡片和来源是不同的?@JiewMeng您可以简单地使用
listSources
,我刚刚更新了示例。