public async Task <IActionResult> OnPostCreatePaymentAsync() { SageResponseDTO response = await _service.RegisterPaymentAsync(OrderModel.Id, OrderModel.PaymentTypeSelected); if (response.Message == "Success") { StatusMessage = $"Sucesso foi criado o recibo sobre a fatura {OrderModel.SalesInvoiceNumber}"; } else { StatusMessage = $"Erro: {response.Message}, Resposta: {response.ResponseBody}"; } return(RedirectToPage(new { id = OrderModel.Id })); }
public async Task <IActionResult> OnPostRegisterInvoiceAsync() { Order order = await GetOrderAsync(OrderModel.Id); SageResponseDTO response = await _invoiceService.RegisterInvoiceAsync(SageApplicationType.SALESWEB, order); if (response.Message == "Success") { StatusMessage = $"Sucesso foi criado a fatura: {response.InvoiceNumber}"; } else { StatusMessage = $"Erro: {response.Message}, Resposta: {response.ResponseBody}"; } return(RedirectToPage(new { id = OrderModel.Id })); }
public async Task <SageResponseDTO> InvoicePayment(SageApplicationType applicationType, long id, PaymentType paymentType, decimal amount) { try { var auth = await GetAuthConfigAsync(applicationType); var sagePaymentConfig = GetSagePaymentType(paymentType); List <KeyValuePair <string, string> > body = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("sales_invoice_payment[date]", DateTime.Now.ToString("dd-MM-yyyy")), new KeyValuePair <string, string>("sales_invoice_payment[amount]", amount.ToString(CultureInfo.InvariantCulture)), new KeyValuePair <string, string>("sales_invoice_payment[bank_account_id]", sagePaymentConfig.BankId), new KeyValuePair <string, string>("sales_invoice_payment[payment_type_id]", sagePaymentConfig.TypeId) }; var builder = new UriBuilder(_baseUrl) { Path = $"accounts/v2/sales_invoices/{id}/payments" }; var uri = builder.Uri; var httpClient = CreateHttpClient(auth.AccessToken); HttpRequestMessage request = GenerateRequest(HttpMethod.Post, uri, body, httpClient, auth); var response = await httpClient.SendAsync(request); var result = await HandleResponse(response, HttpMethod.Post, uri, body, auth); var responseObj = await FormatResponse(result); if (responseObj.StatusCode == HttpStatusCode.Created) { JObject jObject = JObject.Parse(responseObj.ResponseBody); responseObj.PaymentId = (long)jObject["id"]; } //var json = new { StatusCode = response.StatusCode, ResponseBody = await response.Content.ReadAsStringAsync() }; return(responseObj); } catch (Exception ex) { var responseError = new SageResponseDTO { Message = $"Error exception: {ex.Message}", }; return(responseError); } }
private async Task <SageResponseDTO> CreateInvoice(AuthConfig auth, List <KeyValuePair <string, string> > body) { if (string.IsNullOrEmpty(auth.AccessToken)) { return(new SageResponseDTO { Message = "Erro: Os acessos à Sage ainda não estão configurados, acede a https://backoffice.damanojornal.com/Sage/" }); } try { var builder = new UriBuilder(_baseUrl) { Path = $"accounts/v2/sales_invoices" }; var uri = builder.Uri; var httpClient = CreateHttpClient(auth.AccessToken); HttpRequestMessage request = GenerateRequest(HttpMethod.Post, uri, body, httpClient, auth); var response = await httpClient.SendAsync(request); var result = await HandleResponse(response, HttpMethod.Post, uri, body, auth); var responseObj = await FormatResponse(result); if (responseObj.StatusCode == HttpStatusCode.Created) { JObject jObject = JObject.Parse(responseObj.ResponseBody); responseObj.InvoiceId = (long)jObject["id"]; responseObj.InvoiceNumber = (string)jObject["artefact_number"]; } return(responseObj); } catch (Exception ex) { _logger.LogWarning("CreateInvoice Error: {0}; StackTrace: {1}", ex.Message, ex.StackTrace); var responseError = new SageResponseDTO { Message = $"Error exception: {ex.Message}", }; return(responseError); } }
public async Task <IActionResult> OnPostRegisterPaymentAsync() { Order order = await GetOrderAsync(OrderModel.Id); SageResponseDTO response = await _invoiceService.RegisterPaymentAsync(SageApplicationType.SALESWEB, order.SalesInvoiceId.Value, order.Total(), OrderModel.PaymentTypeSelected); if (response.Message == "Success" && response.PaymentId.HasValue) { StatusMessage = $"Sucesso foi criado o recibo sobre a fatura {OrderModel.SalesInvoiceNumber}"; order.SalesPaymentId = response.PaymentId.Value; await _context.SaveChangesAsync(); } else { StatusMessage = $"Erro: {response.Message}, Resposta: {response.ResponseBody}"; } var orderViewModel = _mapper.Map <OrderViewModel>(order); orderViewModel.Items = _mapper.Map <List <OrderItemViewModel> >(order.OrderItems); OrderModel = orderViewModel; return(Page()); }
public async Task <IActionResult> OnPostRegisterInvoiceAsync() { SageResponseDTO response = await _service.RegisterInvoiceAsync(OrderModel.Id); if (response.Message == "Success") { SageResponseDTO responsePayment = await _service.RegisterPaymentAsync(OrderModel.Id, OrderModel.PaymentTypeSelected); if (responsePayment.Message == "Success") { StatusMessage = $"Sucesso foi criada a factura/recibo {response.InvoiceNumber}"; } else { StatusMessage = $"Foi criado a fatura {response.InvoiceNumber} mas ocorreu um erro ao gerar recibo: {responsePayment.Message}, Resposta: {responsePayment.ResponseBody}"; } } else { StatusMessage = $"Erro: {response.Message}, Resposta: {response.ResponseBody}"; } return(RedirectToPage(new { id = OrderModel.Id })); }