public async Task <ActionResult <ProductInvoiceViewModel> > GetProductInvoice(int id) { ProductInvoice productInvoice = await _productInvoicesRepository.FindByIdAsync(id); if (productInvoice == null) { return(NotFound($"No existe ninguna factura de productos con el código {id}.")); } return(_mapper.Map <ProductInvoiceViewModel>(productInvoice)); }
public async Task <ActionResult <ProductInvoiceViewModel> > PayProductInvoice(int id, [FromBody] PaymentModel paymentModel) { ProductInvoice productInvoice = await _productInvoicesRepository.FindByIdAsync(id); if (productInvoice is null) { return(NotFound($"No existe ninguna factura de producto con el código {id}")); } productInvoice.CalculateTotal(); PaymentCreateRequest paymentCreateRequest = new PaymentCreateRequest { Token = paymentModel.Token, PaymentMethodId = paymentModel.PaymentMethodId, TransactionAmount = productInvoice.Total, Description = $"Pay for product invoice {id}", Installments = 1, Payer = new PaymentPayerRequest { FirstName = productInvoice.Client.FirstName, LastName = productInvoice.Client.LastName, Email = paymentModel.Email } }; Payment payment = await _paymentClient.CreateAsync(paymentCreateRequest); if (payment.Status == PaymentStatus.Rejected) { return(BadRequest("El pago no pudo ser procesado.")); } productInvoice.PublishEvent(new PaidInvoice(productInvoice)); productInvoice.State = InvoiceState.Paid; productInvoice.PaymentDate = DateTime.Now; productInvoice.PaymentMethod = PaymentMethod.CreditCard; _productInvoicesRepository.Update(productInvoice); try { await _unitWork.SaveAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductInvoiceExists(id)) { return(NotFound( $"Error de actualizacón. No existe ninguna factura de producto con el código {id}.")); } throw; } return(_mapper.Map <ProductInvoiceViewModel>(productInvoice)); }
public async Task Search_Non_Existent_ProductInvoice() { ProductInvoice productInvoice = await _productInvoicesRepository.FindByIdAsync(2); Assert.IsNull(productInvoice); }