示例#1
0
        public bool ProcessRequest(PaymentRequest PaymentRequest)
        {
            unitOfWork.BeginTransaction();

            try
            {
                unitOfWork.PaymentRequest.Add(PaymentRequest);
                unitOfWork.SaveChanges();

                unitOfWork.PaymentRequestState.Add(new PaymentRequestState {
                    PaymentRequestId = PaymentRequest.PaymentRequestId, State = (int)PaymentState.Pending, AdditionDate = DateTime.Now
                });
                unitOfWork.SaveChanges();

                bool paymentProccessed = false;

                if (PaymentRequest.Amount <= 20)
                {
                    paymentProccessed = cheapPaymentGateway.ProccessRequest(PaymentRequest);
                }
                else if (PaymentRequest.Amount >= 21 && PaymentRequest.Amount <= 500)
                {
                    if (expensivePaymentGateway.IsAvailable())
                    {
                        paymentProccessed = expensivePaymentGateway.ProccessRequest(PaymentRequest);
                    }
                    else
                    {
                        paymentProccessed = cheapPaymentGateway.ProccessRequest(PaymentRequest);
                    }
                }
                else
                {
                    paymentProccessed = premiumPaymentGateway.ProccessRequest(PaymentRequest, 1);
                }

                UpdateRequest(PaymentRequest.PaymentRequestId, paymentProccessed);

                unitOfWork.Commit();
                return(paymentProccessed);
            }
            catch
            {
                unitOfWork.RollBack();
                return(false);
            }
        }