Exemple #1
0
        //Processando o pagamento utilizando microsserviço com mensageria da RabbitMQ
        public void ProcessPayment(PaymentInfoDTO paymentInfoDTO)
        {
            var paymentInfoJson  = JsonSerializer.Serialize(paymentInfoDTO);
            var paymentInfoBytes = Encoding.UTF8.GetBytes(paymentInfoJson);

            _messageBusService.Publish(QUEUE_NAME, paymentInfoBytes);
        }
Exemple #2
0
        //Comunicação com microsserviço com protocolo HTTP - Forma síncrona
        //public async Task<bool> Handle(FinishProjectCommand request, CancellationToken cancellationToken)
        //{
        //    //Padrão CQRS
        //    //var project = await _dbContext.Projects.SingleOrDefaultAsync(p => p.Id == request.Id);

        //    //project.Finish();

        //    //Entity Framework - Padrão CQRS
        //    //await _dbContext.SaveChangesAsync();

        //    //Dapper
        //    //using (var sqlConnection = new SqlConnection(_connectionString))
        //    //{
        //    //    sqlConnection.Open();

        //    //    var sql = "UPDATE Projects SET Status = @status, FinishedAt = @finishedat WHERE Id = @id";

        //    //    sqlConnection.Execute(sql, new { status = project.Status, finishedat = project.FinishedAt, Id = project.Id });
        //    //}

        //    //Padrão Repository
        //    var project = await _projectRepository.GetByIdAsync(request.Id);

        //    project.Finish();

        //    //Processando o pagamento no microsserviço
        //    var paymentInfoDto = new PaymentInfoDTO(request.Id, request.CreditCardNumber, request.Cvv, request.ExpiresAt, request.FullName, project.TotalCost);

        //    var result = await _paymentService.ProcessPayment(paymentInfoDto);

        //    if (!result)
        //    {
        //        project.SetPaymentPending();
        //    }

        //    await _projectRepository.SaveChangesAsync();

        //    return true;
        //}

        //Comunicação com microsserviço com mensageria - Forma assíncrona
        public async Task <bool> Handle(FinishProjectCommand request, CancellationToken cancellationToken)
        {
            var project = await _projectRepository.GetByIdAsync(request.Id);

            var paymentInfoDto = new PaymentInfoDTO(request.Id, request.CreditCardNumber, request.Cvv, request.ExpiresAt, request.FullName, project.TotalCost);

            _paymentService.ProcessPayment(paymentInfoDto);

            project.SetPaymentPending();

            await _projectRepository.SaveChangesAsync();

            return(true);
        }
        public PaymentInfoDTO GetPaymentStatus(string authKey, int orderId)
        {
            using (var uow = UnitOfWorkFactory.CreateWithoutRoot($"[ADS] GetPaymentStatus method")) {
                PaymentInfoDTO result = new PaymentInfoDTO(orderId, PaymentStatus.None);

                if (!CheckAuth(uow, authKey))
                {
                    return(result);
                }


                ISmsPaymentService smsPaymentService = smsPaymentChannelFactory.CreateChannel();
                if (smsPaymentService == null)
                {
                    logger.Warn($"Невозможно получить статус платежа. {nameof(smsPaymentService)} is null");
                    return(result);
                }

                PaymentResult paymentResult = null;
                try {
                    paymentResult = smsPaymentService.GetActualPaymentStatus(orderId);
                }
                catch (Exception ex) {
                    logger.Error(ex);
                }
                if (paymentResult == null || paymentResult.Status == PaymentResult.MessageStatus.Error || paymentResult.PaymentStatus == null)
                {
                    result.Status = PaymentStatus.None;
                    return(result);
                }

                switch (paymentResult.PaymentStatus.Value)
                {
                case SmsPaymentStatus.WaitingForPayment:
                    result.Status = PaymentStatus.WaitingForPayment;
                    break;

                case SmsPaymentStatus.Paid:
                    result.Status = PaymentStatus.Paid;
                    break;

                case SmsPaymentStatus.Cancelled:
                    result.Status = PaymentStatus.Cancelled;
                    break;
                }

                return(result);
            }
        }