public static StripeCustomerUpdateOptions ToStripeCustomerUpdateOptions(this SetPaymentInformationParameters parameters)
 {
     return(new StripeCustomerUpdateOptions
     {
         SourceToken = parameters.PaymentToken,
         Email = parameters.EmailAddress
     });
 }
        public async Task <StripeCustomer> SetPaymentInformationAsync(string tenantDomain, SetPaymentInformationParameters parameters)
        {
            using (var session = _documentStore.OpenAsyncSession())
            {
                try
                {
                    var organization = await session.Query <Organization>().FirstOrDefaultAsync(o => o.OrganizationDomain == tenantDomain);

                    if (organization == null)
                    {
                        throw new Exception(string.Format("Could not find Organization for Domain '{0}'.", tenantDomain));
                    }
                    if (organization.CustomerId == null)
                    {
                        throw new Exception($"The customer {tenantDomain} does not have an associated Stripe customer ID.");
                    }

                    var stripeCustomerService = new StripeCustomerService();
                    var customer = await stripeCustomerService.GetAsync(organization.CustomerId);

                    if (customer == null)
                    {
                        throw new Exception("Could not load customer data.");
                    }

                    var cardService = new StripeCardService();
                    if (customer.DefaultSourceId != null)
                    {
                        await cardService.DeleteAsync(customer.Id, customer.DefaultSourceId);
                    }

                    return(await stripeCustomerService.UpdateAsync(customer.Id, parameters.ToStripeCustomerUpdateOptions()));
                }
                catch (Exception)
                {
                    session.Dispose();
                    throw;
                }
            }
        }