2020-09-18 17:20:31 +02:00
|
|
|
using System;
|
2020-09-21 12:40:45 +02:00
|
|
|
using System.Collections.Generic;
|
2020-09-18 17:20:31 +02:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
2020-09-19 16:53:45 +02:00
|
|
|
using System.Linq;
|
2020-09-18 17:20:31 +02:00
|
|
|
using System.Net.Http;
|
|
|
|
using System.Threading.Tasks;
|
2020-09-19 16:53:45 +02:00
|
|
|
using BTCPayServer.Client.Models;
|
2020-09-18 17:20:31 +02:00
|
|
|
using BTCPayServer.Data;
|
|
|
|
using BTCPayServer.Models.StoreViewModels;
|
2020-09-19 16:53:45 +02:00
|
|
|
using BTCPayServer.Services.Invoices;
|
2020-09-18 17:20:31 +02:00
|
|
|
using BTCPayServer.Services.Shopify;
|
|
|
|
using BTCPayServer.Services.Shopify.Models;
|
2020-09-21 12:40:45 +02:00
|
|
|
using BTCPayServer.Services.Stores;
|
2020-09-18 17:20:31 +02:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-09-21 12:40:45 +02:00
|
|
|
using Microsoft.AspNetCore.Cors;
|
2020-09-18 17:20:31 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-09-24 18:18:17 +02:00
|
|
|
using Newtonsoft.Json.Linq;
|
2020-09-21 12:40:45 +02:00
|
|
|
using NicolasDorier.RateLimits;
|
2020-09-18 17:20:31 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
{
|
|
|
|
public partial class StoresController
|
|
|
|
{
|
|
|
|
|
2020-09-19 16:53:45 +02:00
|
|
|
private static string _cachedShopifyJavascript;
|
2020-09-19 12:13:55 +02:00
|
|
|
|
|
|
|
private async Task<string> GetJavascript()
|
|
|
|
{
|
2020-09-21 12:40:45 +02:00
|
|
|
if (!string.IsNullOrEmpty(_cachedShopifyJavascript) && !_BTCPayEnv.IsDeveloping)
|
2020-09-18 17:20:31 +02:00
|
|
|
{
|
2020-09-19 16:53:45 +02:00
|
|
|
return _cachedShopifyJavascript;
|
2020-09-18 17:20:31 +02:00
|
|
|
}
|
|
|
|
|
2020-09-19 12:13:55 +02:00
|
|
|
string[] fileList = _BtcpayServerOptions.BundleJsCss
|
|
|
|
? new[] { "bundles/shopify-bundle.min.js"}
|
2020-09-21 12:40:45 +02:00
|
|
|
: new[] {"modal/btcpay.js", "shopify/btcpay-shopify.js"};
|
2020-09-19 12:13:55 +02:00
|
|
|
|
|
|
|
|
2020-09-18 17:20:31 +02:00
|
|
|
foreach (var file in fileList)
|
|
|
|
{
|
|
|
|
await using var stream = _webHostEnvironment.WebRootFileProvider
|
|
|
|
.GetFileInfo(file).CreateReadStream();
|
|
|
|
using var reader = new StreamReader(stream);
|
2020-09-19 16:53:45 +02:00
|
|
|
_cachedShopifyJavascript += Environment.NewLine + await reader.ReadToEndAsync();
|
2020-09-18 17:20:31 +02:00
|
|
|
}
|
|
|
|
|
2020-09-19 16:53:45 +02:00
|
|
|
return _cachedShopifyJavascript;
|
2020-09-19 12:13:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[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()}";
|
2020-09-18 17:20:31 +02:00
|
|
|
return Content(jsFile, "text/javascript");
|
|
|
|
}
|
|
|
|
|
2020-09-21 12:40:45 +02:00
|
|
|
[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, bool checkOnly = false)
|
|
|
|
{
|
|
|
|
var invoiceOrderId = $"{ShopifyOrderMarkerHostedService.SHOPIFY_ORDER_ID_PREFIX}{orderId}";
|
|
|
|
var matchedExistingInvoices = await invoiceRepository.GetInvoices(new InvoiceQuery()
|
|
|
|
{
|
2020-09-24 18:18:17 +02:00
|
|
|
OrderId = new[] {invoiceOrderId}, StoreId = new[] {storeId}
|
2020-09-21 12:40:45 +02:00
|
|
|
});
|
|
|
|
matchedExistingInvoices = matchedExistingInvoices.Where(entity =>
|
|
|
|
entity.GetInternalTags(ShopifyOrderMarkerHostedService.SHOPIFY_ORDER_ID_PREFIX)
|
|
|
|
.Any(s => s == orderId))
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
var firstInvoiceStillPending =
|
|
|
|
matchedExistingInvoices.FirstOrDefault(entity => entity.GetInvoiceState().Status == InvoiceStatus.New);
|
|
|
|
if (firstInvoiceStillPending != null)
|
|
|
|
{
|
|
|
|
return Ok(new
|
|
|
|
{
|
|
|
|
invoiceId = firstInvoiceStillPending.Id,
|
2020-09-23 00:04:48 -05:00
|
|
|
status = firstInvoiceStillPending.Status.ToString().ToLowerInvariant()
|
2020-09-21 12:40:45 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var firstInvoiceSettled =
|
|
|
|
matchedExistingInvoices.FirstOrDefault(entity => new []{InvoiceStatus.Paid, InvoiceStatus.Complete, InvoiceStatus.Confirmed }.Contains(entity.GetInvoiceState().Status) );
|
|
|
|
|
|
|
|
if (firstInvoiceSettled != null)
|
|
|
|
{
|
|
|
|
return Ok(new
|
|
|
|
{
|
|
|
|
invoiceId = firstInvoiceSettled.Id,
|
2020-09-23 00:04:48 -05:00
|
|
|
status = firstInvoiceSettled.Status.ToString().ToLowerInvariant()
|
2020-09-21 12:40:45 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (checkOnly)
|
|
|
|
{
|
|
|
|
return Ok();
|
|
|
|
}
|
2020-09-25 14:13:57 -05:00
|
|
|
var store = await _Repo.FindStore(storeId);
|
2020-09-21 12:40:45 +02:00
|
|
|
var shopify = store?.GetStoreBlob()?.Shopify;
|
|
|
|
if (shopify?.IntegratedAt.HasValue is true)
|
|
|
|
{
|
|
|
|
var client = new ShopifyApiClient(httpClientFactory, shopify.CreateShopifyApiCredentials());
|
|
|
|
var order = await client.GetOrder(orderId);
|
|
|
|
if (string.IsNullOrEmpty(order?.Id) || order.FinancialStatus != "pending")
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
var invoice = await invoiceController.CreateInvoiceCoreRaw(
|
2020-09-24 18:18:17 +02:00
|
|
|
new CreateInvoiceRequest() {Amount = order.TotalPrice, Currency = order.Currency, Metadata = new JObject {["orderId"] = invoiceOrderId} }, store,
|
2020-09-21 12:40:45 +02:00
|
|
|
Request.GetAbsoluteUri(""), new List<string>() {invoiceOrderId});
|
|
|
|
|
|
|
|
return Ok(new
|
|
|
|
{
|
|
|
|
invoiceId = invoice.Id,
|
2020-09-23 00:04:48 -05:00
|
|
|
status = invoice.Status.ToString().ToLowerInvariant()
|
2020-09-21 12:40:45 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-18 17:20:31 +02:00
|
|
|
[HttpGet]
|
|
|
|
[Route("{storeId}/integrations")]
|
|
|
|
[Route("{storeId}/integrations/shopify")]
|
|
|
|
public IActionResult Integrations()
|
|
|
|
{
|
|
|
|
var blob = CurrentStore.GetStoreBlob();
|
|
|
|
|
|
|
|
var vm = new IntegrationsViewModel {Shopify = blob.Shopify};
|
|
|
|
|
|
|
|
return View("Integrations", vm);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
[Route("{storeId}/integrations/shopify")]
|
|
|
|
public async Task<IActionResult> Integrations([FromServices] IHttpClientFactory clientFactory,
|
|
|
|
IntegrationsViewModel vm, string command = "", string exampleUrl = "")
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(exampleUrl))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-09-19 16:53:45 +02:00
|
|
|
//https://{apikey}:{password}@{hostname}/admin/api/{version}/{resource}.json
|
2020-09-18 17:20:31 +02:00
|
|
|
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)
|
|
|
|
{
|
2020-09-25 13:50:04 -05:00
|
|
|
TempData[WellKnownTempData.ErrorMessage] = "The provided Example Url was invalid.";
|
2020-09-18 17:20:31 +02:00
|
|
|
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();
|
|
|
|
}
|
2020-09-25 13:50:23 -05:00
|
|
|
catch (ShopifyApiException)
|
2020-09-18 17:20:31 +02:00
|
|
|
{
|
|
|
|
TempData[WellKnownTempData.ErrorMessage] =
|
2020-09-19 16:53:45 +02:00
|
|
|
"Shopify rejected provided credentials, please correct values and try again";
|
2020-09-18 17:20:31 +02:00
|
|
|
return View("Integrations", vm);
|
|
|
|
}
|
|
|
|
|
2020-09-19 16:53:45 +02:00
|
|
|
var scopesGranted = await apiClient.CheckScopes();
|
2020-09-28 07:58:49 +02:00
|
|
|
//TODO: check if these are actually needed
|
2020-09-26 12:51:29 -05:00
|
|
|
if (!scopesGranted.Contains("read_orders") || !scopesGranted.Contains("write_orders"))
|
2020-09-18 17:20:31 +02:00
|
|
|
{
|
2020-09-19 16:53:45 +02:00
|
|
|
TempData[WellKnownTempData.ErrorMessage] =
|
2020-09-26 12:51:29 -05:00
|
|
|
"Please grant the private app permissions for read_orders, write_orders";
|
2020-09-19 16:53:45 +02:00
|
|
|
return View("Integrations", vm);
|
2020-09-18 17:20:31 +02:00
|
|
|
}
|
2020-09-21 12:36:09 +02:00
|
|
|
|
2020-09-25 13:55:34 -05:00
|
|
|
// everything ready, proceed with saving Shopify integration credentials
|
2020-09-21 12:36:09 +02:00
|
|
|
shopify.IntegratedAt = DateTimeOffset.Now;
|
2020-09-19 16:53:45 +02:00
|
|
|
|
2020-09-18 17:20:31 +02:00
|
|
|
var blob = CurrentStore.GetStoreBlob();
|
2020-09-19 16:53:45 +02:00
|
|
|
blob.Shopify = shopify;
|
2020-09-18 17:20:31 +02:00
|
|
|
if (CurrentStore.SetStoreBlob(blob))
|
|
|
|
{
|
|
|
|
await _Repo.UpdateStore(CurrentStore);
|
|
|
|
}
|
|
|
|
|
2020-09-19 16:53:45 +02:00
|
|
|
TempData[WellKnownTempData.SuccessMessage] = "Shopify integration successfully updated";
|
2020-09-18 17:20:31 +02:00
|
|
|
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});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|