From 9b7ca76b9941391f203001cdc581ba4bb8da3798 Mon Sep 17 00:00:00 2001 From: Andrew Camilleri Date: Tue, 28 Dec 2021 07:56:54 +0100 Subject: [PATCH] Use Outstanding amount for shopify order invoices (#3203) * Use Outstanding amount for shopify order invoices * consider shopify order may have multiple transactions * Use presentment currency not just currency fixes #2683 --- BTCPayServer/Plugins/Shopify/ApiModels/ShopifyOrder.cs | 4 ++++ .../Plugins/Shopify/OrderTransactionRegisterLogic.cs | 7 ++++--- BTCPayServer/Plugins/Shopify/ShopifyApiClient.cs | 2 +- BTCPayServer/Plugins/Shopify/ShopifyController.cs | 4 ++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/BTCPayServer/Plugins/Shopify/ApiModels/ShopifyOrder.cs b/BTCPayServer/Plugins/Shopify/ApiModels/ShopifyOrder.cs index 80c8b7ce4..65a2813e1 100644 --- a/BTCPayServer/Plugins/Shopify/ApiModels/ShopifyOrder.cs +++ b/BTCPayServer/Plugins/Shopify/ApiModels/ShopifyOrder.cs @@ -9,8 +9,12 @@ namespace BTCPayServer.Plugins.Shopify.ApiModels public string Id { get; set; } [JsonProperty("total_price")] public decimal TotalPrice { get; set; } + [JsonProperty("total_outstanding")] + public decimal TotalOutstanding { get; set; } [JsonProperty("currency")] public string Currency { get; set; } + [JsonProperty("presentment_currency")] + public string PresentmentCurrency { get; set; } [JsonProperty("financial_status")] public string FinancialStatus { get; set; } [JsonProperty("transactions")] diff --git a/BTCPayServer/Plugins/Shopify/OrderTransactionRegisterLogic.cs b/BTCPayServer/Plugins/Shopify/OrderTransactionRegisterLogic.cs index 49c496616..82ffaf9c9 100644 --- a/BTCPayServer/Plugins/Shopify/OrderTransactionRegisterLogic.cs +++ b/BTCPayServer/Plugins/Shopify/OrderTransactionRegisterLogic.cs @@ -19,9 +19,10 @@ namespace BTCPayServer.Plugins.Shopify { currency = currency.ToUpperInvariant().Trim(); var existingShopifyOrderTransactions = (await _client.TransactionsList(orderId)).transactions; - var baseParentTransaction = existingShopifyOrderTransactions.FirstOrDefault(); - if (baseParentTransaction is null || - !_keywords.Any(a => baseParentTransaction.gateway.Contains(a, StringComparison.InvariantCultureIgnoreCase))) + + //if there isn't a record for btcpay payment gateway, abort + var baseParentTransaction = existingShopifyOrderTransactions.FirstOrDefault(holder => !_keywords.Any(a => holder.gateway.Contains(a, StringComparison.InvariantCultureIgnoreCase))); + if (baseParentTransaction is null) { return null; } diff --git a/BTCPayServer/Plugins/Shopify/ShopifyApiClient.cs b/BTCPayServer/Plugins/Shopify/ShopifyApiClient.cs index 49a1f93aa..f3a524341 100644 --- a/BTCPayServer/Plugins/Shopify/ShopifyApiClient.cs +++ b/BTCPayServer/Plugins/Shopify/ShopifyApiClient.cs @@ -102,7 +102,7 @@ namespace BTCPayServer.Plugins.Shopify public async Task GetOrder(string orderId) { var req = CreateRequest(_credentials.ShopName, HttpMethod.Get, - $"orders/{orderId}.json?fields=id,total_price,currency,transactions,financial_status"); + $"orders/{orderId}.json?fields=id,total_price,total_outstanding,currency,presentment_currency,transactions,financial_status"); var strResp = await SendRequest(req); diff --git a/BTCPayServer/Plugins/Shopify/ShopifyController.cs b/BTCPayServer/Plugins/Shopify/ShopifyController.cs index 08a3d2df6..41f5e9c08 100644 --- a/BTCPayServer/Plugins/Shopify/ShopifyController.cs +++ b/BTCPayServer/Plugins/Shopify/ShopifyController.cs @@ -189,8 +189,8 @@ namespace BTCPayServer.Plugins.Shopify var invoice = await _invoiceController.CreateInvoiceCoreRaw( new CreateInvoiceRequest() { - Amount = amount < order.TotalPrice ? amount : order.TotalPrice, - Currency = order.Currency, + Amount = amount < order.TotalOutstanding ? amount : order.TotalOutstanding, + Currency = order.PresentmentCurrency, Metadata = new JObject {["orderId"] = invoiceOrderId} }, store, Request.GetAbsoluteRoot(), new List() {invoiceOrderId});