Ejemplo n.º 1
0
        public async Task <PaymentResponseModel> ProcessAsync(PaymentRequestModel request, MerchantModel merchant)
        {
            _paymentRequestModelValidator.ValidateAndThrow(request);
            _merchantModelValidator.ValidateAndThrow(merchant);

            var merchantAcquirer = await _merchantAcquirerRepository.GetByMerchantIdAsync(merchant.Id);

            if (merchantAcquirer == null)
            {
                throw new GatewayException($"No acquirer setup for merchant '{ merchant.Name }'.");
            }

            // Get the appropriate processor from the AcquirerName.
            var enumAsString = (ProcessorList)Enum.Parse(typeof(ProcessorList), merchantAcquirer.AcquirerName);

            _processor = _processors[enumAsString];

            // Create and insert payment details
            var payment = BuildPaymentEntity(request, merchantAcquirer);
            await _paymentRepository.InsertAsync(payment);

            // Create processor request
            var processorRequest = new ProcessorRequest
            {
                PaymentId       = payment.PaymentId.ToString(),
                PaymentDetails  = _mapper.Map <PaymentDetails>(request),
                AcquirerDetails = _mapper.Map <AcquirerDetails>(merchantAcquirer)
            };

            var processorResponse = await _processor.ProcessPaymentAsync(processorRequest);

            var responseCodeMapping = await _acquirerResponseCodeMappingRepository.GetByAcquirerResponseCodeAsync(processorResponse.AcquirerResponseCode);

            var response = _mapper.Map <PaymentResponseModel>(processorResponse);

            response.TrackId      = request.TrackId;
            response.ResponseCode = responseCodeMapping?.GatewayResponseCode ?? Constants.FailResponseCode;
            response.Status       = response.ResponseCode.Equals(Constants.SuccessResponseCode) ? Constants.SuccessStatus : Constants.FailStatus;
            response.Card.Number  = MaskHelper.MaskCardNumber(response.Card.Number);

            // Map response details to the payment and update data store
            _mapper.Map(response, payment);
            await _paymentRepository.UpdateAsync(payment);

            return(response);
        }