Stripe API - C#/ .NET列出所有客户分页问题

时间:2017-08-23 18:00:17

标签: c# .net stripe.net

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

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

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

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

我能够使用来自源帐户的信息成功将前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;

            }

        }
    }

非常感谢你:)

1 个答案:

答案 0 :(得分: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;