From cf99f0fca0cf6a12ce06e7dfef4d6f09065985a2 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Sun, 13 Sep 2020 17:07:55 -0500 Subject: [PATCH] Importing ShopifyApiClient for interactions with Shopify Api --- .../Services/Shopify/ShopifyApiClient.cs | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 BTCPayServer/Services/Shopify/ShopifyApiClient.cs diff --git a/BTCPayServer/Services/Shopify/ShopifyApiClient.cs b/BTCPayServer/Services/Shopify/ShopifyApiClient.cs new file mode 100644 index 000000000..dad345b7c --- /dev/null +++ b/BTCPayServer/Services/Shopify/ShopifyApiClient.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using DBriize.Utils; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace BTCPayServer.Services.Shopify +{ + public class ShopifyApiClient + { + private readonly HttpClient _httpClient; + private readonly ILogger _logger; + private readonly ShopifyApiClientCredentials _creds; + + public ShopifyApiClient(IHttpClientFactory httpClientFactory, ILogger logger, ShopifyApiClientCredentials creds) + { + if (httpClientFactory != null) + { + _httpClient = httpClientFactory.CreateClient(nameof(ShopifyApiClient)); + } + else // tests don't provide IHttpClientFactory + { + _httpClient = new HttpClient(); + } + _logger = logger; + _creds = creds; + + var bearer = $"{creds.ApiKey}:{creds.ApiPassword}"; + bearer = Encoding.UTF8.GetBytes(bearer).ToBase64String(); + + _httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + bearer); + } + + public async Task TransactionsList(string orderId) + { + var req = createRequest(_creds.ShopName, HttpMethod.Get, $"orders/{orderId}/transactions.json"); + + var strResp = await sendRequest(req); + + dynamic parsed = JObject.Parse(strResp); + + return parsed; + } + + public async Task TransactionCreate(string orderId, TransactionCreate txnCreate) + { + var postJson = JsonConvert.SerializeObject(txnCreate); + + var req = createRequest(_creds.ShopName, HttpMethod.Post, $"orders/{orderId}/transactions.json"); + req.Content = new StringContent(postJson, Encoding.UTF8, "application/json"); + + var strResp = await sendRequest(req); + return JObject.Parse(strResp); + } + + private HttpRequestMessage createRequest(string shopNameInUrl, HttpMethod method, string action) + { + var url = $"https://{shopNameInUrl}.myshopify.com/admin/api/2020-07/" + action; + + var req = new HttpRequestMessage(method, url); + + return req; + } + + private async Task sendRequest(HttpRequestMessage req) + { + using var resp = await _httpClient.SendAsync(req); + + var strResp = await resp.Content.ReadAsStringAsync(); + return strResp; + } + } + + public class ShopifyApiClientCredentials + { + public string ShopName { get; set; } + public string ApiKey { get; set; } + public string ApiPassword { get; set; } + public string SharedSecret { get; set; } + } + + public class TransactionCreate + { + public DataHolder transaction { get; set; } + + public class DataHolder + { + public string currency { get; set; } + public string amount { get; set; } + public string kind { get; set; } + public string parent_id { get; set; } + public string gateway { get; set; } + public string source { get; set; } + } + } +}