public string MakePayment(PaymentBM payment) { Payment newPayment = null; //Payment to this account var payeeAccount = _context.Account.Where(x => x.AccountNumber == payment.PayeeAccountNumber).FirstOrDefault(); //Payment from this account var payorAccount = _context.Account.Where(x => x.AccountNumber == payment.PayorAccountNumber).FirstOrDefault(); string transactionId = ""; if (payorAccount != null && payeeAccount != null) { var paymentMethodId = _context.PaymentMethod.Where(x => x.PaymentMethodCode == payment.PaymentMethod).Select(x => x.Id).FirstOrDefault(); var statusId = _context.PaymentStatus.Where(x => x.Status == PaymentSatus.Completed.ToString()).Select(x => x.Id).FirstOrDefault(); //ToDo: Below needs to be reviewed and removed if not needed transactionId = PaymentHelper.GenerateTransactionId(); if (_context.Payment.Any(x => x.TransactionId == transactionId)) { transactionId = PaymentHelper.GenerateTransactionId(); } payeeAccount.CurrentBalance = payment.TransactionType == TransactionType.Credit.ToString() ? payeeAccount.CurrentBalance + payment.Amount : payeeAccount.CurrentBalance - payment.Amount; payorAccount.CurrentBalance = payment.TransactionType == TransactionType.Credit.ToString() ? payorAccount.CurrentBalance - payment.Amount : payorAccount.CurrentBalance + payment.Amount; newPayment = new Payment { PayorAccountId = payorAccount.Id, PayorCustomerId = payorAccount.CustomerId.Value, PayeeAccountId = payeeAccount.Id, PayeeCustomerId = payeeAccount.CustomerId.Value, PaymentMethodId = paymentMethodId, PaymentStatusId = statusId, PaymentAmount = payment.Amount, TransactionId = transactionId, PaymentDate = payment.PaymentDate.IsDate() ? Convert.ToDateTime(payment.PaymentDate) : DateTime.Now, TransactionType = payment.TransactionType, Remarks = $"Amount {payment.Amount.ToString()} has been {payment.TransactionType}ed to account {payment.PayeeAccountNumber}. The current balance is {payeeAccount.CurrentBalance} in Payee account {payment.PayeeAccountNumber}. The current balance is {payorAccount.CurrentBalance} in Payor account {payment.PayeeAccountNumber}." }; _context.Payment.Add(newPayment); _context.SaveChanges(); } return(newPayment != null && newPayment.Id != null ? transactionId : string.Empty); }
public void Post(PaymentBM value) { //ToDo: Clean-up //if(value == null) //{ // value = new PaymentBM // { // //payee gets payment from payor // PayeeAccountNumber = "70872490", // PayeeCurrency = PaymentEnums.Currency.GBP.ToString(), // //payor pay to payee // PayorAccountNumber = "35933158286", // PayorCurrency = PaymentEnums.Currency.INR.ToString(), // PaymentMethod = PaymentEnums.PaymentMethod.InternateBanking.ToString(), // Amount = 10000, // PaymentDate = DateTime.Now.ToString(), // TransactionType = PaymentEnums.TransactionType.Credit.ToString() // }; //} if (value != null) { if (string.IsNullOrEmpty(value.PayeeCurrency)) { value.PayeeCurrency = PaymentEnums.Currency.GBP.ToString(); } if (string.IsNullOrEmpty(value.PayorCurrency)) { value.PayorCurrency = PaymentEnums.Currency.INR.ToString(); } value.Amount = value.Amount * _paymentService.GetCurrencyExchangeRate(value.PayorCurrency, value.PayeeCurrency); value.PaymentDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); if (string.IsNullOrEmpty(value.PaymentMethod)) { value.PaymentMethod = PaymentEnums.PaymentMethod.InternateBanking.ToString(); } } _paymentService.MakePayment(value); }