Exemplo n.º 1
0
        //
        // Private
        //
        private async Task <IActionResult> ProcessPaymentOptionsPost(Guid lowellReferenceSurrogateKey,
                                                                     PaymentOptionsVm paymentOptionsVmWithUserEntries)
        {
            if (!ModelState.IsValid)
            {
                var outstandingPayment = paymentOptionsVmWithUserEntries.FullPaymentAmountDerived;

                bool isDirectDebitPayment =
                    paymentOptionsVmWithUserEntries.SelectedPlanSetupOption == PlanSetupOptions.AverageSetupValue ||
                    paymentOptionsVmWithUserEntries.SelectedPlanSetupOption == PlanSetupOptions.DisposableIncome ||
                    paymentOptionsVmWithUserEntries.SelectedPlanSetupOption == PlanSetupOptions.OtherPaymentOffer;
                if (isDirectDebitPayment && paymentOptionsVmWithUserEntries.DirectDebitAmount > outstandingPayment)
                {
                    ModelState.Clear();
                    paymentOptionsVmWithUserEntries.DirectDebitAmount = null;
                    ModelState.AddModelError(nameof(PaymentOptionsVm.SelectedPlanSetupOption),
                                             $"{ValidationMessages.AmountGreaterThenAllowed}{outstandingPayment}");
                }
                else if (!isDirectDebitPayment && paymentOptionsVmWithUserEntries.PartialPaymentAmount.HasValue &&
                         paymentOptionsVmWithUserEntries.PartialPaymentAmount.Value >
                         outstandingPayment)
                {
                    ModelState.Clear();
                    paymentOptionsVmWithUserEntries.PartialPaymentAmount = null;
                    ModelState.AddModelError(nameof(PaymentOptionsVm.PartialPaymentAmount),
                                             $"{ValidationMessages.AmountGreaterThenAllowed}{outstandingPayment}");
                }
                else if (!isDirectDebitPayment && paymentOptionsVmWithUserEntries.PartialPaymentAmount.HasValue &&
                         paymentOptionsVmWithUserEntries.PartialPaymentAmount.Value < 1.0m
                         )
                {
                    ModelState.Clear();
                    paymentOptionsVmWithUserEntries.PartialPaymentAmount = null;
                    ModelState.AddModelError(nameof(PaymentOptionsVm.PartialPaymentAmount),
                                             $"{ValidationMessages.AmountLessThanOneGbp}");
                }

                Debug.Assert(paymentOptionsVmWithUserEntries.InitialState != null, "Initial state must be stashed to re-create view on round-trip");
                return(View(paymentOptionsVmWithUserEntries));
            }

            if (paymentOptionsVmWithUserEntries.SelectedPaymentOption ==
                PaymentOptionsSelectionsVm.Values.PartialPayment ||
                paymentOptionsVmWithUserEntries.SelectedPaymentOption ==
                PaymentOptionsSelectionsVm.Values.FullPayment)
            {
                OneOffPaymentReviewVm oneOffPaymentReviewVm;
                {
                    //
                    // IMPORTANT: Validates models again versus CaseFlow, to ensure no tamper or state changes since
                    // started process of entering payment information
                    //
                    var result = await _buildOneOffPaymentReviewVmService.ValidateAndBuild(
                        LoggedInUser, ApplicationSessionState, lowellReferenceSurrogateKey,
                        paymentOptionsVmWithUserEntries, GetCaseflowUserId());

                    if (result.IsValid == false)
                    {
                        return(View("ValidationFailed"));
                    }

                    oneOffPaymentReviewVm = result.OneOffPaymentReviewVm;
                }

                // Stash state of payment options form, so that 'change' feature can return
                oneOffPaymentReviewVm.FilledInPaymentOptionsState =
                    SerialiseModel(paymentOptionsVmWithUserEntries);

                var oneOffPaymentDto = _mapper.Map <OneOffPaymentReviewVm, OneOffPaymentDto>(oneOffPaymentReviewVm);

                _logger.LogDebug("Before AddVerifoneTransactionAsync");

                await _verifonePaymentProviderService.AddVerifoneTransactionAsync(
                    new VerifoneTransactionDto
                {
                    CompanyCode     = 1, //LFL
                    TransactionGuid = oneOffPaymentReviewVm.VerifoneTransactionGuid,
                    TransactionData = JsonConvert.SerializeObject(oneOffPaymentDto)
                });

                _gtmService.RaiseOneOffPaymentEvent_OptionsSelected(oneOffPaymentReviewVm, LoggedInUserId,
                                                                    "Regular Account");

                await _webActivityService.LogOneOffPaymentSelected(oneOffPaymentReviewVm.LowellReference,
                                                                   LoggedInUserId, !oneOffPaymentReviewVm.PaidInFull, oneOffPaymentReviewVm.DiscountSelected);

                // Ensure no model state is used in view render
                ModelState.Clear();

                ApplicationSessionState.LogPaymentResult = true;
                return(View("OneOff", oneOffPaymentReviewVm));
            }

            if (paymentOptionsVmWithUserEntries.SelectedPaymentOption == PaymentOptionsSelectionsVm.Values.DirectDebit)
            {
                // Not validated here - occurs on the final step (DirectDebitComplete)

                var directDebitDetailsVm =
                    _buildDirectDebitDetailsVmService.Build(paymentOptionsVmWithUserEntries);

                // Stash model in state, to allow view to be rebuilt on postback
                directDebitDetailsVm.InitialState = SerialiseModel(directDebitDetailsVm);

                // Stash payment options VM in Direct Debit VM to allow it to be passed back
                // (e.g. if user clicks 'change' and goes back to Payment Options)
                directDebitDetailsVm.PaymentOptionsFilledInState =
                    SerialiseModel(paymentOptionsVmWithUserEntries);

                if (directDebitDetailsVm.DiscountSelected)
                {
                    await LogDiscountDirectDebitOptionsSelected(paymentOptionsVmWithUserEntries);
                }
                else
                {
                    await LogDirectDebitOptionsSelected(paymentOptionsVmWithUserEntries);
                }

                // TODO: change over to building and returning view directly
                _gtmService.RaiseDirectDebitEvent_OptionsSelected(directDebitDetailsVm, LoggedInUserId,
                                                                  "Regular Account");

                ApplicationSessionState.LogSetUpPlanResult = true;

                return(View("DirectDebit", directDebitDetailsVm));
            }

            throw new ApplicationException("Invalid SelectedPaymentOption");
        }