Exemplo n.º 1
0
        /// <summary>
        /// Processes a payment request, verifying payment details with the bank and returning the result of the payment request
        /// </summary>
        /// <param name="paymentDto">The DTO representing a PaymentRequestDto</param>
        /// <returns>A PaymentResponseDto, which contains the payment identifier and status.</returns>
        public PaymentResponseDto MakePayment(PaymentRequestDto paymentDto)
        {
            _logger.LogInformation("Received a Payment request for " + paymentDto.Amount.ToString() + " " + paymentDto.Currency + ".");

            Payment payment = _mapper.Map <Payment>(paymentDto);

            payment.Card = _cardService.FindOrCreateCard(payment.Card);

            BankPaymentRequestDto  bankPaymentRequest  = _mapper.Map <BankPaymentRequestDto>(payment);
            BankPaymentResponseDto bankPaymentResponse = _bankService.PostPayment(bankPaymentRequest);

            if (bankPaymentResponse.ReasonCode == -1)
            {
                _logger.LogInformation("Payment rejected by bank.");

                payment.Status = PaymentStatus.Failed;
            }
            else
            {
                _logger.LogInformation("Payment accepted by bank!");

                payment.Status = PaymentStatus.Success;
            }

            payment.BankTransactionId = bankPaymentResponse.BankTransactionId;
            payment.PaymentDate       = DateTime.Now;
            Payment newPayment = _paymentRepository.InsertPayment(payment);

            _logger.LogInformation("Payment successfully processed.");

            PaymentResponseDto newPaymentDto = _mapper.Map <PaymentResponseDto>(newPayment);

            return(newPaymentDto);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Posts a payment request to the bank. Currently the status is determined randomly until functionality is swapped out for a real bank API call.
        /// </summary>
        /// <param name="payment">The payment request</param>
        /// <returns>A BankPaymentResponseDto, which contains the response from the bank</returns>
        public BankPaymentResponseDto PostPayment(BankPaymentRequestDto payment)
        {
            _logger.LogInformation("Submitting payment request to bank for processsing.");

            Random random = new Random();

            BankPaymentResponseDto dto = new BankPaymentResponseDto();

            dto.BankTransactionId = random.Next(1000, 100000);

            bool successfulTransaction = random.Next(1, 10) < 8 ? true : false;

            if (!successfulTransaction)
            {
                dto.Reason     = "Failure";
                dto.ReasonCode = -1;
            }
            else
            {
                dto.Reason     = "Success";
                dto.ReasonCode = 1;
            }

            return(dto);
        }
Exemplo n.º 3
0
        public async Task <ResponseDto> Process(PaymentRequestDto dto)
        {
            string      paymentProviderUniqueToken = _configuration[Constants.PaymentProviderUniqueToken];
            ResponseDto result = new ResponseDto();

            //save request in Db
            Request request = _mapper.Map <Request>(dto);
            await _context.Requests.AddAsync(request);

            //validate merchant
            Merchant merchant = _context.Merchants.SingleOrDefault(x => x.MerchantUniqueToken == dto.MerchantUniqueToken);

            if (merchant is null)
            {
                result.Status     = PaymentStatus.Failed;
                result.ResultCode = ResultCode.PaymentProviderError;
                return(result);
            }

            //send request to bank
            BankPaymentRequestDto requestToBank = _mapper.Map <BankPaymentRequestDto>(dto);

            requestToBank.PaymentProviderUniqueToken = new Guid(paymentProviderUniqueToken);

            string        json = JsonSerializer.Serialize(requestToBank);
            StringContent data = new StringContent(json, Encoding.UTF8, "application/json");

            using var response = await _httpClient.PostAsync("Operations", data);

            if (!response.IsSuccessStatusCode)
            {
                var exMessage = await response.Content.ReadAsStringAsync();

                throw new Exception(exMessage);
            }

            using var stream = await response.Content.ReadAsStreamAsync();

            result = await JsonSerializer.DeserializeAsync <ResponseDto>(stream,
                                                                         new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

            if (result.ResultCode != ResultCode.Success)
            {
                return(result);
            }

            //save changes in db
            Guid paymentId = await UpdatePaymentData(dto, result);

            request.Status = result.Status;
            await _context.SaveChangesAsync();

            result.OperationId = paymentId;
            return(result);
        }
        public Task <BankPaymentResponseDto> ProcessPaymentRequest(BankPaymentRequestDto paymentRequestDto)
        {
            return(Task.Run(() =>
            {
                //Let's imagine that this bank processes Visa only
                if (paymentRequestDto.CardNumber.StartsWith("4"))
                {
                    return new BankPaymentResponseDto()
                    {
                        IsProcessed = true,
                        ProcessingId = Guid.NewGuid(),
                        ProcessingDate = DateTime.UtcNow
                    };
                }

                return new BankPaymentResponseDto()
                {
                    IsProcessed = false
                };
            }));
        }