Example #1
0
        public async Task <ActionResult <ServiceInvoiceViewModel> > GetServiceInvoice(int id)
        {
            ServiceInvoice serviceInvoice = await _serviceInvoicesRepository.FindByIdAsync(id);

            if (serviceInvoice == null)
            {
                return(NotFound($"No existe ninguna factura de servicio con el código {id}."));
            }

            return(_mapper.Map <ServiceInvoiceViewModel>(serviceInvoice));
        }
Example #2
0
        public async Task <ActionResult <ServiceInvoiceViewModel> > PayServiceInvoice(int id,
                                                                                      [FromBody] PaymentModel paymentModel)
        {
            ServiceInvoice serviceInvoice = await _serviceInvoicesRepository.FindByIdAsync(id);

            if (serviceInvoice is null)
            {
                return(NotFound($"No existe ninguna factura de servicio con el código {id}."));
            }

            serviceInvoice.CalculateTotal();

            PaymentCreateRequest paymentCreateRequest = new PaymentCreateRequest
            {
                Token             = paymentModel.Token,
                PaymentMethodId   = paymentModel.PaymentMethodId,
                TransactionAmount = serviceInvoice.Total,
                Description       = $"Pay of service invoice {serviceInvoice.Id}",
                Installments      = 1,
                Payer             = new PaymentPayerRequest
                {
                    FirstName = serviceInvoice.Client.FirstName,
                    LastName  = serviceInvoice.Client.LastName,
                    Email     = paymentModel.Email
                }
            };

            Payment payment = await _paymentClient.CreateAsync(paymentCreateRequest);

            if (payment.Status == PaymentStatus.Rejected)
            {
                return(BadRequest("El pago no pudo ser procesado."));
            }

            serviceInvoice.State         = InvoiceState.Paid;
            serviceInvoice.PaymentDate   = DateTime.Now;
            serviceInvoice.PaymentMethod = PaymentMethod.CreditCard;

            serviceInvoice.PublishEvent(new PaidInvoice(serviceInvoice));
            _serviceInvoicesRepository.Update(serviceInvoice);

            try
            {
                await _unitWork.SaveAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ServiceInvoiceExists(id))
                {
                    return(NotFound(
                               $"Error de actualizacón. No existe ninguna factura de servicio con el código {id}."));
                }

                throw;
            }

            return(_mapper.Map <ServiceInvoiceViewModel>(serviceInvoice));
        }