public async Task <CreatePaymentMethodModel> Handle(CreatePaymentMethodCommand request, CancellationToken cancellationToken) { try { if (request.IsDefault) { //set other customer payment methods to not default var entities = _context.PaymentMethods .Where(a => a.CustomerID == request.CustomerID && a.IsDefault) .ToList(); if (entities.Count != 0) { entities.ForEach(e => e.IsDefault = false); _context.PaymentMethods.UpdateRange(entities); await _context.SaveChangesAsync(cancellationToken); } } var sPaymentMethod = _stripe .GetPaymentMethod(request.StripePaymentMethodID); var paymentMethod = new PaymentMethod { CustomerID = request.CustomerID, IsDefault = request.IsDefault, NameOnCard = request.NameOnCard, StripePaymentMethodID = request.StripePaymentMethodID, CardBrand = sPaymentMethod.Card.Brand, Last4 = sPaymentMethod.Card.Last4, ExpMonth = sPaymentMethod.Card.ExpMonth, ExpYear = sPaymentMethod.Card.ExpYear, }; _context.PaymentMethods.Add(paymentMethod); await _context.SaveChangesAsync(cancellationToken); return(_mapper.Map <CreatePaymentMethodModel>(paymentMethod)); } catch (Exception e) { return(new CreatePaymentMethodModel()); } }