如何使用 Stripe NodeJS SDK 获取更多客户资源? [英] How to get more customer sources using Stripe NodeJS SDK?

查看:18
本文介绍了如何使用 Stripe NodeJS SDK 获取更多客户资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只能通过获取客户 API 获取第 10 个客户来源:

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"
  },
  ...
}

但是我如何获得更多呢?是通过 AJAX 调用的唯一方法吗?我在想 SDK 中的某个地方应该有一个函数?

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?

推荐答案

当您通过 API 检索 Customer 对象时,Stripe 将返回 sources 属性,它是一个 List 对象.data 属性将是一个数组,其中最多包含 10 个源.

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.

如果您希望获得比最近 10 个源更多的源,则需要使用 分页.这个想法是您将首先获得 N 个对象的列表(默认为 10 个).然后您将通过再次请求 N 个对象但使用参数 starting_after 设置为上一页中最后一个对象的 id 来从 Stripe 请求下一个页面".您将继续这样做,直到返回的页面中的 has_more 属性为 false 表示您检索了所有对象.

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.

例如,如果您的客户有 35 个来源,您将获得第一页 (10),然后调用列表获得另外 10 个 (20),然后再获得 10 个 (30),最后一次调用将仅返回 5 个来源(35) 和 has_more 将是错误的.

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.

要减少调用次数,您还可以将 limit 设置为更高的值.在这种情况下,最大值为 100.

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

代码如下所示:

// 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 上的完整运行示例:https://runkit.com/5a6b26c0e3908200129fbb5d/5b49eabda462940012c33880" rel="nofollow noreferrer">https://runkit.com/5a6b26c0e3908200129fbb5d/5b40/a>

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

这篇关于如何使用 Stripe NodeJS SDK 获取更多客户资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