From 0c2cf0c36c4782e43118a852941d2a77acef8298 Mon Sep 17 00:00:00 2001 From: Carlos Pavajeau Date: Mon, 18 Jan 2021 11:15:44 -0500 Subject: [PATCH] refactor(back-end): Use the new functionalities of MercadoPagoCore --- .../ProductInvoicesControllerTest.cs | 5 +- .../ServiceInvoicesControllerTest.cs | 5 +- .../Controllers/ProductInvoicesController.cs | 55 +++++++++--------- .../Controllers/ServiceInvoicesController.cs | 56 ++++++++++--------- 4 files changed, 66 insertions(+), 55 deletions(-) diff --git a/Kaizen.Test/Controllers/ProductInvoicesControllerTest.cs b/Kaizen.Test/Controllers/ProductInvoicesControllerTest.cs index 8137acfa..fe123f9f 100644 --- a/Kaizen.Test/Controllers/ProductInvoicesControllerTest.cs +++ b/Kaizen.Test/Controllers/ProductInvoicesControllerTest.cs @@ -8,6 +8,7 @@ using Kaizen.Domain.Repositories; using Kaizen.Models.ProductInvoice; using Kaizen.Test.Helpers; +using MercadoPagoCore.Client.Payment; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Moq; @@ -21,15 +22,17 @@ public class ProductInvoicesControllerTest : BaseControllerTest private ProductInvoicesController _productInvoicesController; private Mock _productInvoicesRepository; private Mock _unitWork; + private Mock _paymentClient; [SetUp] public void SetUp() { _productInvoicesRepository = new Mock(); _unitWork = new Mock(); + _paymentClient = new Mock(); _productInvoicesController = new ProductInvoicesController(_productInvoicesRepository.Object, - _unitWork.Object, ServiceProvider.GetService()); + _unitWork.Object, ServiceProvider.GetService(), _paymentClient.Object); SetUpProductInvoicesRepository(); SetUpUnitWork(); diff --git a/Kaizen.Test/Controllers/ServiceInvoicesControllerTest.cs b/Kaizen.Test/Controllers/ServiceInvoicesControllerTest.cs index 805352ef..3c1db84c 100644 --- a/Kaizen.Test/Controllers/ServiceInvoicesControllerTest.cs +++ b/Kaizen.Test/Controllers/ServiceInvoicesControllerTest.cs @@ -6,6 +6,7 @@ using Kaizen.Domain.Repositories; using Kaizen.Models.ServiceInvoice; using Kaizen.Test.Helpers; +using MercadoPagoCore.Client.Payment; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Moq; @@ -19,15 +20,17 @@ public class ServiceInvoicesControllerTest : BaseControllerTest private ServiceInvoicesController _serviceInvoicesController; private Mock _serviceInvoicesRepository; private Mock _unitWork; + private Mock _paymentClient; [SetUp] public void SetUp() { _serviceInvoicesRepository = new Mock(); _unitWork = new Mock(); + _paymentClient = new Mock(); _serviceInvoicesController = new ServiceInvoicesController(_serviceInvoicesRepository.Object, - _unitWork.Object, ServiceProvider.GetService()); + _unitWork.Object, ServiceProvider.GetService(), _paymentClient.Object); SetUpServiceInvoicesRepository(); SetUpUnitWork(); diff --git a/Kaizen/Controllers/ProductInvoicesController.cs b/Kaizen/Controllers/ProductInvoicesController.cs index dcc600b0..3e242e67 100644 --- a/Kaizen/Controllers/ProductInvoicesController.cs +++ b/Kaizen/Controllers/ProductInvoicesController.cs @@ -7,7 +7,8 @@ using Kaizen.Domain.Repositories; using Kaizen.Models.Base; using Kaizen.Models.ProductInvoice; -using MercadoPagoCore.Resources; +using MercadoPagoCore.Client.Payment; +using MercadoPagoCore.Resource.Payment; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -22,12 +23,14 @@ public class ProductInvoicesController : ControllerBase private readonly IProductInvoicesRepository _productInvoicesRepository; private readonly IUnitWork _unitWork; private readonly IMapper _mapper; + private readonly PaymentClient _paymentClient; - public ProductInvoicesController(IProductInvoicesRepository productInvoicesRepository, IUnitWork unitWork, IMapper mapper) + public ProductInvoicesController(IProductInvoicesRepository productInvoicesRepository, IUnitWork unitWork, IMapper mapper, PaymentClient paymentClient) { _productInvoicesRepository = productInvoicesRepository; _unitWork = unitWork; _mapper = mapper; + _paymentClient = paymentClient; } [HttpGet] @@ -53,7 +56,7 @@ public async Task> GetProductInvoice(int i [HttpGet("[action]/{id}")] public async Task>> ClientInvoices(string id) { - var productInvoices = await _productInvoicesRepository.GetClientInvoices(id); + IEnumerable productInvoices = await _productInvoicesRepository.GetClientInvoices(id); return Ok(_mapper.Map>(productInvoices)); } @@ -100,14 +103,14 @@ public async Task> Pay(int id, [FromBody] productInvoice.CalculateTotal(); - Payment payment = new Payment + PaymentCreateRequest paymentCreateRequest = new PaymentCreateRequest { Token = paymentModel.Token, PaymentMethodId = paymentModel.PaymentMethodId, - TransactionAmount = (float?)productInvoice.Total, + TransactionAmount = productInvoice.Total, Description = $"Pay or product invoice {id}", Installments = 1, - Payer = new MercadoPagoCore.DataStructures.Payment.Payer + Payer = new PaymentPayerRequest { FirstName = productInvoice.Client.FirstName, LastName = productInvoice.Client.LastName, @@ -115,34 +118,34 @@ public async Task> Pay(int id, [FromBody] } }; - if (payment.Save() && payment.Status == MercadoPagoCore.Common.PaymentStatus.approved) + Payment payment = await _paymentClient.CreateAsync(paymentCreateRequest); + + if (payment.Status == PaymentStatus.Rejected) { - productInvoice.State = InvoiceState.Paid; - productInvoice.PaymentDate = DateTime.Now; - productInvoice.PaymentMethod = Domain.Entities.PaymentMethod.CreditCard; + return BadRequest("El pago no pudo ser procesado."); + } - _productInvoicesRepository.Update(productInvoice); + productInvoice.State = InvoiceState.Paid; + productInvoice.PaymentDate = DateTime.Now; + productInvoice.PaymentMethod = PaymentMethod.CreditCard; - try - { - await _unitWork.SaveAsync(); - } - catch (DbUpdateConcurrencyException) + _productInvoicesRepository.Update(productInvoice); + + try + { + await _unitWork.SaveAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!ProductInvoiceExists(id)) { - if (!ProductInvoiceExists(id)) - { - return NotFound($"Error de actualizacón. No existe ninguna factura de producto con el código {id}."); - } - else - { - throw; - } + return NotFound($"Error de actualizacón. No existe ninguna factura de producto con el código {id}."); } - return _mapper.Map(productInvoice); + throw; } - return BadRequest(payment.Errors.Value); + return _mapper.Map(productInvoice); } [HttpPost] diff --git a/Kaizen/Controllers/ServiceInvoicesController.cs b/Kaizen/Controllers/ServiceInvoicesController.cs index a63de484..9b47c8c1 100644 --- a/Kaizen/Controllers/ServiceInvoicesController.cs +++ b/Kaizen/Controllers/ServiceInvoicesController.cs @@ -8,8 +8,8 @@ using Kaizen.Domain.Repositories; using Kaizen.Models.Base; using Kaizen.Models.ServiceInvoice; -using MercadoPagoCore.Common; -using MercadoPagoCore.Resources; +using MercadoPagoCore.Client.Payment; +using MercadoPagoCore.Resource.Payment; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -24,12 +24,14 @@ public class ServiceInvoicesController : ControllerBase private readonly IServiceInvoicesRepository _serviceInvoicesRepository; private readonly IUnitWork _unitWork; private readonly IMapper _mapper; + private readonly PaymentClient _paymentClient; - public ServiceInvoicesController(IServiceInvoicesRepository serviceInvoicesRepository, IUnitWork unitWork, IMapper mapper) + public ServiceInvoicesController(IServiceInvoicesRepository serviceInvoicesRepository, IUnitWork unitWork, IMapper mapper, PaymentClient paymentClient) { _serviceInvoicesRepository = serviceInvoicesRepository; _unitWork = unitWork; _mapper = mapper; + _paymentClient = paymentClient; } [HttpGet] @@ -102,14 +104,14 @@ public async Task> Pay(int id, [FromBody] serviceInvoice.CalculateTotal(); - Payment payment = new Payment + PaymentCreateRequest paymentCreateRequest = new PaymentCreateRequest { Token = paymentModel.Token, PaymentMethodId = paymentModel.PaymentMethodId, - TransactionAmount = (float?)serviceInvoice.Total, + TransactionAmount = serviceInvoice.Total, Description = $"Pay of service invoice {serviceInvoice.Id}", Installments = 1, - Payer = new MercadoPagoCore.DataStructures.Payment.Payer + Payer = new PaymentPayerRequest { FirstName = serviceInvoice.Client.FirstName, LastName = serviceInvoice.Client.LastName, @@ -117,35 +119,35 @@ public async Task> Pay(int id, [FromBody] } }; - if (payment.Save() && payment.Status == PaymentStatus.approved) + Payment payment = await _paymentClient.CreateAsync(paymentCreateRequest); + + if (payment.Status == PaymentStatus.Rejected) { - serviceInvoice.State = InvoiceState.Paid; - serviceInvoice.PaymentDate = DateTime.Now; - serviceInvoice.PaymentMethod = Domain.Entities.PaymentMethod.CreditCard; + return BadRequest("El pago no pudo ser procesado."); + } - serviceInvoice.PublishEvent(new PaidInvoice(serviceInvoice)); - _serviceInvoicesRepository.Update(serviceInvoice); + serviceInvoice.State = InvoiceState.Paid; + serviceInvoice.PaymentDate = DateTime.Now; + serviceInvoice.PaymentMethod = PaymentMethod.CreditCard; - try - { - await _unitWork.SaveAsync(); - } - catch (DbUpdateConcurrencyException) + serviceInvoice.PublishEvent(new PaidInvoice(serviceInvoice)); + _serviceInvoicesRepository.Update(serviceInvoice); + + try + { + await _unitWork.SaveAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!ServiceInvoiceExists(id)) { - if (!ServiceInvoiceExists(id)) - { - return NotFound($"Error de actualizacón. No existe ninguna factura de servicio con el código {id}."); - } - else - { - throw; - } + return NotFound($"Error de actualizacón. No existe ninguna factura de servicio con el código {id}."); } - return _mapper.Map(serviceInvoice); + throw; } - return BadRequest(payment.Errors.Value); + return _mapper.Map(serviceInvoice); } private bool ServiceInvoiceExists(int id)