mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2026-02-01 04:14:28 +01:00
Merge pull request #4554 from Kukks/gf/pmcriteria
This commit is contained in:
13
BTCPayServer.Client/Models/PaymentMethodCriteriaData.cs
Normal file
13
BTCPayServer.Client/Models/PaymentMethodCriteriaData.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using BTCPayServer.JsonConverters;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BTCPayServer.Client.Models;
|
||||
|
||||
public class PaymentMethodCriteriaData
|
||||
{
|
||||
public string PaymentMethod { get; set; }
|
||||
public string CurrencyCode { get; set; }
|
||||
[JsonConverter(typeof(NumericStringJsonConverter))]
|
||||
public decimal Amount { get; set; }
|
||||
public bool Above { get; set; }
|
||||
}
|
||||
@@ -63,6 +63,9 @@ namespace BTCPayServer.Client.Models
|
||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
public NetworkFeeMode NetworkFeeMode { get; set; } = NetworkFeeMode.Never;
|
||||
|
||||
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
|
||||
public List<PaymentMethodCriteriaData> PaymentMethodCriteria { get; set; }
|
||||
|
||||
public bool PayJoinEnabled { get; set; }
|
||||
|
||||
public InvoiceData.ReceiptOptions Receipt { get; set; }
|
||||
|
||||
@@ -1217,10 +1217,30 @@ namespace BTCPayServer.Tests
|
||||
var newStore = await client.CreateStore(new CreateStoreRequest() { Name = "A" });
|
||||
|
||||
//update store
|
||||
var updatedStore = await client.UpdateStore(newStore.Id, new UpdateStoreRequest() { Name = "B" });
|
||||
Assert.Empty(newStore.PaymentMethodCriteria);
|
||||
await client.GenerateOnChainWallet(newStore.Id, "BTC", new GenerateOnChainWalletRequest());
|
||||
var updatedStore = await client.UpdateStore(newStore.Id, new UpdateStoreRequest() { Name = "B", PaymentMethodCriteria = new List<PaymentMethodCriteriaData>()
|
||||
{
|
||||
new()
|
||||
{
|
||||
Amount = 10,
|
||||
Above = true,
|
||||
PaymentMethod = "BTC",
|
||||
CurrencyCode = "USD"
|
||||
}
|
||||
}});
|
||||
Assert.Equal("B", updatedStore.Name);
|
||||
Assert.Equal("B", (await client.GetStore(newStore.Id)).Name);
|
||||
|
||||
var s = (await client.GetStore(newStore.Id));
|
||||
Assert.Equal("B", s.Name);
|
||||
var pmc = Assert.Single(s.PaymentMethodCriteria);
|
||||
//check that pmc equals the one we set
|
||||
Assert.Equal(10, pmc.Amount);
|
||||
Assert.True(pmc.Above);
|
||||
Assert.Equal("BTC", pmc.PaymentMethod);
|
||||
Assert.Equal("USD", pmc.CurrencyCode);
|
||||
updatedStore = await client.UpdateStore(newStore.Id, new UpdateStoreRequest() { Name = "B"});
|
||||
Assert.Empty(newStore.PaymentMethodCriteria);
|
||||
|
||||
//list stores
|
||||
var stores = await client.GetStores();
|
||||
var storeIds = stores.Select(data => data.Id);
|
||||
|
||||
@@ -9,6 +9,7 @@ using BTCPayServer.Client.Models;
|
||||
using BTCPayServer.Data;
|
||||
using BTCPayServer.Payments;
|
||||
using BTCPayServer.Security;
|
||||
using BTCPayServer.Services.Rates;
|
||||
using BTCPayServer.Services.Stores;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
@@ -147,11 +148,18 @@ namespace BTCPayServer.Controllers.Greenfield
|
||||
AnyoneCanCreateInvoice = storeBlob.AnyoneCanInvoice,
|
||||
LightningDescriptionTemplate = storeBlob.LightningDescriptionTemplate,
|
||||
PaymentTolerance = storeBlob.PaymentTolerance,
|
||||
PayJoinEnabled = storeBlob.PayJoinEnabled
|
||||
PayJoinEnabled = storeBlob.PayJoinEnabled,
|
||||
PaymentMethodCriteria = storeBlob.PaymentMethodCriteria?.Where(criteria => criteria.Value is not null)?.Select(criteria => new PaymentMethodCriteriaData()
|
||||
{
|
||||
Above = criteria.Above,
|
||||
Amount = criteria.Value.Value,
|
||||
CurrencyCode = criteria.Value.Currency,
|
||||
PaymentMethod = criteria.PaymentMethod.ToStringNormalized()
|
||||
})?.ToList()?? new List<PaymentMethodCriteriaData>()
|
||||
};
|
||||
}
|
||||
|
||||
private void ToModel(StoreBaseData restModel, Data.StoreData model, PaymentMethodId defaultPaymentMethod)
|
||||
private void ToModel(StoreBaseData restModel, StoreData model, PaymentMethodId defaultPaymentMethod)
|
||||
{
|
||||
var blob = model.GetStoreBlob();
|
||||
model.StoreName = restModel.Name;
|
||||
@@ -188,6 +196,17 @@ namespace BTCPayServer.Controllers.Greenfield
|
||||
blob.LightningDescriptionTemplate = restModel.LightningDescriptionTemplate;
|
||||
blob.PaymentTolerance = restModel.PaymentTolerance;
|
||||
blob.PayJoinEnabled = restModel.PayJoinEnabled;
|
||||
blob.PaymentMethodCriteria = restModel.PaymentMethodCriteria?.Select(criteria =>
|
||||
new PaymentMethodCriteria()
|
||||
{
|
||||
Above = criteria.Above,
|
||||
Value = new CurrencyValue()
|
||||
{
|
||||
Currency = criteria.CurrencyCode,
|
||||
Value = criteria.Amount
|
||||
},
|
||||
PaymentMethod = PaymentMethodId.Parse(criteria.PaymentMethod)
|
||||
}).ToList() ?? new List<PaymentMethodCriteria>();
|
||||
blob.NormalizeToRelativeLinks(Request);
|
||||
model.SetStoreBlob(blob);
|
||||
}
|
||||
@@ -222,6 +241,30 @@ namespace BTCPayServer.Controllers.Greenfield
|
||||
if (request.PaymentTolerance < 0 && request.PaymentTolerance > 100)
|
||||
ModelState.AddModelError(nameof(request.PaymentTolerance), "PaymentTolerance can only be between 0 and 100 percent");
|
||||
|
||||
if (request.PaymentMethodCriteria?.Any() is true)
|
||||
{
|
||||
for (int index = 0; index < request.PaymentMethodCriteria.Count; index++)
|
||||
{
|
||||
PaymentMethodCriteriaData pmc = request.PaymentMethodCriteria[index];
|
||||
if (string.IsNullOrEmpty(pmc.CurrencyCode))
|
||||
{
|
||||
request.AddModelError(data => data.PaymentMethodCriteria[index].CurrencyCode, "CurrencyCode is required", this);
|
||||
}else if (CurrencyNameTable.Instance.GetCurrencyData(pmc.CurrencyCode, false) is null)
|
||||
{
|
||||
request.AddModelError(data => data.PaymentMethodCriteria[index].CurrencyCode, "CurrencyCode is invalid", this);
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(pmc.PaymentMethod) || PaymentMethodId.TryParse(pmc.PaymentMethod) is null)
|
||||
{
|
||||
request.AddModelError(data => data.PaymentMethodCriteria[index].PaymentMethod, "Payment method was invalid", this);
|
||||
}
|
||||
|
||||
if (pmc.Amount < 0)
|
||||
{
|
||||
request.AddModelError(data => data.PaymentMethodCriteria[index].Amount, "Amount must be greater than 0", this);
|
||||
}
|
||||
}
|
||||
}
|
||||
return !ModelState.IsValid ? this.CreateValidationError(ModelState) : null;
|
||||
}
|
||||
|
||||
|
||||
@@ -281,6 +281,33 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"PaymentMethodCriteriaData": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"paymentMethod": {
|
||||
"type": "string",
|
||||
"description": "The payment method (e.g., \"BTC\" or \"BTC_LightningLike\")",
|
||||
"nullable": false
|
||||
},
|
||||
"currencyCode": {
|
||||
"type": "string",
|
||||
"description": "The currency",
|
||||
"default": "USD",
|
||||
"example": "USD"
|
||||
},
|
||||
"amount": {
|
||||
"type": "string",
|
||||
"format": "decimal",
|
||||
"minimum": 0,
|
||||
"description": "The amount"
|
||||
},
|
||||
"above": {
|
||||
"type": "boolean",
|
||||
"default": false,
|
||||
"description": "If the criterion is for above or below the amount"
|
||||
}
|
||||
}
|
||||
},
|
||||
"StoreBaseData": {
|
||||
"type": "object",
|
||||
"x-abstract": true,
|
||||
|
||||
Reference in New Issue
Block a user