public async Task <Customer> GetCustomerAsync(string customerId)
        {
            var service = new Stripe.CustomerService();

            return(await service.GetAsync(customerId));
        }
Esempio n. 2
0
        public async Task <Domain.Scheduling.Billing.Customer> GetByBillingId(string billingId)
        {
            var stripeCustomer = await customerService.GetAsync(billingId);

            var customer = new Domain.Scheduling.Billing.Customer(
                Guid.Parse(stripeCustomer.Metadata["Id"]),
                Guid.Parse(stripeCustomer.Metadata["UserId"]),
                BillingReference.Customer(stripeCustomer.Id)
                );

            if (stripeCustomer.Subscriptions.Data.Count > 0)
            {
                var stripeSubscription = stripeCustomer.Subscriptions.Data[0];

                customer.Subscription = new Domain.Scheduling.Billing.Subscription(
                    Guid.Parse(stripeSubscription.Metadata["Id"]),
                    Domain.Scheduling.Billing.Subscription.ParseStatus(stripeSubscription.Status),
                    new Domain.Scheduling.Billing.Period(
                        stripeSubscription.TrialStart ?? throw new NullReferenceException(),
                        stripeSubscription.TrialEnd ?? throw new NullReferenceException()
                        ),
                    new Domain.Scheduling.Billing.Period(
                        stripeSubscription.CurrentPeriodStart,
                        stripeSubscription.CurrentPeriodEnd
                        ),
                    stripeSubscription.CancelAtPeriodEnd,
                    new SubscriptionPlanReference(
                        Guid.Parse(stripeSubscription.Metadata["PlanId"]),
                        stripeSubscription.Metadata["PriceBillingId"]
                        ),
                    BillingReference.Subscription(stripeSubscription.Id)
                    );
            }

            /*
             * Payment sources on customer from CustomerService.GetAsync() will always be empty.
             * Need to get them manually instead.
             */
            var paymentMethods = await paymentMethodService.ListAsync(new Stripe.PaymentMethodListOptions()
            {
                Customer = stripeCustomer.Id,
                Type     = "card"
            });

            for (int i = 0; i < paymentMethods.Data.Count; i++)
            {
                var paymentMethod = paymentMethods.Data[i];

                // Add our id to the card if this is the first time we've seen it.
                if (!paymentMethod.Metadata.ContainsKey("Id"))
                {
                    paymentMethod = await paymentMethodService.UpdateAsync(paymentMethod.Id, new PaymentMethodUpdateOptions()
                    {
                        Metadata = new Dictionary <string, string>(new[] {
                            KeyValuePair.Create("Id", Guid.NewGuid().ToString())
                        })
                    });
                }

                var card = paymentMethod.Card;

                customer.PaymentMethods.Add(new Domain.Scheduling.Billing.PaymentMethod(
                                                Guid.Parse(paymentMethod.Metadata["Id"]),
                                                card.Brand,
                                                card.Last4,
                                                // Stripe won't set default unless there is more than 1 payment method on a customer
                                                paymentMethods.Data.Count == 1 ? true : stripeCustomer.InvoiceSettings.DefaultPaymentMethodId == paymentMethod.Id,
                                                new ExpirationDate(card.ExpMonth.ToString(), card.ExpYear.ToString()),
                                                BillingReference.PaymentMethod(paymentMethod.Id)
                                                ));
            }

            return(customer);
        }