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")); }
public AttachPaymentMethodResponse AttachPaymentMethodToCustomer(string customerId, string paymentMethodId) { HttpContent content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("customer", customerId), }); content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); HttpResponseMessage response = (_client.PostAsync($"https://api.stripe.com/v1/payment_methods/{paymentMethodId}/attach", content)) .GetAwaiter().GetResult(); var attachPaymentMethodResponse = new AttachPaymentMethodResponse(); if (response.IsSuccessStatusCode) { attachPaymentMethodResponse = JsonConvert.DeserializeObject <AttachPaymentMethodResponse>(response.Content.ReadAsStringAsync().Result); } return(attachPaymentMethodResponse); }