/// <inheritdoc /> /// <exception cref="UserNotFoundException">Thrown if the user is not found</exception> /// <exception cref="Exception">Thrown if error</exception> public async Task <TransactionViewModel> InjectPaymentMethod(InjectPaymentMethodBody injectPaymentMethodBody) { var user = await ApplicationContext.Users .Include(x => x.PaymentMethods) .FirstOrDefaultAsync(x => x.Id == injectPaymentMethodBody.UserId); var transaction = await ApplicationContext.Transactions.FirstOrDefaultAsync(x => x.StripeIntentId == injectPaymentMethodBody.IntentId); if (user == null) { Logger.LogInformation("User was not found"); throw new UserNotFoundException(); } if (user.PaymentMethods.All(x => x.StripeCardId != injectPaymentMethodBody.CardId)) { throw new Exception("User doesn't own card"); } if (transaction == null) { throw new Exception("Transaction not found"); } var service = new PaymentIntentService(); try { // request the to Stripe api with card id var intent = service.Update(injectPaymentMethodBody.IntentId, new PaymentIntentUpdateOptions { PaymentMethod = injectPaymentMethodBody.CardId }); // update transaction using Stripe response transaction.Updated = DateTime.Now; transaction.Status = Enum.Parse <PaymentStatus>(intent.Status, true); if (transaction.Status == PaymentStatus.succeeded) { transaction.Paid = true; transaction.End = DateTime.Now; } await ApplicationContext.SaveChangesAsync(); return(Mapper.Map <TransactionViewModel>(transaction)); } catch (Exception e) { Console.WriteLine(e); throw; } }
public async Task <ActionResult <TransactionViewModel> > InjectPaymentMethod(InjectPaymentMethodBody injectPaymentMethodBody) { try { Logger.LogInformation("Injecting payment into {Id}", injectPaymentMethodBody.IntentId); var result = await TransactionService.InjectPaymentMethod(injectPaymentMethodBody); return(result); } catch (Exception e) { Logger.LogError(e.ToString()); return(BadRequest(e)); } }