Beispiel #1
0
        /// <summary>
        /// Processes the pending payment
        /// Without knowing the internals of the bank, we can assume a response containing the success along with a unique identifier
        /// This has been abstracted into the IBankServiceClient
        /// </summary>
        /// <param name="submitPaymentCommand">The payment command.</param>
        /// <returns></returns>
        public async Task ProcessPaymentAsync(SubmitPaymentCommand submitPaymentCommand)
        {
            _logger.LogInformation("Processing payment Id: {0}", submitPaymentCommand.PaymentId);

            using (var operation = _telemetrySubmitter.BeginTimedOperation(new ServiceOperation(nameof(CreatePaymentProcessor), nameof(ProcessPaymentAsync))))
            {
                try
                {
                    var payment = await _paymentRepository.GetByIdAsync(submitPaymentCommand.PaymentId);

                    var result = await _bankServiceClient.CreateOrderAsync(submitPaymentCommand);

                    payment.PaymentStatus = result.IsSuccessful ? PaymentStatus.Success : PaymentStatus.Failed;

                    _logger.LogInformation("Setting payment Id: {0} as {1}", submitPaymentCommand.PaymentId, payment.PaymentStatus);

                    payment.BankTransactionId = result.Id;

                    await _paymentRepository.UpdateAsync(payment);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Failed to CreatePayment due to {0}", ex.Message);
                    operation.SetFaulted(ex);
                }
            }
        }
        public async Task SubmitPayment_GivenCorrectPaymentInfo_ShouldReturnSuccess()
        {
            // Arrange
            var client = await _factory.GetAuthenticatedClientAsync(TestMerchantID, TestApiKey);

            var generatedPaymentOrderID = await GeneratePayment(client);

            var submitCommand = new SubmitPaymentCommand()
            {
                CardName    = "Test CardName",
                CvvCode     = "545",
                CardNumber  = "5555555555554444",
                ExpiryMonth = 12,
                ExpiryYear  = 2024,
                PaymentID   = generatedPaymentOrderID
            };

            // Act
            var submittedResponse = await client.PostAsync($"/api/submit-payment", Utils.GetRequestContent(submitCommand));

            var payment = await Utils.GetResponseContent <SubmitPaymentResultWm>(submittedResponse);

            // Assert
            payment.OrderID.Should().NotBeNullOrEmpty();
            payment.ResponseCode.Should().BeOneOf(
                PaymentProcessEnum.RequestFuturePayment.Id,
                PaymentProcessEnum.PaymentFailed.Id,
                PaymentProcessEnum.PaymentSucceeded.Id);
            submittedResponse.StatusCode.Should().Be(HttpStatusCode.OK);
        }
        public async Task CompletePayment(SubmitPaymentCommand command)
        {
            await Task.Delay(1000);

            await _bus.Publish(new PaymentCompletedEvent
            {
                PaymentId     = command.Id,
                BankPaymentId = Guid.NewGuid(),
                Success       = new Random().Next(0, 2) == 0
            });
        }
Beispiel #4
0
        public async Task StartAsync_HappyPath_CallsManager()
        {
            // arrange
            SubmitPaymentCommand command = Fixture.Create <SubmitPaymentCommand>();

            CommandQueueMock.Setup(m => m.DequeueAsync(It.IsAny <CancellationToken>())).ReturnsAsync(command);
            CreatePaymentManagerMock
            .Setup(m => m.ProcessPaymentAsync(command))
            .Callback <SubmitPaymentCommand>(t => SUT.Dispose())
            .Returns(Task.CompletedTask);

            // act
            await SUT.StartAsync(CancellationToken.None);

            // assert
            CreatePaymentManagerMock.Verify(m => m.ProcessPaymentAsync(command));
        }
        public async Task SubmitPayment_GivenInvalidCardNumber_ShouldThrownValidationException(string cardNumber)
        {
            // Arrange
            var client = await _factory.GetAuthenticatedClientAsync(TestMerchantID, TestApiKey);

            var generatedPaymentOrderID = await GeneratePayment(client);

            var submitCommand = new SubmitPaymentCommand()
            {
                CardName    = "Test CardName",
                CvvCode     = "545",
                CardNumber  = cardNumber,
                ExpiryMonth = 12,
                ExpiryYear  = 2024,
                PaymentID   = generatedPaymentOrderID
            };

            // Act
            var submittedResponse = await client.PostAsync($"/api/submit-payment", Utils.GetRequestContent(submitCommand));

            // Assert
            submittedResponse.StatusCode.Should().Be(HttpStatusCode.BadRequest);
        }
 public BankOperationModel Submit(SubmitPaymentCommand command)
 {
     EnsureIsValid(command);
     EnsureIsSecure <SubmitPaymentCommand, CardSecurityValidator>(command);
     try
     {
         var userCard    = _deps.UserCards.SurelyFind(command.FromCardId);
         var template    = _deps.PaymentTemplates.SurelyFind(command.TemplateCode);
         var payment     = _deps.CardPaymentFactory.Create(userCard, template, command.Form);
         var paymentLink = new PaymentTransactionLink(payment.Withdrawal, payment.Order);
         _deps.Payments.Create(payment);
         _deps.PaymentTransactionLinks.Create(paymentLink);
         var userOperation = new UserBankOperation(payment, Identity.User);
         _deps.UserBankOperations.Create(userOperation);
         Commit();
         var model = payment.ToModel <BankOperation, BankOperationModel>();
         Publish(new OperationProgressEvent(Operation.Id, model));
         return(model);
     }
     catch (Exception ex)
     {
         throw new ServiceException("Can't submit payment.", ex);
     }
 }
Beispiel #7
0
 public IHttpActionResult Submit(SubmitPaymentCommand command)
 {
     return(Ok(_paymentService.Submit(command)));
 }
Beispiel #8
0
        public async Task <IActionResult> SubmitPayment([FromBody] SubmitPaymentCommand model)
        {
            var result = await _mediator.Send(model);

            return(Ok(result));
        }
Beispiel #9
0
 public Task <BankPaymentResponse> CreateOrderAsync(SubmitPaymentCommand submitPaymentCommand)
 {
     return(Task.FromResult(new BankPaymentResponse {
         IsSuccessful = true, Id = Guid.NewGuid()
     }));
 }