Пример #1
0
        public IActionResult AddPaymentMethod(PaymentMethodModel model)
        {
            var  customer         = _db.Customers.FirstOrDefault(c => c.Email == model.Email);
            bool isPaymentPrimary = false;

            if (customer == null)
            {
                customer = new StripeAppPrototype.Entities.Customer {
                    Email = model.Email
                };
                CreateCustomerResponse customerResponse = _businessServices.CreateCustomer(customer);
                if (String.IsNullOrEmpty(customerResponse.id))
                {
                    ModelState.AddModelError("", "Could not add customer method");
                    return(View(model));
                }

                customer.Id      = customerResponse.id;
                isPaymentPrimary = true;
                _db.Customers.Add(customer);
            }

            var paymentMethod = new StripeAppPrototype.Entities.PaymentMethod {
                Id = model.PaymentMethodId, CustomerId = customer.Id, Description = model.Description, IsPrimary = isPaymentPrimary
            };

            _db.PaymentMethods.Add(paymentMethod);

            AttachPaymentMethodResponse attachPaymentMethodResponse =
                _businessServices.AttachPaymentMethodToCustomer(customer.Id, paymentMethod.Id);

            if (isPaymentPrimary)
            {
                UpdateCustomerResponse updateCustomerResponse = _businessServices.UpdateCustomer(customer.Id, paymentMethod.Id);
                if (String.IsNullOrEmpty(updateCustomerResponse.id))
                {
                    ModelState.AddModelError("", "Could not add primary");
                    return(View(model));
                }
            }

            _db.SaveChanges();

            return(RedirectToAction("Index", "Home"));
        }