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 CreateAsync_Approved_Success() { PaymentCreateRequest request = await BuildCreateRequestAsync(true, "approved"); Payment payment = await client.CreateAsync(request); Assert.NotNull(payment); Assert.NotNull(payment.Id); Assert.True(payment.Captured); Assert.Equal(request.ExternalReference, payment.ExternalReference); }