Beispiel #1
0
        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 Save_Valid_ProductInvoice()
        {
            try
            {
                ProductInvoice productInvoice = new ProductInvoice
                {
                    State    = InvoiceState.Generated,
                    ClientId = "12345678",
                };

                productInvoice.CalculateTotal();

                await _productInvoicesRepository.Insert(productInvoice);

                await _dbContext.SaveChangesAsync();

                Assert.Pass();
            }
            catch (DbUpdateException e)
            {
                Assert.Fail(e.Message);
            }
        }