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); }
public async Task <PaymentResponseModel> GetByIdAsync(Guid paymentId, MerchantModel merchant) { Guard.IsNotNull(paymentId, nameof(paymentId)); _merchantModelValidator.ValidateAndThrow(merchant); var payment = await _paymentRepository.GetByIdAsync(paymentId); if (payment == null) { throw new ObjectNotFoundException($"No payment with id '{ paymentId } was found.'"); } // This check should normally return a Not Found exception for security purposes. Unauthorized used for testing purposes only. if (payment.MerchantId != merchant.Id) { throw new UnauthorizedException($"Merchant is not authorised to retrieve this payment."); } var response = _mapper.Map <PaymentResponseModel>(payment); response.Card.Number = MaskHelper.MaskCardNumber(_cryptor.Decrypt(response.Card.Number)); return(response); }