Abstract Store integrations (#2384)

* Decouple Shopify from Store

* Decouple shopify from store blob

* Update BTCPayServer.Tests.csproj

* Make sure shopify obj is set

* make shopify a system plugin
This commit is contained in:
Andrew Camilleri
2021-04-08 06:37:05 +02:00
committed by GitHub
parent ad1b708da5
commit 5de93f8cc4
28 changed files with 511 additions and 361 deletions

View File

@@ -134,6 +134,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Views\Stores\Integrations" />
<Folder Include="wwwroot\vendor\clipboard.js\" />
<Folder Include="wwwroot\vendor\highlightjs\" />
<Folder Include="wwwroot\vendor\summernote" />

View File

@@ -1,181 +1,20 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using BTCPayServer.Client;
using BTCPayServer.Client.Models;
using BTCPayServer.Data;
using BTCPayServer.Models;
using BTCPayServer.Models.StoreViewModels;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Shopify;
using BTCPayServer.Services.Shopify.ApiModels;
using BTCPayServer.Services.Shopify.Models;
using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NBitcoin;
using NBitcoin.DataEncoders;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NicolasDorier.RateLimits;
namespace BTCPayServer.Controllers
{
public partial class StoresController
{
private static string _cachedShopifyJavascript;
private async Task<string> GetJavascript()
{
if (!string.IsNullOrEmpty(_cachedShopifyJavascript) && !_BTCPayEnv.IsDeveloping)
{
return _cachedShopifyJavascript;
}
string[] fileList = _BtcpayServerOptions.BundleJsCss
? new[] {"bundles/shopify-bundle.min.js"}
: new[] {"modal/btcpay.js", "shopify/btcpay-shopify.js"};
foreach (var file in fileList)
{
await using var stream = _webHostEnvironment.WebRootFileProvider
.GetFileInfo(file).CreateReadStream();
using var reader = new StreamReader(stream);
_cachedShopifyJavascript += Environment.NewLine + await reader.ReadToEndAsync();
}
return _cachedShopifyJavascript;
}
[AllowAnonymous]
[HttpGet("{storeId}/integrations/shopify/shopify.js")]
public async Task<IActionResult> ShopifyJavascript(string storeId)
{
var jsFile =
$"var BTCPAYSERVER_URL = \"{Request.GetAbsoluteRoot()}\"; var STORE_ID = \"{storeId}\"; {await GetJavascript()}";
return Content(jsFile, "text/javascript");
}
[RateLimitsFilter(ZoneLimits.Shopify, Scope = RateLimitsScope.RemoteAddress)]
[AllowAnonymous]
[EnableCors(CorsPolicies.All)]
[HttpGet("{storeId}/integrations/shopify/{orderId}")]
public async Task<IActionResult> ShopifyInvoiceEndpoint(
[FromServices] InvoiceRepository invoiceRepository,
[FromServices] InvoiceController invoiceController,
[FromServices] IHttpClientFactory httpClientFactory,
string storeId, string orderId, decimal amount, bool checkOnly = false)
{
var invoiceOrderId = $"{ShopifyOrderMarkerHostedService.SHOPIFY_ORDER_ID_PREFIX}{orderId}";
var matchedExistingInvoices = await invoiceRepository.GetInvoices(new InvoiceQuery()
{
OrderId = new[] {invoiceOrderId}, StoreId = new[] {storeId}
});
matchedExistingInvoices = matchedExistingInvoices.Where(entity =>
entity.GetInternalTags(ShopifyOrderMarkerHostedService.SHOPIFY_ORDER_ID_PREFIX)
.Any(s => s == orderId))
.ToArray();
var firstInvoiceStillPending =
matchedExistingInvoices.FirstOrDefault(entity => entity.GetInvoiceState().Status == InvoiceStatusLegacy.New);
if (firstInvoiceStillPending != null)
{
return Ok(new
{
invoiceId = firstInvoiceStillPending.Id,
status = firstInvoiceStillPending.Status.ToString().ToLowerInvariant()
});
}
var firstInvoiceSettled =
matchedExistingInvoices.LastOrDefault(entity =>
new[] {InvoiceStatusLegacy.Paid, InvoiceStatusLegacy.Complete, InvoiceStatusLegacy.Confirmed}.Contains(
entity.GetInvoiceState().Status));
var store = await _Repo.FindStore(storeId);
var shopify = store?.GetStoreBlob()?.Shopify;
ShopifyApiClient client = null;
ShopifyOrder order = null;
if (shopify?.IntegratedAt.HasValue is true)
{
client = new ShopifyApiClient(httpClientFactory, shopify.CreateShopifyApiCredentials());
order = await client.GetOrder(orderId);
if (string.IsNullOrEmpty(order?.Id))
{
return NotFound();
}
}
if (firstInvoiceSettled != null)
{
//if BTCPay was shut down before the tx managed to get registered on shopify, this will fix it on the next UI load in shopify
if (client != null && order?.FinancialStatus == "pending" &&
firstInvoiceSettled.Status != InvoiceStatusLegacy.Paid)
{
await new OrderTransactionRegisterLogic(client).Process(orderId, firstInvoiceSettled.Id,
firstInvoiceSettled.Currency,
firstInvoiceSettled.Price.ToString(CultureInfo.InvariantCulture), true);
order = await client.GetOrder(orderId);
}
if (order?.FinancialStatus != "pending" && order?.FinancialStatus != "partially_paid")
{
return Ok(new
{
invoiceId = firstInvoiceSettled.Id,
status = firstInvoiceSettled.Status.ToString().ToLowerInvariant()
});
}
}
if (checkOnly)
{
return Ok();
}
if (shopify?.IntegratedAt.HasValue is true)
{
if (string.IsNullOrEmpty(order?.Id) ||
!new[] {"pending", "partially_paid"}.Contains(order.FinancialStatus))
{
return NotFound();
}
//we create the invoice at due amount provided from order page or full amount if due amount is bigger than order amount
var invoice = await invoiceController.CreateInvoiceCoreRaw(
new CreateInvoiceRequest()
{
Amount = amount < order.TotalPrice ? amount : order.TotalPrice,
Currency = order.Currency,
Metadata = new JObject {["orderId"] = invoiceOrderId}
}, store,
Request.GetAbsoluteUri(""), new List<string>() {invoiceOrderId});
return Ok(new {invoiceId = invoice.Id, status = invoice.Status.ToString().ToLowerInvariant()});
}
return NotFound();
}
[HttpGet]
[Route("{storeId}/integrations")]
[Route("{storeId}/integrations/shopify")]
[HttpGet("{storeId}/integrations")]
public IActionResult Integrations()
{
var blob = CurrentStore.GetStoreBlob();
var vm = new IntegrationsViewModel {Shopify = blob.Shopify};
return View("Integrations", vm);
{
return View("Integrations",new IntegrationsViewModel());
}
[HttpGet("{storeId}/webhooks")]
@@ -299,94 +138,5 @@ namespace BTCPayServer.Controllers
return File(delivery.GetBlob().Request, "application/json");
}
[HttpPost("{storeId}/integrations/shopify")]
public async Task<IActionResult> Integrations([FromServices] IHttpClientFactory clientFactory,
IntegrationsViewModel vm, string command = "", string exampleUrl = "")
{
if (!string.IsNullOrEmpty(exampleUrl))
{
try
{
//https://{apikey}:{password}@{hostname}/admin/api/{version}/{resource}.json
var parsedUrl = new Uri(exampleUrl);
var userInfo = parsedUrl.UserInfo.Split(":");
vm.Shopify = new ShopifySettings()
{
ApiKey = userInfo[0],
Password = userInfo[1],
ShopName = parsedUrl.Host.Replace(".myshopify.com", "",
StringComparison.InvariantCultureIgnoreCase)
};
command = "ShopifySaveCredentials";
}
catch (Exception)
{
TempData[WellKnownTempData.ErrorMessage] = "The provided Example Url was invalid.";
return View("Integrations", vm);
}
}
switch (command)
{
case "ShopifySaveCredentials":
{
var shopify = vm.Shopify;
var validCreds = shopify != null && shopify?.CredentialsPopulated() == true;
if (!validCreds)
{
TempData[WellKnownTempData.ErrorMessage] = "Please provide valid Shopify credentials";
return View("Integrations", vm);
}
var apiClient = new ShopifyApiClient(clientFactory, shopify.CreateShopifyApiCredentials());
try
{
await apiClient.OrdersCount();
}
catch (ShopifyApiException)
{
TempData[WellKnownTempData.ErrorMessage] =
"Shopify rejected provided credentials, please correct values and try again";
return View("Integrations", vm);
}
var scopesGranted = await apiClient.CheckScopes();
if (!scopesGranted.Contains("read_orders") || !scopesGranted.Contains("write_orders"))
{
TempData[WellKnownTempData.ErrorMessage] =
"Please grant the private app permissions for read_orders, write_orders";
return View("Integrations", vm);
}
// everything ready, proceed with saving Shopify integration credentials
shopify.IntegratedAt = DateTimeOffset.Now;
var blob = CurrentStore.GetStoreBlob();
blob.Shopify = shopify;
if (CurrentStore.SetStoreBlob(blob))
{
await _Repo.UpdateStore(CurrentStore);
}
TempData[WellKnownTempData.SuccessMessage] = "Shopify integration successfully updated";
break;
}
case "ShopifyClearCredentials":
{
var blob = CurrentStore.GetStoreBlob();
blob.Shopify = null;
if (CurrentStore.SetStoreBlob(blob))
{
await _Repo.UpdateStore(CurrentStore);
}
TempData[WellKnownTempData.SuccessMessage] = "Shopify integration credentials cleared";
break;
}
}
return RedirectToAction(nameof(Integrations), new {storeId = CurrentStore.Id});
}
}
}

View File

@@ -23,7 +23,6 @@ using BTCPayServer.Services;
using BTCPayServer.Services.Apps;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Rates;
using BTCPayServer.Services.Shopify;
using BTCPayServer.Services.Stores;
using BTCPayServer.Services.Wallets;
using BundlerMinifier.TagHelpers;

View File

@@ -11,7 +11,6 @@ using BTCPayServer.Payments.CoinSwitch;
using BTCPayServer.Rating;
using BTCPayServer.Services.Mails;
using BTCPayServer.Services.Rates;
using BTCPayServer.Services.Shopify.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -29,7 +28,6 @@ namespace BTCPayServer.Data
PaymentMethodCriteria = new List<PaymentMethodCriteria>();
}
public ShopifySettings Shopify { get; set; }
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public NetworkFeeMode NetworkFeeMode { get; set; }

