示例#1
0
        public async Task Handle_BankResponseRejected_SendsProcessRejectedPaymentRequest()
        {
            //arrange
            var merchantId = _fixture.Create <string>();

            _merchantContext.Setup(x => x.GetMerchantId()).Returns(merchantId);

            var transferFundsErrorResponse = _fixture.Create <TransferFundsErrorResponse>();

            _mockBankApiClient
            .Setup(x => x.TransferFunds(It.IsAny <TransferFundsRequest>()))
            .ReturnsAsync(TransferFundsResponse.Error(StatusCodes.Status422UnprocessableEntity, transferFundsErrorResponse));

            var request = _fixture.Create <CreatePaymentRequest>();

            var expected = new ProcessRejectedPaymentRequest
            {
                Source = new ProcessRejectedPaymentRequest.PaymentSource
                {
                    Cvv        = request.Source.Cvv,
                    CardExpiry = request.Source.CardExpiry,
                    CardNumber = request.Source.CardNumber,
                },
                Recipient = new ProcessRejectedPaymentRequest.PaymentRecipient
                {
                    SortCode      = request.Recipient.SortCode,
                    AccountNumber = request.Recipient.AccountNumber
                },
                Currency = request.Currency,
                Amount   = request.Amount,
                Merchant = new ProcessRejectedPaymentRequest.MerchantDetails
                {
                    Id = merchantId,
                },
                BankResponse = new ProcessRejectedPaymentRequest.BankPaymentResponse
                {
                    FailureReason = transferFundsErrorResponse.Code
                }
            };

            //act
            await _createPaymentHandler.Handle(request);

            //assert
            _mediator.Verify(x => x.Send(It.Is <ProcessRejectedPaymentRequest>(req => req.Should().BeEquivalentToBool(expected)), It.IsAny <CancellationToken>()), Times.Once);
        }
示例#2
0
        public async Task Handle_BankResponseRejected_ReturnsRejectedStatus()
        {
            //arrange
            var processRejectedPaymentResponse = _fixture.Create <ProcessRejectedPaymentResponse>();

            _mediator
            .Setup(x => x.Send(It.IsAny <ProcessRejectedPaymentRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(processRejectedPaymentResponse);

            _mockBankApiClient
            .Setup(x => x.TransferFunds(It.IsAny <TransferFundsRequest>()))
            .ReturnsAsync(TransferFundsResponse.Error(422, _fixture.Create <TransferFundsErrorResponse>()));

            //act
            var res = await _createPaymentHandler.Handle(_fixture.Create <CreatePaymentRequest>());

            //assert
            res.StatusCode.Should().Be(StatusCodes.Status201Created);
            res.SuccessResponse.Status.Should().Be(PaymentStatus.Rejected);
            res.SuccessResponse.PaymentId.Should().Be(processRejectedPaymentResponse.Id);
        }
        private async Task <TransferFundsResponse> TransferFundsResponseInternal(TransferFundsRequest request)
        {
            var response = await _httpClient.PostAsync(
                "api/payment",
                new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"));

            var responseBody = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                if (response.StatusCode == (HttpStatusCode)HttpStatusCodeUnprocessableEntity)
                {
                    return(TransferFundsResponse.Error((int)response.StatusCode, JsonConvert.DeserializeObject <TransferFundsErrorResponse>(responseBody)));
                }
                else
                {
                    return(TransferFundsResponse.UnknownError((int)response.StatusCode));
                }
            }

            var deserialized = JsonConvert.DeserializeObject <TransferFundsSuccessfulResponse>(responseBody);

            return(TransferFundsResponse.Successful(deserialized));
        }