public async Task <bool> ProcessTransaction(CardDetail cardDetail, PaymentState paymentStatus) { premiumTried = 0; _paymentStatus = paymentStatus; try { bool states; if (CardVerification(cardDetail)) { switch (cardDetail.Amount) { case <= 20: states = await CheapTransaction(cardDetail); break; case > 20 and <= 500: states = await ExpensiveTransaction(cardDetail); break; default: states = await PremiumTransaction(cardDetail); break; } _paymentStatus.StateEnum = states ? PaymentStateEnum.Processed : PaymentStateEnum.Failed; _context.PaymentState.Update(_paymentStatus); await _context.SaveChangesAsync(); } else { throw new Exception(); } return(states); } catch (Exception e) { _paymentStatus.StateEnum = PaymentStateEnum.Failed; _context.PaymentState.Update(_paymentStatus); _context.SaveChangesAsync(); Console.WriteLine(e); return(false); } }
public async Task <ActionResult <bool> > ProcessPayment(CardDetail cardDetail) { try { if (cardDetail == null) { throw new Exception(); } var paymentStatus = new PaymentState(); paymentStatus.StateEnum = PaymentStateEnum.Pending; _context.PaymentState.Add(paymentStatus); await _context.SaveChangesAsync(); var isProcessed = await _paymentGatewayRepo.ProcessTransaction(cardDetail, paymentStatus); if (isProcessed) { return(Ok("Payment is processed")); } return(BadRequest(new { message = "The request is invalid" })); } catch (Exception e) { return(StatusCode(500, new { error = e.Message, message = "Lost connection to payment platform, Please try back later" })); } }
public void ProcessPaymentWhenInit(CardDetail cardDetail) //[ValueSource("CCVerificationTestData")] { var paymentStatus = new PaymentState(); paymentStatus.StateEnum = PaymentStateEnum.Pending; _context.SaveChangesAsync(); var process = new PaymentGatwayRepo(_context).ProcessTransaction(cardDetail, paymentStatus).Result; if (process) { paymentStatus.StateEnum = PaymentStateEnum.Processed; } else { paymentStatus.StateEnum = PaymentStateEnum.Failed; } _context.SaveChangesAsync(); Assert.IsTrue(process, "Transaction Failed"); }