View File

@@ -19,6 +19,7 @@ using BTCPayServer.Payments.Bitcoin;
using BTCPayServer.Payments.Lightning;
using BTCPayServer.Payments.PayJoin;
using BTCPayServer.Plugins;
using BTCPayServer.Plugins.Shopify;
using BTCPayServer.Security;
using BTCPayServer.Security.Bitpay;
using BTCPayServer.Security.GreenField;
@@ -32,7 +33,6 @@ using BTCPayServer.Services.Notifications;
using BTCPayServer.Services.Notifications.Blobs;
using BTCPayServer.Services.PaymentRequests;
using BTCPayServer.Services.Rates;
using BTCPayServer.Services.Shopify;
using BTCPayServer.Services.Stores;
using BTCPayServer.Services.Wallets;
using BTCPayServer.U2F;
@@ -359,8 +359,6 @@ namespace BTCPayServer.Hosting
services.AddSingleton<INotificationHandler, PayoutNotification.Handler>();
services.AddSingleton<IHostedService, DbMigrationsHostedService>();
services.AddShopify();
#if DEBUG
services.AddSingleton<INotificationHandler, JunkNotification.Handler>();
#endif

View File

@@ -3,13 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BTCPayServer.Services.Shopify.Models;
using static BTCPayServer.Data.StoreBlob;
namespace BTCPayServer.Models.StoreViewModels
{
public class IntegrationsViewModel
{
public ShopifySettings Shopify { get; set; }
}
}

