【问题标题】:Stripe API - C#/.NET List All Customers problems with paginationStripe API - C#/.NET 列出所有客户的分页问题
【发布时间】:2018-02-01 10:42:32
【问题描述】:

由于我对 C# 和 .NET 缺乏经验,这可能是一个简单的答案。我有两个 Stripe 测试帐户。 TL:博士;我本质上是在寻找Customers.all 解决方案。

源帐户包含所有客户、卡和费用数据。

目标帐户具有由 Stripe 支持完成的复制卡和客户数据。

我有循环从源帐户提取所有数据的代码。然后,它使用来自源的客户/卡数据的集合从目标帐户中查找客户数据。之后,它会将源帐户中的费用重新创建到目标帐户中。

我能够使用源帐户中的信息将前 100 笔费用成功复制到目标帐户,但我很难获得其余客户。

这是我目前所拥有的:

public static void GenerateDestinationChargeData()
    {
        // code to get collection of customer data from destination account
        StripeConfiguration.SetApiKey(destinationTestKey);

        var customerService = new StripeCustomerService();


      IEnumerable<StripeCustomer> customerItems = customerService.List(
          new StripeCustomerListOptions()
          {
              Limit = 100,
              //this is what I cannot figure out, eventually to get all of the customers from the destination account
              StartingAfter = customerItems.LastOrDefault().Id
          }
       );



        // loop through collection of customers from destination acct to fetch customer charge data from source account
        foreach (var i in customerItems)
        {
            bool isError = false;

            var liveChargeService = new StripeChargeService();
            StripeConfiguration.SetApiKey(sourceTestKey);
            StripeList<StripeCharge> chargeItems = new StripeList<StripeCharge>();
            chargeItems = liveChargeService.List(
               new StripeChargeListOptions()
               {
                   Limit = 100,
                   CustomerId = i.Id
               }
             );

            // loop through customer charge data from source and re-create charge data on destination Acct
            foreach (var c in chargeItems.Data)
            {
                StripeConfiguration.SetApiKey(sourceTestKey);
                var emailReceipt = "";
                Dictionary<string, string> chargeMetaData = new Dictionary<string, string>();
                var onBehalfOf = "";
                var transferGroup = "";
                var chargeDescription = "";
                var chargeCaptured = "";
                var chargeCurrency = "";
                var chargeStatementDescriptor = "";

                if (c.ReceiptEmail != null)
                {
                    emailReceipt = c.ReceiptEmail;
                }
                if (c.Metadata != null)
                {
                    chargeMetaData = c.Metadata;
                }
                if (c.OnBehalfOf != null)
                {
                    onBehalfOf = c.OnBehalfOf.ToString();
                }
                if (c.TransferGroup != null)
                {
                    transferGroup = c.TransferGroup;
                }
                if (c.Description != null)
                {
                    chargeDescription = c.Description;
                }
                if (c.Captured != null)
                {
                    chargeCaptured = c.Captured.ToString();
                }
                if (c.Currency != null)
                {
                    chargeCurrency = c.Currency;
                }
                if (c.StatementDescriptor != null)
                {
                    chargeStatementDescriptor = c.StatementDescriptor;
                }

                try
                {
                    var chargeOptions = new StripeChargeCreateOptions();
                    chargeOptions.CustomerId = i.Id;
                    chargeOptions.ReceiptEmail = emailReceipt;
                    chargeOptions.Metadata = chargeMetaData;
                    chargeOptions.Description = chargeDescription;
                    chargeOptions.Capture = c.Captured;
                    chargeOptions.Currency = chargeCurrency;
                    chargeOptions.Amount = c.Amount;
                    chargeOptions.StatementDescriptor = chargeStatementDescriptor;

                    StripeChargeService chargeService = new StripeChargeService(destinationTestKey);

                    StripeCharge stripeCharge = chargeService.Create(chargeOptions);
                }
                catch (Exception ex)
                {
                    Utility.NotifyDevAdminException("test", ex);
                    isError = true;
                }
                if (isError) continue;

            }

        }
    }

非常感谢:)

【问题讨论】:

  • 解决方案非常简单,我觉得我懒得解决这个问题了。我将客户服务代码块包装在一个 while 循环中,将客户推送到一个数组中,然后对其进行迭代以最终重新创建费用
  • 嗨,您可以编辑答案吗,您是如何实现的?
  • 我遇到了同样的问题
  • @AmmarAhmed 帖子已更新

标签: c# .net stripe.net


【解决方案1】:

由于我们无法使用当前的 Stripe API 执行 Customers.all,因此解决方案是设置一个空变量并将我们获得的第一组 100 中的最后一个客户 ID 分配给它,然后从最后分配的值继续查询

var lastId = String.Empty;

if (String.IsNullOrEmpty(lastId))
{
  StripeConfiguration.SetApiKey(sourceCustomerAccountAPIKey);

  customerItems = customerService.List(
  new StripeCustomerListOptions(){ Limit = 100 });
}
else 
{
  StripeConfiguration.SetApiKey(sourceCustomerAccountAPIKey);
  customerItems = customerService.List(
  new StripeCustomerListOptions() {
      Limit = 100,
      StartingAfter = lastId });
}

lastId = customerItems.LastOrDefault().Id;

【讨论】:

  • 谢谢,这很有帮助:)
猜你喜欢
  • 2020-02-20
  • 2013-03-14
  • 2020-12-28
  • 2020-05-28
  • 2021-04-09
  • 2023-03-09
  • 2013-10-31
  • 1970-01-01
  • 2021-06-16
相关资源
最近更新 更多