public async Task <ActionResult <ServiceInvoiceViewModel> > PutServiceInvoice(int id, ServiceInvoiceEditModel serviceInvoiceModel) { ServiceInvoice serviceInvoice = await _serviceInvoicesRepository.FindByIdAsync(id); if (serviceInvoice is null) { return(NotFound($"No existe ninguna factura de servicio con el código {id}.")); } _mapper.Map(serviceInvoiceModel, 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)); }
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)); }
private async Task FindAndReportExpiredServiceInvoices(CancellationToken cancellationToken) { IEnumerable <ServiceInvoice> serviceInvoices = await _serviceInvoicesRepository.GetPendingExpiredServiceInvoices(); foreach (ServiceInvoice serviceInvoice in serviceInvoices) { serviceInvoice.State = InvoiceState.Expired; _serviceInvoicesRepository.Update(serviceInvoice); } await _unitWork.SaveAsync(); await _invoiceHub.Clients.Group("Administrator").SendAsync("OverdueServiceBills", serviceInvoices, cancellationToken); }