public async Task <string> GetDataAsync(SageApplicationType applicationType, string url) { try { var auth = await GetAuthConfigAsync(applicationType); var builder = new UriBuilder(_baseUrl) { Path = url.Substring(0, url.IndexOf('?') != -1 ? url.IndexOf('?') : url.Length), Query = url.IndexOf('?') != -1 ? url.Substring(url.IndexOf('?') + 1) : "" }; var uri = builder.Uri; var httpClient = CreateHttpClient(auth.AccessToken); HttpRequestMessage request = GenerateRequest(HttpMethod.Get, uri, null, httpClient, auth); var response = await httpClient.SendAsync(request); var result = await HandleResponse(response, HttpMethod.Get, uri, null, auth); //var json = new { StatusCode = response.StatusCode, ResponseBody = await response.Content.ReadAsStringAsync() }; return(JsonConvert.SerializeObject(await FormatResponse(result), Formatting.Indented)); } catch (Exception ex) { var json = new { ExceptionMessage = ex.Message, StackTrace = ex.StackTrace, InnerExceptionMessage = ex.InnerException?.Message, InnerExceptionStackTrace = ex.InnerException?.StackTrace }; return(JsonConvert.SerializeObject(json, Formatting.Indented)); } }
public async Task <SageResponseDTO> CreateInvoiceWithTaxNumber(SageApplicationType applicationType, List <OrderItem> orderItems, string customerName, string taxNumber, string address, string address2, string postalCode, string city, string country, int referenceId, decimal carriageAmount) { var auth = await GetAuthConfigAsync(applicationType); if (orderItems == null || orderItems.Count == 0) { return new SageResponseDTO { Message = "Error Input Data", ResponseBody = "Error: No items" } } ; List <KeyValuePair <string, string> > body = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("sales_invoice[customer_company]", customerName), new KeyValuePair <string, string>("sales_invoice[customer_tax_number]", taxNumber), new KeyValuePair <string, string>("sales_invoice[date]", DateTime.Now.ToString("dd-MM-yyyy")), new KeyValuePair <string, string>("sales_invoice[due_date]", DateTime.Now.AddMonths(1).ToString("dd-MM-yyyy")), new KeyValuePair <string, string>("sales_invoice[main_address_street_1]", address), new KeyValuePair <string, string>("sales_invoice[main_address_street_2]", address2), new KeyValuePair <string, string>("sales_invoice[main_address_postcode]", postalCode), new KeyValuePair <string, string>("sales_invoice[main_address_locality]", city), new KeyValuePair <string, string>("sales_invoice[main_address_country_id]", country), //new KeyValuePair<string,string>("sales_invoice[carriage_tax_rate_id]", "4"), new KeyValuePair <string, string>("sales_invoice[vat_exemption_reason_id]", "10"), new KeyValuePair <string, string>("sales_invoice[reference]", referenceId.ToString()) }; var idx = AddLinesToBody(orderItems, body); SetCarriageAmount(idx, carriageAmount, body); return(await CreateInvoice(auth, body)); }
public async Task <string> GetAccountData(SageApplicationType applicationType) { try { var auth = await GetAuthConfigAsync(applicationType); //var uri = new Uri(@"https://api.sageone.com/core/v2/business"); var builder = new UriBuilder(_baseUrl) { Path = $"/core/v2/business" }; var uri = builder.Uri; var httpClient = CreateHttpClient(auth.AccessToken); HttpRequestMessage request = GenerateRequest(HttpMethod.Get, uri, null, httpClient, auth); var response = await httpClient.SendAsync(request); var result = await HandleResponse(response, HttpMethod.Get, uri, null, auth); //var json = new { StatusCode = response.StatusCode, ResponseBody = await response.Content.ReadAsStringAsync() }; return(JsonConvert.SerializeObject(await FormatResponse(result), Formatting.Indented)); } catch (Exception ex) { var json = new { ExceptionMessage = ex.Message, StackTrace = ex.StackTrace, InnerExceptionMessage = ex.InnerException?.Message, InnerExceptionStackTrace = ex.InnerException?.StackTrace }; return(JsonConvert.SerializeObject(json, Formatting.Indented)); } }
public async Task <SageResponseDTO> CreateAnonymousInvoice(SageApplicationType applicationType, List <OrderItem> orderItems, int referenceId, decimal carriageAmount) { var auth = await GetAuthConfigAsync(applicationType); if (orderItems == null || orderItems.Count == 0) { return new SageResponseDTO { Message = "Error Input Data", ResponseBody = "Error: No items" } } ; List <KeyValuePair <string, string> > body = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("sales_invoice[date]", DateTime.Now.ToString("dd-MM-yyyy")), new KeyValuePair <string, string>("sales_invoice[due_date]", DateTime.Now.AddMonths(1).ToString("dd-MM-yyyy")), //new KeyValuePair<string,string>("sales_invoice[carriage_tax_rate_id]", "4"), new KeyValuePair <string, string>("sales_invoice[vat_exemption_reason_id]", "10"), new KeyValuePair <string, string>("sales_invoice[reference]", referenceId.ToString()) }; var idx = AddLinesToBody(orderItems, body); SetCarriageAmount(idx, carriageAmount, body); return(await CreateInvoice(auth, body)); }
protected async Task <AuthConfig> GetAuthConfigAsync(SageApplicationType type) { return(await _cache.GetOrCreateAsync($"CONFIG_TYPE_{type}", async entry => { entry.SlidingExpiration = TimeSpan.FromSeconds(30); return await _authRepository.GetAuthConfigAsync(type); })); }
public Task <SageResponseDTO> RegisterInvoiceAsync(SageApplicationType applicationType, Order order) { return(Task.FromResult(new SageResponseDTO { InvoiceId = 99999, InvoiceNumber = "FT/99999", Message = "Success" })); }
public Task <SageResponseDTO> RegisterPaymentAsync(SageApplicationType applicationType, long salesInvoiceId, decimal total, PaymentType paymentType) { return(Task.FromResult(new SageResponseDTO { InvoiceId = 99999, InvoiceNumber = "FT/99999", Message = "Success", PaymentId = 99999 })); }
public async Task UpdateAuthConfigAsync(SageApplicationType applicationId, string accessToken, string refreshToken) { var appConfig = await _context.AuthConfigs.FindAsync(applicationId); if (appConfig != null) { appConfig.AccessToken = accessToken; appConfig.RefreshToken = refreshToken; await _context.SaveChangesAsync(); } }
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); } }
public async Task <byte[]> GetPDFReceipt(SageApplicationType applicationType, long invoiceId, long paymentId) { var auth = await GetAuthConfigAsync(applicationType); var builder = new UriBuilder(_baseUrl) { Path = $"/accounts/v2/sales_invoices/{invoiceId}/payments/{paymentId}" }; var uri = builder.Uri; var httpClient = CreateHttpClient(auth.AccessToken); HttpRequestMessage request = GenerateRequest(HttpMethod.Get, uri, null, httpClient, auth, true); var response = await httpClient.SendAsync(request); var result = await HandleResponse(response, HttpMethod.Get, uri, null, auth, true); return(await result.Content.ReadAsByteArrayAsync()); }
public async Task <SageResponseDTO> RegisterInvoiceAsync(SageApplicationType applicationType, Order order) { List <OrderItem> items = new List <OrderItem>(); foreach (var item in order.OrderItems) { items.Add(new OrderItem(item.ItemOrdered, item.UnitPrice, item.Units, item.CatalogAttribute1, item.CatalogAttribute2, item.CatalogAttribute3, item.CustomizeName, item.CustomizeSide, null)); } SageResponseDTO response; if (order. TaxNumber.HasValue) { var maxStreet1Length = order.BillingToAddress.Street.Length; if (maxStreet1Length >= 50) { maxStreet1Length = 50; } response = await _sageService.CreateInvoiceWithTaxNumber(applicationType, items, order.BillingToAddress.Name, order.TaxNumber.Value.ToString(), order.BillingToAddress.Street.Substring(0, maxStreet1Length), order.BillingToAddress.Street.Length > 50?order.BillingToAddress.Street.Substring(50) : string.Empty, order.BillingToAddress.PostalCode, order.BillingToAddress.City, order.BillingToAddress.Country == "Portugal"? "175" : order.BillingToAddress.Country, order.Id, order.ShippingCost); } else { response = await _sageService.CreateAnonymousInvoice(applicationType, items, order.Id, order.ShippingCost); } return(response); }
public async Task <AuthConfig> GetAuthConfigAsync(SageApplicationType applicationId) { return(await _context.AuthConfigs.FindAsync(applicationId)); }
public async Task <SageResponseDTO> RegisterPaymentAsync(SageApplicationType applicationType, long salesInvoiceId, decimal total, PaymentType paymentType) { return(await _sageService.InvoicePayment(applicationType, salesInvoiceId, paymentType, total)); }
public async Task <(string AccessToken, string RefreshToken)> GetAccessTokenAsync(SageApplicationType applicationType, string code) { var auth = await GetAuthConfigAsync(applicationType); var httpClient = CreateHttpClient(); AddDefaultHeaders(httpClient); var data = SageOneUtils.ConvertPostParams(SageOneUtils.GetAccessTokenPostData(code, auth.ClientId, auth.ClientSecret, auth.CallbackURL)); var content = new StringContent(data); var uri = new Uri(_settings.AccessTokenURL); var response = await httpClient.PostAsync(uri, content); if (response.IsSuccessStatusCode) { var responseContent = await response.Content.ReadAsStringAsync(); JObject jObject = JObject.Parse(responseContent); string access_token = (string)jObject["access_token"]; string refresh_token = (string)jObject["refresh_token"]; return(access_token, refresh_token); } throw new SageException(response?.StatusCode.ToString(), await response?.Content?.ReadAsStringAsync()); }
public Task <byte[]> GetPDFReceiptAsync(SageApplicationType applicationType, long invoiceId, long paymentId) { byte[] bytes = null; return(Task.FromResult(bytes)); }
public Task <(string AccessToken, string RefreshToken)> GenerateNewAccessTokenAsync(SageApplicationType applicationType, string code) { return(Task.FromResult((Guid.NewGuid().ToString(), Guid.NewGuid().ToString()))); }
public async Task <byte[]> GetInvoicePDFAsync(SageApplicationType application, long invoiceId) { return(await _invoiceService.GetPDFInvoiceAsync(application, invoiceId)); }
public async Task <byte[]> GetPDFReceiptAsync(SageApplicationType applicationType, long invoiceId, long paymentId) { return(await _sageService.GetPDFReceipt(applicationType, invoiceId, paymentId)); }
public async Task <(string AccessToken, string RefreshToken)> GetAccessTokenByRefreshAsync(SageApplicationType applicationType) { var auth = await GetAuthConfigAsync(applicationType); var httpClient = CreateHttpClient(); AddDefaultHeaders(httpClient); var data = SageOneUtils.ConvertPostParams(SageOneUtils.GetRefreshTokenPostData(auth.ClientId, auth.ClientSecret, auth.RefreshToken)); var content = new StringContent(data); var uri = new Uri(_settings.AccessTokenURL); var response = await httpClient.PostAsync(uri, content); //var responseObj = await HandleResponse(response); var responseContent = await response.Content.ReadAsStringAsync(); JObject jObject = JObject.Parse(responseContent); string access_token = (string)jObject["access_token"]; string refresh_token = (string)jObject["refresh_token"]; return(access_token, refresh_token); }
public async Task <(string AccessToken, string RefreshToken)> GenerateNewAccessTokenAsync(SageApplicationType applicationType, string code) { return(await _sageService.GetAccessTokenAsync(applicationType, code)); }