View File

@@ -1,7 +1,7 @@
using System;
using Newtonsoft.Json;
namespace BTCPayServer.Services.Shopify.ApiModels
namespace BTCPayServer.Plugins.Shopify.ApiModels
{
public class CreateScriptResponse
{

View File

@@ -2,7 +2,7 @@ using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace BTCPayServer.Services.Shopify.ApiModels
namespace BTCPayServer.Plugins.Shopify.ApiModels
{
public class CreateWebhookResponse
{

View File

@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BTCPayServer.Services.Shopify.ApiModels.DataHolders
namespace BTCPayServer.Plugins.Shopify.ApiModels.DataHolders
{
public class TransactionDataHolder
{

View File

@@ -1,6 +1,6 @@
using Newtonsoft.Json;
namespace BTCPayServer.Services.Shopify.ApiModels
namespace BTCPayServer.Plugins.Shopify.ApiModels
{
public class CountResponse
{

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace BTCPayServer.Services.Shopify.ApiModels
namespace BTCPayServer.Plugins.Shopify.ApiModels
{
public class ShopifyOrder
{

View File

@@ -1,7 +1,7 @@
using System;
using Newtonsoft.Json;
namespace BTCPayServer.Services.Shopify.ApiModels
namespace BTCPayServer.Plugins.Shopify.ApiModels
{
public class ShopifyTransaction
{

View File

@@ -1,4 +1,4 @@
namespace BTCPayServer.Services.Shopify.ApiModels
namespace BTCPayServer.Plugins.Shopify.ApiModels
{
public class TransactionsCreateReq
{

View File

@@ -0,0 +1,9 @@
using BTCPayServer.Plugins.Shopify.ApiModels.DataHolders;
namespace BTCPayServer.Plugins.Shopify.ApiModels
{
public class TransactionsCreateResp
{
public TransactionDataHolder transaction { get; set; }
}
}

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic;
using BTCPayServer.Services.Shopify.ApiModels.DataHolders;
using BTCPayServer.Plugins.Shopify.ApiModels.DataHolders;
namespace BTCPayServer.Services.Shopify.ApiModels
namespace BTCPayServer.Plugins.Shopify.ApiModels
{
public class TransactionsListResp
{

View File

@@ -1,7 +1,8 @@
using System;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;
namespace BTCPayServer.Services.Shopify.Models
namespace BTCPayServer.Plugins.Shopify.Models
{
public class ShopifySettings
{
@@ -18,5 +19,14 @@ namespace BTCPayServer.Services.Shopify.Models
!string.IsNullOrWhiteSpace(Password);
}
public DateTimeOffset? IntegratedAt { get; set; }
[JsonIgnore]
public string ShopifyUrl
{
get
{
return ShopName?.Contains(".") is true ? $"https://{ShopName}.myshopify.com" : ShopName;
}
}
}
}

View File

@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Services.Shopify.ApiModels;
using BTCPayServer.Services.Shopify.ApiModels.DataHolders;
using BTCPayServer.Plugins.Shopify.ApiModels;
namespace BTCPayServer.Services.Shopify
namespace BTCPayServer.Plugins.Shopify
{
public class OrderTransactionRegisterLogic
{

View File

@@ -4,11 +4,11 @@ using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using BTCPayServer.Services.Shopify.ApiModels;
using BTCPayServer.Plugins.Shopify.ApiModels;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Services.Shopify
namespace BTCPayServer.Plugins.Shopify
{
public class ShopifyApiClient
{
@@ -127,11 +127,11 @@ namespace BTCPayServer.Services.Shopify
return strResp?.Contains(orderId, StringComparison.OrdinalIgnoreCase) == true;
}
}
}
public class ShopifyApiClientCredentials
{
public string ShopName { get; set; }
public string ApiKey { get; set; }
public string ApiPassword { get; set; }
}
public class ShopifyApiClientCredentials
{
public string ShopName { get; set; }
public string ApiKey { get; set; }
public string ApiPassword { get; set; }
}
}

View File

@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BTCPayServer.Services.Shopify
namespace BTCPayServer.Plugins.Shopify
{
public class ShopifyApiException : Exception
{

View File

@@ -0,0 +1,301 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Client;
using BTCPayServer.Client.Models;
using BTCPayServer.Configuration;
using BTCPayServer.Controllers;
using BTCPayServer.Data;
using BTCPayServer.Plugins.Shopify.ApiModels;
using BTCPayServer.Plugins.Shopify.Models;
using BTCPayServer.Services;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using NicolasDorier.RateLimits;
using StoreData = BTCPayServer.Data.StoreData;
namespace BTCPayServer.Plugins.Shopify
{
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)]
public class ShopifyController : Controller
{
private readonly BTCPayServerEnvironment _btcPayServerEnvironment;
private readonly IOptions<BTCPayServerOptions> _btcPayServerOptions;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly StoreRepository _storeRepository;
private readonly InvoiceRepository _invoiceRepository;
private readonly InvoiceController _invoiceController;
private readonly IHttpClientFactory _clientFactory;
public ShopifyController(BTCPayServerEnvironment btcPayServerEnvironment,
IOptions<BTCPayServerOptions> btcPayServerOptions,
IWebHostEnvironment webHostEnvironment,
StoreRepository storeRepository,
InvoiceRepository invoiceRepository,
InvoiceController invoiceController,
IHttpClientFactory clientFactory)
{
_btcPayServerEnvironment = btcPayServerEnvironment;
_btcPayServerOptions = btcPayServerOptions;
_webHostEnvironment = webHostEnvironment;
_storeRepository = storeRepository;
_invoiceRepository = invoiceRepository;
_invoiceController = invoiceController;
_clientFactory = clientFactory;
}
public StoreData CurrentStore
{
get
{
return this.HttpContext.GetStoreData();
}
}
private static string _cachedShopifyJavascript;
private async Task<string> GetJavascript()
{
if (!string.IsNullOrEmpty(_cachedShopifyJavascript) && !_btcPayServerEnvironment.IsDeveloping)
{
return _cachedShopifyJavascript;
}
string[] fileList = _btcPayServerOptions.Value.BundleJsCss
? new[] {"bundles/shopify-bundle.min.js"}
: new[] {"modal/btcpay.js", "shopify/btcpay-shopify.js"};
foreach (var file in fileList)
{
await using var stream = _webHostEnvironment.WebRootFileProvider
.GetFileInfo(file).CreateReadStream();
using var reader = new StreamReader(stream);
_cachedShopifyJavascript += Environment.NewLine + await reader.ReadToEndAsync();
}
return _cachedShopifyJavascript;
}
[AllowAnonymous]
[HttpGet("stores/{storeId}/integrations/shopify/shopify.js")]
public async Task<IActionResult> ShopifyJavascript(string storeId)
{
var jsFile =
$"var BTCPAYSERVER_URL = \"{Request.GetAbsoluteRoot()}\"; var STORE_ID = \"{storeId}\"; {await GetJavascript()}";
return Content(jsFile, "text/javascript");
}
[RateLimitsFilter(ZoneLimits.Shopify, Scope = RateLimitsScope.RemoteAddress)]
[AllowAnonymous]
[EnableCors(CorsPolicies.All)]
[HttpGet("stores/{storeId}/integrations/shopify/{orderId}")]
public async Task<IActionResult> ShopifyInvoiceEndpoint(
string storeId, string orderId, decimal amount, bool checkOnly = false)
{
var invoiceOrderId = $"{ShopifyOrderMarkerHostedService.SHOPIFY_ORDER_ID_PREFIX}{orderId}";
var matchedExistingInvoices = await _invoiceRepository.GetInvoices(new InvoiceQuery()
{
OrderId = new[] {invoiceOrderId}, StoreId = new[] {storeId}
});
matchedExistingInvoices = matchedExistingInvoices.Where(entity =>
entity.GetInternalTags(ShopifyOrderMarkerHostedService.SHOPIFY_ORDER_ID_PREFIX)
.Any(s => s == orderId))
.ToArray();
var firstInvoiceStillPending =
matchedExistingInvoices.FirstOrDefault(entity =>
entity.GetInvoiceState().Status == InvoiceStatusLegacy.New);
if (firstInvoiceStillPending != null)
{
return Ok(new
{
invoiceId = firstInvoiceStillPending.Id,
status = firstInvoiceStillPending.Status.ToString().ToLowerInvariant()
});
}
var firstInvoiceSettled =
matchedExistingInvoices.LastOrDefault(entity =>
new[] {InvoiceStatusLegacy.Paid, InvoiceStatusLegacy.Complete, InvoiceStatusLegacy.Confirmed}
.Contains(
entity.GetInvoiceState().Status));
var store = await _storeRepository.FindStore(storeId);
var shopify = store?.GetStoreBlob()?.GetShopifySettings();
ShopifyApiClient client = null;
ShopifyOrder order = null;
if (shopify?.IntegratedAt.HasValue is true)
{
client = new ShopifyApiClient(_clientFactory, shopify.CreateShopifyApiCredentials());
order = await client.GetOrder(orderId);
if (string.IsNullOrEmpty(order?.Id))
{
return NotFound();
}
}
if (firstInvoiceSettled != null)
{
//if BTCPay was shut down before the tx managed to get registered on shopify, this will fix it on the next UI load in shopify
if (client != null && order?.FinancialStatus == "pending" &&
firstInvoiceSettled.Status != InvoiceStatusLegacy.Paid)
{
await new OrderTransactionRegisterLogic(client).Process(orderId, firstInvoiceSettled.Id,
firstInvoiceSettled.Currency,
firstInvoiceSettled.Price.ToString(CultureInfo.InvariantCulture), true);
order = await client.GetOrder(orderId);
}
if (order?.FinancialStatus != "pending" && order?.FinancialStatus != "partially_paid")
{
return Ok(new
{
invoiceId = firstInvoiceSettled.Id,
status = firstInvoiceSettled.Status.ToString().ToLowerInvariant()
});
}
}
if (checkOnly)
{
return Ok();
}
if (shopify?.IntegratedAt.HasValue is true)
{
if (string.IsNullOrEmpty(order?.Id) ||
!new[] {"pending", "partially_paid"}.Contains(order.FinancialStatus))
{
return NotFound();
}
//we create the invoice at due amount provided from order page or full amount if due amount is bigger than order amount
var invoice = await _invoiceController.CreateInvoiceCoreRaw(
new CreateInvoiceRequest()
{
Amount = amount < order.TotalPrice ? amount : order.TotalPrice,
Currency = order.Currency,
Metadata = new JObject {["orderId"] = invoiceOrderId}
}, store,
Request.GetAbsoluteUri(""), new List<string>() {invoiceOrderId});
return Ok(new {invoiceId = invoice.Id, status = invoice.Status.ToString().ToLowerInvariant()});
}
return NotFound();
}
[HttpGet]
[Route("stores/{storeId}/integrations/shopify")]
public IActionResult EditShopifyIntegration()
{
var blob = CurrentStore.GetStoreBlob();
return View( blob.GetShopifySettings());
}
[HttpPost("stores/{storeId}/integrations/shopify")]
public async Task<IActionResult> EditShopifyIntegration(string storeId,
ShopifySettings vm, string command = "", string exampleUrl = "")
{
if (!string.IsNullOrEmpty(exampleUrl))
{
try
{
//https://{apikey}:{password}@{hostname}/admin/api/{version}/{resource}.json
var parsedUrl = new Uri(exampleUrl);
var userInfo = parsedUrl.UserInfo.Split(":");
vm = new ShopifySettings()
{
ApiKey = userInfo[0],
Password = userInfo[1],
ShopName = parsedUrl.Host.Replace(".myshopify.com", "",
StringComparison.InvariantCultureIgnoreCase)
};
command = "ShopifySaveCredentials";
}
catch (Exception)
{
TempData[WellKnownTempData.ErrorMessage] = "The provided Example Url was invalid.";
return View( vm);
}
}
switch (command)
{
case "ShopifySaveCredentials":
{
var shopify = vm;
var validCreds = shopify != null && shopify?.CredentialsPopulated() == true;
if (!validCreds)
{
TempData[WellKnownTempData.ErrorMessage] = "Please provide valid Shopify credentials";
return View(vm);
}
var apiClient = new ShopifyApiClient(_clientFactory, shopify.CreateShopifyApiCredentials());
try
{
await apiClient.OrdersCount();
}
catch (ShopifyApiException)
{
TempData[WellKnownTempData.ErrorMessage] =
"Shopify rejected provided credentials, please correct values and try again";
return View( vm);
}
var scopesGranted = await apiClient.CheckScopes();
if (!scopesGranted.Contains("read_orders") || !scopesGranted.Contains("write_orders"))
{
TempData[WellKnownTempData.ErrorMessage] =
"Please grant the private app permissions for read_orders, write_orders";
return View(vm);
}
// everything ready, proceed with saving Shopify integration credentials
shopify.IntegratedAt = DateTimeOffset.Now;
var blob = CurrentStore.GetStoreBlob();
blob.SetShopifySettings(shopify);
if (CurrentStore.SetStoreBlob(blob))
{
await _storeRepository.UpdateStore(CurrentStore);
}
TempData[WellKnownTempData.SuccessMessage] = "Shopify integration successfully updated";
break;
}
case "ShopifyClearCredentials":
{
var blob = CurrentStore.GetStoreBlob();
blob.SetShopifySettings(null);
if (CurrentStore.SetStoreBlob(blob))
{
await _storeRepository.UpdateStore(CurrentStore);
}
TempData[WellKnownTempData.SuccessMessage] = "Shopify integration credentials cleared";
break;
}
}
return RedirectToAction(nameof(EditShopifyIntegration), new {storeId = CurrentStore.Id});
}
}
}

View File

@@ -0,0 +1,47 @@
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Abstractions.Services;
using BTCPayServer.Data;
using BTCPayServer.Plugins.Shopify.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NBitcoin;
using NBXplorer;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Plugins.Shopify
{
public static class ShopifyExtensions
{
public const string StoreBlobKey = "shopify";
public static ShopifyApiClientCredentials CreateShopifyApiCredentials(this ShopifySettings shopify)
{
return new ShopifyApiClientCredentials
{
ShopName = shopify.ShopName,
ApiKey = shopify.ApiKey,
ApiPassword = shopify.Password
};
}
public static ShopifySettings GetShopifySettings(this StoreBlob storeBlob)
{
if (storeBlob.AdditionalData.TryGetValue(StoreBlobKey, out var rawS) && rawS is JObject rawObj)
{
return new Serializer(null).ToObject<ShopifySettings>(rawObj);
}
return null;
}
public static void SetShopifySettings(this StoreBlob storeBlob, ShopifySettings settings)
{
if (settings is null)
{
storeBlob.AdditionalData.Remove(StoreBlobKey);
}
else
{
storeBlob.AdditionalData.AddOrReplace(StoreBlobKey, new Serializer(null).ToString(settings));
}
}
}
}

View File

@@ -9,12 +9,12 @@ using BTCPayServer.Data;
using BTCPayServer.Events;
using BTCPayServer.HostedServices;
using BTCPayServer.Logging;
using BTCPayServer.Plugins.Shopify.Models;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Shopify.Models;
using BTCPayServer.Services.Stores;
using Microsoft.Extensions.Logging;
namespace BTCPayServer.Services.Shopify
namespace BTCPayServer.Plugins.Shopify
{
public class ShopifyOrderMarkerHostedService : EventHostedServiceBase
{
@@ -74,9 +74,10 @@ namespace BTCPayServer.Services.Shopify
// ensure that store in question has shopify integration turned on
// and that invoice's orderId has shopify specific prefix
if (storeBlob.Shopify?.IntegratedAt.HasValue == true)
var settings = storeBlob.GetShopifySettings();
if (settings?.IntegratedAt.HasValue == true)
{
var client = CreateShopifyApiClient(storeBlob.Shopify);
var client = CreateShopifyApiClient(settings);
if (!await client.OrderExists(shopifyOrderId))
{
// don't register transactions for orders that don't exist on shopify

View File

@@ -0,0 +1,23 @@
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Abstractions.Models;
using BTCPayServer.Abstractions.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace BTCPayServer.Plugins.Shopify
{
public class ShopifyPlugin : BaseBTCPayServerPlugin
{
public override string Identifier => "BTCPayServer.Plugins.Shopify";
public override string Name => "Shopify";
public override string Description => "Allows you to integrate BTCPay Server as a payment option in Shopify.";
public override void Execute(IServiceCollection applicationBuilder)
{
applicationBuilder.AddSingleton<IHostedService, ShopifyOrderMarkerHostedService>();
applicationBuilder.AddSingleton<IUIExtension>(new UIExtension("Shopify/StoreIntegrationShopifyOption",
"store-integrations-list"));
base.Execute(applicationBuilder);
}
}
}

View File

@@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BTCPayServer.Services.Shopify.ApiModels.DataHolders;
namespace BTCPayServer.Services.Shopify.ApiModels
{
public class TransactionsCreateResp
{
public TransactionDataHolder transaction { get; set; }
}
}

View File

@@ -1,24 +0,0 @@
using BTCPayServer.Services.Shopify.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace BTCPayServer.Services.Shopify
{
public static class ShopifyExtensions
{
public static ShopifyApiClientCredentials CreateShopifyApiCredentials(this ShopifySettings shopify)
{
return new ShopifyApiClientCredentials
{
ShopName = shopify.ShopName,
ApiKey = shopify.ApiKey,
ApiPassword = shopify.Password
};
}
public static void AddShopify(this IServiceCollection services)
{
services.AddSingleton<IHostedService, ShopifyOrderMarkerHostedService>();
}
}
}

View File

@@ -0,0 +1,46 @@
@using BTCPayServer.Plugins.Shopify
@{
var shopify = Context.GetStoreData().GetStoreBlob().GetShopifySettings();
var shopifyCredsSet = shopify?.IntegratedAt.HasValue is true;
var shopifyUrl = shopify?.ShopifyUrl;
}
<li class="list-group-item bg-tile ">
<div class="d-flex align-items-center">
<span class="d-flex flex-wrap flex-fill flex-column flex-sm-row">
<strong class="mr-3">
Shopify
<a href="https://docs.btcpayserver.org/Shopify" target="_blank">
<span class="fa fa-question-circle-o" title="More information..."></span>
</a>
</strong>
<span title="" class="d-flex mr-3" >
<span class="text-nowrap text-secondary">@shopifyUrl</span>
</span>
</span>
<span class="d-flex align-items-center fw-semibold">
@if (shopifyCredsSet)
{
<span class="d-flex align-items-center text-success">
<span class="mr-2 btcpay-status btcpay-status--enabled"></span>
Enabled
</span>
<span class="text-light ml-3 mr-2">|</span>
<a lass="btn btn-link px-1 py-1 fw-semibold" asp-controller="Shopify" asp-action="EditShopifyIntegration" asp-route-storeId="@Context.GetRouteValue("storeId")">
Modify
</a>
}
else
{
<span class="d-flex align-items-center text-danger">
<span class="mr-2 btcpay-status btcpay-status--disabled"></span>
Disabled
</span>
<a class="btn btn-primary btn-sm ml-4 px-3 py-1 fw-semibold" asp-controller="Shopify" asp-action="EditShopifyIntegration" asp-route-storeId="@Context.GetRouteValue("storeId")">
Setup
</a>
}
</span>
</div>
</li>

View File

@@ -1,8 +1,15 @@
@model IntegrationsViewModel
@using BTCPayServer.Views.Stores
@model BTCPayServer.Plugins.Shopify.Models.ShopifySettings
@{
var shopify = Model.Shopify;
var shopifyCredsSet = shopify?.IntegratedAt.HasValue is true;
var shopifyUrl = shopify is null? null: !shopify?.ShopName?.Contains(".") is true ? $"https://{shopify.ShopName}.myshopify.com" : shopify.ShopName;
Layout = "../Shared/_NavLayout.cshtml";
ViewData["NavPartialName"] = "../Stores/_Nav";
ViewData.SetActivePageAndTitle(StoreNavPages.Integrations, "Integrations");
var shopifyCredsSet = Model?.IntegratedAt.HasValue is true;
var shopifyUrl = Model?.ShopifyUrl;
}
<form method="post" id="shopifyForm">
<h4 class="mb-3">
@@ -25,43 +32,43 @@
else
{
<div class="form-group">
<label asp-for="Shopify.ShopName"></label>
<label asp-for="ShopName"></label>
<div class="input-group">
@if (!Model.Shopify?.ShopName?.Contains(".") is true)
@if (!Model?.ShopName?.Contains(".") is true)
{
<div class="input-group-prepend">
<span class="input-group-text">https://</span>
</div>
}
<input asp-for="Shopify.ShopName" class="form-control" readonly="@shopifyCredsSet" />
<input asp-for="ShopName" class="form-control" readonly="@shopifyCredsSet" />
@if (!Model.Shopify?.ShopName?.Contains(".") is true)
@if (!Model?.ShopName?.Contains(".") is true)
{
<div class="input-group-append">
<span class="input-group-text">.myshopify.com</span>
</div>
}
</div>
<span asp-validation-for="Shopify.ShopName" class="text-danger"></span>
<span asp-validation-for="ShopName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Shopify.ApiKey"></label>
<input asp-for="Shopify.ApiKey" class="form-control" readonly="@shopifyCredsSet" />
<span asp-validation-for="Shopify.ApiKey" class="text-danger"></span>
<label asp-for="ApiKey"></label>
<input asp-for="ApiKey" class="form-control" readonly="@shopifyCredsSet" />
<span asp-validation-for="ApiKey" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Shopify.Password"></label>
<input asp-for="Shopify.Password" class="form-control" type="password" value="@Model.Shopify?.Password" readonly="@shopifyCredsSet" />
<span asp-validation-for="Shopify.Password" class="text-danger"></span>
<label asp-for="Password"></label>
<input asp-for="Password" class="form-control" type="password" value="@Model?.Password" readonly="@shopifyCredsSet" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="alert alert-warning">
<p>
In Shopify please paste following script at <a href="@shopifyUrl/admin/settings/checkout#PolarisTextField1" target="_blank" class="font-weight-bold"> Settings &gt; Checkout &gt; Order Processing &gt; Additional Scripts</a>
</p>
<kbd style="display: block; word-break: break-all;">
@($"<script src='{Url.Action("ShopifyJavascript", "Stores", new {storeId = Context.GetRouteValue("storeId")}, Context.Request.Scheme)}'></script>")
@($"<script src='{Url.Action("ShopifyJavascript", "Shopify", new {storeId = Context.GetRouteValue("storeId")}, Context.Request.Scheme)}'></script>")
</kbd>
</div>
<p class="alert alert-warning">
@@ -69,8 +76,8 @@
</p>
<p class="alert alert-success">
Orders on <b>@shopify.ShopName</b>.myshopify.com will be marked as paid on successful invoice payment.
Started: @shopify.IntegratedAt.Value.ToBrowserDate()
Orders on <b>@Model.ShopName</b>.myshopify.com will be marked as paid on successful invoice payment.
Started: @Model.IntegratedAt.Value.ToBrowserDate()
</p>
<button name="command" type="submit" class="btn btn-danger" value="ShopifyClearCredentials">

View File

@@ -4,7 +4,7 @@
ViewData.SetActivePageAndTitle(StoreNavPages.Integrations, "Integrations");
}
<partial name="_StatusMessage" />
<partial name="_StatusMessage"/>
@if (!ViewContext.ModelState.IsValid)
{
@@ -15,20 +15,30 @@
</div>
}
<div class="row">
<div class="col-md-8">
<partial name="Integrations/Shopify"/>
<ul class="list-group mb-3">
<vc:ui-extension-point location="store-integrations-list"></vc:ui-extension-point>
</ul>
<div class="row">
<div class="col-12">
<h4 class="mb-3 mt-5">
Other Integrations
</h4>
<p>
Take a look at documentation for the list of other integrations we support and the directions on how to enable them:
<ul>
<li><a href="https://docs.btcpayserver.org/WooCommerce/" target="_blank">WooCommerce</a></li>
<li><a href="https://docs.btcpayserver.org/Drupal/" target="_blank">Drupal</a></li>
<li><a href="https://docs.btcpayserver.org/Magento/" target="_blank">Magento</a></li>
<li><a href="https://docs.btcpayserver.org/PrestaShop/" target="_blank">PrestaShop</a></li>
<li>
<a href="https://docs.btcpayserver.org/WooCommerce/" target="_blank">WooCommerce</a>
</li>
<li>
<a href="https://docs.btcpayserver.org/Drupal/" target="_blank">Drupal</a>
</li>
<li>
<a href="https://docs.btcpayserver.org/Magento/" target="_blank">Magento</a>
</li>
<li>
<a href="https://docs.btcpayserver.org/PrestaShop/" target="_blank">PrestaShop</a>
</li>
</ul>
</p>
</div>