public async Task <Customer> GetByEmail(string email)
        {
            try
            {
                #region GetCustomersList
                var CustomerOptions = new CustomerListOptions
                {
                    Limit = 50,
                    Email = email,
                    //RnD about extra parameters
                    //Created = DateTime.Now,
                    //StartingAfter = DateTime.Now.ToString(),
                    //EndingBefore = DateTime.Now.ToString(),
                };

                var customerService = new CustomerService();
                StripeList <Customer> stripeCustomersList = customerService.List(CustomerOptions);
                #endregion

                Customer stripeCustomer = new Customer();

                if (stripeCustomersList.Any(x => x.Email.Equals(email)))
                {
                    stripeCustomer = stripeCustomersList.FirstOrDefault(x => x.Email.Equals(email));
                }

                return(stripeCustomer);
            }

            catch (StripeException e)
            {
                string errorMessage = "";
                switch (e.StripeError.Error)
                {
                case "card_error":
                    errorMessage = $"Card Error occurred on {e.StripeError.PaymentIntent.Id}, Error: {e.StripeError.Error}, Error Code: {e.StripeError.Code}, Error Description: {e.StripeError.ErrorDescription}";
                    break;

                case "api_error":
                    errorMessage = $"API Error occurred: {e.StripeError.Error}, Error Code: {e.StripeError.Code}, Error Description: {e.StripeError.ErrorDescription}";
                    break;

                case "api_connection_error":
                    errorMessage = $"API Connection Error occurred: {e.StripeError.Error}, Error Code: {e.StripeError.Code}, Error Description: {e.StripeError.ErrorDescription}";
                    break;

                case "invalid_request_error	":
                    errorMessage = $"Invalid request Error occurred: {e.StripeError.Error}, Error Code: {e.StripeError.Code}, Error Description: {e.StripeError.ErrorDescription}";
                    break;

                default:
                    errorMessage = $"Some Error occurred: {e.StripeError.Error}, Error Code: {e.StripeError.Code}, Error Description: {e.StripeError.ErrorDescription}";
                    break;
                }

                throw new InvalidOperationException(errorMessage);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds specified product to stripe if it doesn't exist
        /// </summary>
        private static async Task AddProduct(ProductService productService, StripeList <Stripe.Product> products, Models.Product product)
        {
            if (!products.Any(x => x.Name.Equals(product.Name, StringComparison.InvariantCultureIgnoreCase)))
            {
                var newProduct = await productService.CreateAsync(new ProductCreateOptions
                {
                    Name = product.Name,
                    StatementDescriptor = product.StatementDescription,
                    Metadata            = product.Metadata,
                    Type = "service"
                });

                product.Id = newProduct.Id;
            }
        }
Ejemplo n.º 3
0
        public async Task <List <RetrievedCard> > GetAllCurrentCards(string email)
        {
            var list = new List <RetrievedCard>();

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Authorization", $"Bearer {StripeClientConstants.ApiKey}");
                var response = await client.GetStringAsync(_stripeSearchQueryUri + email + "&prefix=false");

                var data       = JsonConvert.DeserializeObject <RootStripeApiJsonData>(response);
                var customerId = data.data.Count == 0 ? string.Empty : data.data[0].id;

                StripeConfiguration.SetApiKey(StripeClientConstants.ApiKey);

                var cardService = new StripeCardService();

                StripeList <StripeCard> cardlist = cardService.List(customerId,
                                                                    new StripeListOptions()
                {
                    Limit = 5
                },
                                                                    false,
                                                                    new StripeRequestOptions()
                {
                    ApiKey = _stripeSecretTestkey
                }
                                                                    );
                if (cardlist.Any())
                {
                    foreach (var item in cardlist)
                    {
                        list.Add(new RetrievedCard()
                        {
                            Name  = item.Name,
                            Last4 = item.Last4,
                            Id    = item.Id
                        });
                    }
                }
                else
                {
                    return(list);
                }

                return(list);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Adds specified plan to stripe if it doesn't exist
 /// </summary>
 private static async Task AddPlan(PlanService planService, StripeList <Stripe.Plan> plans, Models.Plan plan, string productId)
 {
     if (!plans.Any(x => x.Nickname.Equals(plan.Name)))
     {
         await planService.CreateAsync(new PlanCreateOptions
         {
             Nickname = plan.Name,
             Amount   = plan.PricePerUnit,
             Metadata = plan.Metadata,
             Product  = new PlanProductOptions()
             {
                 Id = productId
             },
             Interval = plan.Interval.ToString().ToLower(),
             Currency = plan.PlanCurrency.ToString().ToLower(),
         });
     }
 }
        public async Task <IActionResult> CreateCustomer(CreateCustomerCommand command)
        {
            Customer customer = new Customer();

            #region GetCustomersList
            var CustomerOptions = new CustomerListOptions
            {
                Limit = 50,
                Email = command.Email
            };
            var customerService = new CustomerService();
            StripeList <Customer> customersList = customerService.List(
                CustomerOptions
                );
            #endregion

            if (customersList.Any(x => x.Email.Equals(command.Email)))
            {
                var temp = customersList.FirstOrDefault(x => x.Email.Equals(command.Email));
                customer.Id = temp.Id;
            }

            else
            {
                customer = customerService.Create(new CustomerCreateOptions
                {
                    Email = command.Email
                });
            }

            //CustomerId = customer.Id;

            //Fix the issue, till then return null
            return(null);
            //return Ok(new CreateCustomerCommandResult() { Payload = customer.Id });
        }