public Payment ProcessPayment(Payment payment)
        {
            _repository.CreatePayment(payment);
            PaymentState paymentResponse;

            if (payment.Amount < Constants.CheapAmountUpperBound)
            {
                paymentResponse = _cheapPaymentGateway.Charge(payment);
            }
            else if (payment.Amount >= Constants.CheapAmountUpperBound && payment.Amount <= Constants.ExpensiveAmountUpperBound)
            {
                paymentResponse = _expensivePaymentGateway.Charge(payment);
                if (paymentResponse.Status != PaymentStatusTypes.Processed)
                {
                    paymentResponse = _cheapPaymentGateway.Charge(payment);
                }
            }
            else if (payment.Amount > Constants.ExpensiveAmountUpperBound)
            {
                int count = 0;
                do
                {
                    paymentResponse = _premiumPaymentService.Charge(payment);
                    count++;
                }while(
                    count < Constants.PremiumPaymentServiceCount &&
                    paymentResponse.Status != PaymentStatusTypes.Processed
                    );
            }
            return(payment);
        }
Пример #2
0
        public Result Execute(RefillBalanceInput input)
        {
            Result <MoneyToCharge> moneyToCharge = MoneyToCharge.Create(input.MoneyAmount);

            Result <Customer> customer = _customerRepository.Get(input.CustomerId).ToResult("Customer doesn't exist.");

            return(Result.Combine(moneyToCharge, customer)
                   .OnSuccess(() => customer.Value.AddBalance(moneyToCharge.Value.Value).ToResult("For increment the current balance the value at increment must be greater than 100"))
                   .OnSuccess(() => _paymentGateway.Charge(customer.Value.BillingInfo, moneyToCharge.Value.Value))
                   .OnSuccess(() => _uow.Commit().OnFailure(() => _paymentGateway.Rollback()))
                   .OnBoth(() => _log.Info("RefillBalanceCommand has been executed.")));
        }
Пример #3
0
        public ChargeOperationResult Charge(PaymentChargeParameters paymentChargeParameters)
        {
            if (paymentChargeParameters == null)
            {
                throw new ArgumentNullException(nameof(paymentChargeParameters));
            }
            ValidatePaymentChargeParameters(paymentChargeParameters);
            var ch = PaymentGateway.Charge(
                paymentChargeParameters.Description,
                (int)(paymentChargeParameters.Amount * 100),
                paymentChargeParameters.CustomerId,
                paymentChargeParameters.GatewayCardId);

            if (ch["error"] != null)
            {
                return(new ChargeOperationResult {
                    Success = false, Message = ch.error.message.ToString()
                });
            }

            //log transaction details
            var charge = PaymentChargeRepository.Create();

            charge.ChargeDate = DateTime.Now;
            //LoginId is redundant, can be obtained through the payment, but we still save the FK here
            charge.LoginId         = paymentChargeParameters.CustomerId;
            charge.PaymentId       = paymentChargeParameters.PaymentId;
            charge.CreditCardId    = paymentChargeParameters.CardId;
            charge.Description     = paymentChargeParameters.Description;
            charge.Amount          = paymentChargeParameters.Amount;
            charge.ReferenceId     = paymentChargeParameters.PaymentId;
            charge.ReferenceType   = paymentChargeParameters.ReferenceType;
            charge.GatewayChargeId = ch["id"];
            charge.Result          = ch["outcome"]["type"].ToString();
            charge.ResultDetails   = JsonConvert.SerializeObject(ch["outcome"], Formatting.None);
            PaymentChargeRepository.Insert(charge);
            return(new ChargeOperationResult {
                Success = true, PaymentChargeId = charge.Id
            });
        }