如何从.NET Core中的Stripe通过电子邮件检索客户的所有订阅

时间:2020-07-09 08:00:38

标签: c# asp.net-core stripe-payments

我正在使用 .NET Core 进行Stripe Payment,并且想在用户的仪表板上显示该用户的所有订阅,因此我一直关注着一些Stripe Docs,要么我没有得到它,要么我找不到我需要的东西。这是我在Stripe Docs Here

上引用的链接

1 个答案:

答案 0 :(得分:4)

我认为,这是您需要执行的操作,以便通过电子邮件从Stripe检索客户的所有订阅。

步骤1 。创建此方法,该方法接受电子邮件作为参数并返回所请求用户的订阅。

        public async Task<List<Subscription>> GetSubscriptionsByEmail(string customerEmail)
        {
            var customerService = new CustomerService();
            var customerOptions = new CustomerListOptions()
            {
                Email = customerEmail
            };
            var customers = await customerService.ListAsync(customerOptions);
            var customer = customers.FirstOrDefault();
            //Check if customer exists with the given Email.
            if (customer == null)
            {
                throw new Exception(message: $"No customer exists with Email = {customerEmail}.");
            }
            return customer.Subscriptions.Data.ToList();
        }

第2步:在需要的地方调用在第1步中创建的方法。

List<Subscription> subscriptions = await GetSubscriptionsByEmail("CUSTOMER_EMAIL");

如果给定CUSTOMER_EMAIL的客户存在,您将获得订阅列表。

注意:我正在考虑您已经设置了StripeConfiguration.ApiKey=YOUR_STRIPE_KEY.