diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fe2dac7..891180a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,7 +2,7 @@ name: Build & Test on: pull_request: - branches: [main, develop] + branches: [main, develop, release/**] jobs: build-and-test: diff --git a/BTCPayServer.Plugins.Branta.Tests/Services/BrantaServiceTests.cs b/BTCPayServer.Plugins.Branta.Tests/Services/BrantaServiceTests.cs index 66954fe..a8477d1 100644 --- a/BTCPayServer.Plugins.Branta.Tests/Services/BrantaServiceTests.cs +++ b/BTCPayServer.Plugins.Branta.Tests/Services/BrantaServiceTests.cs @@ -178,7 +178,7 @@ public async Task CreateInvoiceIfNotExists_CreatesZeroKnowledgeInvoice() } [Fact] - public async Task CreateInvoiceIfNotExists_ProperlyAddsQueryParamsForLightningOnly() + public async Task CreateInvoiceIfNotExists_DoesNotAddZeroKnowledgeParamsBolt11() { var invoice = CreateInvoice("BTC-Lightning"); var checkoutModel = CreateCheckoutModel(invoice); @@ -187,8 +187,8 @@ public async Task CreateInvoiceIfNotExists_ProperlyAddsQueryParamsForLightningOn await _brantaService.CreateInvoiceIfNotExistsAsync(checkoutModel); - Assert.Contains("?branta_id", checkoutModel.InvoiceBitcoinUrlQR); - Assert.Contains("&branta_secret", checkoutModel.InvoiceBitcoinUrlQR); + Assert.DoesNotContain("?branta_id", checkoutModel.InvoiceBitcoinUrlQR); + Assert.DoesNotContain("&branta_secret", checkoutModel.InvoiceBitcoinUrlQR); } [Fact] @@ -205,7 +205,6 @@ public async Task CreateInvoiceIfNotExists_ShouldNotSetZeroKnowledgeIfRequestUns Assert.DoesNotContain("&branta_secret", checkoutModel.InvoiceBitcoinUrlQR); } - private InvoiceData GetSavedInvoiceData() { return _invoiceServiceMock.Invocations diff --git a/BTCPayServer.Plugins.Branta/Classes/BrantaClient.cs b/BTCPayServer.Plugins.Branta/Classes/BrantaClient.cs index 42bd577..3b3a5f2 100644 --- a/BTCPayServer.Plugins.Branta/Classes/BrantaClient.cs +++ b/BTCPayServer.Plugins.Branta/Classes/BrantaClient.cs @@ -1,6 +1,8 @@ using BTCPayServer.Plugins.Branta.Models; using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; using System; +using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Text; @@ -10,11 +12,17 @@ namespace BTCPayServer.Plugins.Branta.Classes; public class BrantaClient(IHttpClientFactory httpClientFactory) { - public static readonly string PaymentVersion = "v1"; + public static readonly string PaymentVersion = "v2"; + + private JsonSerializerSettings _jsonSettings = new() + { + ContractResolver = new CamelCasePropertyNamesContractResolver() + }; public async Task PostPaymentAsync(PaymentRequest paymentRequest, BrantaSettings brantaSettings) { - var json = JsonConvert.SerializeObject(paymentRequest); + + var json = JsonConvert.SerializeObject(paymentRequest, _jsonSettings); var content = new StringContent(json, Encoding.UTF8, "application/json"); using var request = new HttpRequestMessage(HttpMethod.Post, $"{PaymentVersion}/payments") @@ -38,15 +46,18 @@ public async Task PostPaymentAsync(PaymentRequest paymentRequest, BrantaSettings public class PaymentRequest { - public Payment payment { get; set; } + public List Destinations { get; set; } + + public string Description { get; set; } + + public string Ttl { get; set; } + + public string BtcPayServerPluginVersion { get; set; } } -public class Payment +public class Destination { - public string description { get; set; } - public string payment { get; set; } - public string[] alt_payments { get; set; } - public string ttl { get; set; } - public string btcPayServerPluginVersion { get; set; } - public bool zk { get; set; } + public string Value { get; set; } + + public bool Zk { get; set; } } diff --git a/BTCPayServer.Plugins.Branta/Classes/Helper.cs b/BTCPayServer.Plugins.Branta/Classes/Helper.cs index fab96cb..1c64058 100644 --- a/BTCPayServer.Plugins.Branta/Classes/Helper.cs +++ b/BTCPayServer.Plugins.Branta/Classes/Helper.cs @@ -14,7 +14,7 @@ public static string GetVersion() public static void SetZeroKnowledgeParams(this CheckoutModel model, string payment, string secret) { - if (payment == null || secret == null) + if (payment == null || secret == null || model.PaymentMethodId != "BTC-CHAIN") { return; } diff --git a/BTCPayServer.Plugins.Branta/Services/BrantaService.cs b/BTCPayServer.Plugins.Branta/Services/BrantaService.cs index a03a9c2..7e082f1 100644 --- a/BTCPayServer.Plugins.Branta/Services/BrantaService.cs +++ b/BTCPayServer.Plugins.Branta/Services/BrantaService.cs @@ -115,8 +115,16 @@ private async Task CreateInvoiceAsync(InvoiceEntity btcPayInvoice, return 3; }) - .Select(pp => pp.Destination) - .Select(d => brantaSettings.EnableZeroKnowledge ? Helper.Encrypt(d, secret.ToString()) : d) + .Select(pp => + { + var isZk = brantaSettings.EnableZeroKnowledge && pp.PaymentMethodId == PaymentMethodId.TryParse("BTC"); + + return new Destination() + { + Value = isZk ? Helper.Encrypt(pp.Destination, secret.ToString()) : pp.Destination, + Zk = isZk + }; + }) .ToList(); var invoiceData = new InvoiceData() @@ -124,8 +132,9 @@ private async Task CreateInvoiceAsync(InvoiceEntity btcPayInvoice, DateCreated = now, InvoiceId = btcPayInvoice.Id, PaymentId = payments - .OrderBy(p => p.Length) - .First(), + .OrderBy(p => p.Value.Length) + .First() + .Value, Environment = brantaSettings.StagingEnabled ? Enums.ServerEnvironment.Staging : Enums.ServerEnvironment.Production, StoreId = btcPayInvoice.StoreId, ZeroKnowledgeSecret = secret @@ -148,15 +157,10 @@ private async Task CreateInvoiceAsync(InvoiceEntity btcPayInvoice, var paymentRequest = new Classes.PaymentRequest() { - payment = new Payment - { - description = brantaSettings.PostDescriptionEnabled ? GetDescription(btcPayInvoice) : null, - payment = payments.First(), - alt_payments = [.. payments.Skip(1)], - ttl = ttl.ToString(), - btcPayServerPluginVersion = Helper.GetVersion(), - zk = brantaSettings.EnableZeroKnowledge - } + Destinations = payments, + Description = brantaSettings.PostDescriptionEnabled ? GetDescription(btcPayInvoice) : null, + Ttl = ttl.ToString(), + BtcPayServerPluginVersion = Helper.GetVersion() }; await brantaClient.PostPaymentAsync(paymentRequest, brantaSettings); diff --git a/BTCPayServer.Plugins.Branta/Views/UIBranta/EditBranta.cshtml b/BTCPayServer.Plugins.Branta/Views/UIBranta/EditBranta.cshtml index a91dfbe..3491251 100644 --- a/BTCPayServer.Plugins.Branta/Views/UIBranta/EditBranta.cshtml +++ b/BTCPayServer.Plugins.Branta/Views/UIBranta/EditBranta.cshtml @@ -142,7 +142,7 @@ else
- +