Esempio n. 1
0
        public async Task <PaymentChangeStatusResponse> UpdatePaymentStatusAsync(PaymentChangeStatusRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var payment = _paymentRepository.GetAll().FirstOrDefault(p => p.Id == request.Id);

            if (payment == null)
            {
                throw new InvalidOperationException(nameof(payment));
            }

            var validationResult = _paymentValidator.Validate(payment, request);

            if (validationResult.Result != PaymentChangeStatusResult.Ok)
            {
                return(validationResult);
            }

            payment.Status            = request.Status;
            payment.StatusDescription = Enum.GetName(typeof(PaymentStatus), request.Status);
            payment.Reason            = request.Reason;

            _paymentRepository.Update(payment);
            await _unitOfWork.CommitAsync();

            return(validationResult);
        }
Esempio n. 2
0
        public bool ProcessPayment(PaymentDTO paymentDTO)
        {
            bool result = false;

            if (PaymentValidatorComponent.Validate(paymentDTO))
            {
                PaymentEO paymentEO = GetPaymentEO(paymentDTO);
                UpdateStatus(paymentEO, PaymentStatus.Pending);
                try
                {
                    result = ProcessPaymentWithGateway(paymentDTO);
                    if (result)
                    {
                        UpdateStatus(paymentEO, PaymentStatus.Processed);
                    }
                    else
                    {
                        UpdateStatus(paymentEO, PaymentStatus.Failed);
                    }
                }
                catch (Exception exe)
                {
                    AddError(PaymentErrorCode.Internal_Error, exe.Message);
                }
            }
            return(result);
        }
        public void AndAValidPaymentRequestIsSupplied_ReturnsTrue()
        {
            //Arrange
            var payment = CreateValidPayment();

            //Act
            var result = _validator.Validate(payment);

            //Assert
            result.IsValid.Should().Be(true);
        }
        public async Task <ClientResponse <PaymentResult> > ProcessPaymentAsync(PaymentRequest paymentRequest)
        {
            if (paymentRequest == null)
            {
                throw new ArgumentNullException(nameof(paymentRequest));
            }

            var validationResult = _paymentValidator.Validate(paymentRequest);

            if (!validationResult.IsValid)
            {
                return(ClientResponse <PaymentResult> .FromError(validationResult.ToString(), new PaymentResult(paymentRequest.PaymentRequestId)));
            }

            var payment = paymentRequest.ToPayment();

            try
            {
                await UpdatePaymentAsync(payment);

                var bankPaymentResult = await _bankPaymentClient.ProcessPaymentAsync(paymentRequest);

                payment.PaymentStatus      = bankPaymentResult.Status ? PaymentStatus.Succeed : PaymentStatus.Declined;
                payment.PaymentReferenceId = bankPaymentResult.PaymentReferenceId;

                await UpdatePaymentAsync(payment);

                var paymentResult = new PaymentResult(payment.PaymentRequestId, payment.PaymentReferenceId);
                return(bankPaymentResult.Status
                    ? ClientResponse <PaymentResult> .FromPayload(paymentResult)
                    : ClientResponse <PaymentResult> .FromError("Payment was unsuccessful.The bank returns unsuccessful response.", paymentResult));
            }
            catch (PaymentStoreException e)
            {
                _logger.LogError("Error in saving payment", e);

                // TODO: Can be handled in different strategies. E.g Revert the payment with bank or retry saving.

                return(ClientResponse <PaymentResult> .FromError("Payment was unsuccessful.", new PaymentResult(payment.PaymentRequestId, payment.PaymentReferenceId)));
            }
            catch (Exception e)
            {
                _logger.LogError("Error in process payment", e);

                return(ClientResponse <PaymentResult> .FromError("Payment was unsuccessful.", new PaymentResult(payment.PaymentRequestId)));
            }
        }