Fix: Bitpay's API rate route wasn't backward for some queries (#5671)

This commit is contained in:
Nicolas Dorier
2024-01-18 14:08:07 +09:00
committed by GitHub
parent bd196ad963
commit cd8ef0c1ff
2 changed files with 21 additions and 9 deletions

View File

@@ -1147,6 +1147,14 @@ namespace BTCPayServer.Tests
bitpay = new Bitpay(k, tester.PayTester.ServerUri); bitpay = new Bitpay(k, tester.PayTester.ServerUri);
Assert.True(bitpay.TestAccess(Facade.Merchant)); Assert.True(bitpay.TestAccess(Facade.Merchant));
Assert.True(bitpay.TestAccess(Facade.PointOfSale)); Assert.True(bitpay.TestAccess(Facade.PointOfSale));
HttpClient client = new HttpClient();
var token = (await bitpay.GetAccessTokenAsync(Facade.Merchant)).Value;
var getRates = tester.PayTester.ServerUri.AbsoluteUri + $"rates/?cryptoCode=BTC&token={token}";
var req = new HttpRequestMessage(HttpMethod.Get, getRates);
req.Headers.Add("x-signature", NBitpayClient.Extensions.BitIdExtensions.GetBitIDSignature(k, getRates, null));
req.Headers.Add("x-identity", k.PubKey.ToHex());
var resp = await client.SendAsync(req);
resp.EnsureSuccessStatusCode();
// Can generate API Key // Can generate API Key
var repo = tester.PayTester.GetService<TokenRepository>(); var repo = tester.PayTester.GetService<TokenRepository>();
@@ -1167,7 +1175,6 @@ namespace BTCPayServer.Tests
apiKey = apiKey2; apiKey = apiKey2;
// Can create an invoice with this new API Key // Can create an invoice with this new API Key
HttpClient client = new HttpClient();
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post,
tester.PayTester.ServerUri.AbsoluteUri + "invoices"); tester.PayTester.ServerUri.AbsoluteUri + "invoices");
message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic",

View File

@@ -48,7 +48,7 @@ namespace BTCPayServer.Controllers
[Route("rates/{baseCurrency}")] [Route("rates/{baseCurrency}")]
[HttpGet] [HttpGet]
[BitpayAPIConstraint] [BitpayAPIConstraint]
public async Task<IActionResult> GetBaseCurrencyRates(string baseCurrency, CancellationToken cancellationToken) public async Task<IActionResult> GetBaseCurrencyRates(string baseCurrency, string cryptoCode = null, CancellationToken cancellationToken = default)
{ {
var supportedMethods = CurrentStore.GetSupportedPaymentMethods(_networkProvider); var supportedMethods = CurrentStore.GetSupportedPaymentMethods(_networkProvider);
@@ -57,16 +57,16 @@ namespace BTCPayServer.Controllers
var currencypairs = BuildCurrencyPairs(currencyCodes, baseCurrency); var currencypairs = BuildCurrencyPairs(currencyCodes, baseCurrency);
var result = await GetRates2(currencypairs, null, cancellationToken); var result = await GetRates2(currencypairs, null, cryptoCode, cancellationToken);
var rates = (result as JsonResult)?.Value as Rate[]; var rates = (result as JsonResult)?.Value as Rate[];
return rates == null ? result : Json(new DataWrapper<Rate[]>(rates)); return rates == null ? result : Json(new DataWrapper<Rate[]>(rates));
} }
[HttpGet("rates/{baseCurrency}/{currency}")] [HttpGet("rates/{baseCurrency}/{currency}")]
[BitpayAPIConstraint] [BitpayAPIConstraint]
public async Task<IActionResult> GetCurrencyPairRate(string baseCurrency, string currency, CancellationToken cancellationToken) public async Task<IActionResult> GetCurrencyPairRate(string baseCurrency, string currency, string cryptoCode = null, CancellationToken cancellationToken = default)
{ {
var result = await GetRates2($"{baseCurrency}_{currency}", null, cancellationToken); var result = await GetRates2($"{baseCurrency}_{currency}", null, cryptoCode, cancellationToken);
return (result as JsonResult)?.Value is not Rate[] rates return (result as JsonResult)?.Value is not Rate[] rates
? result ? result
: Json(new DataWrapper<Rate>(rates.First())); : Json(new DataWrapper<Rate>(rates.First()));
@@ -74,9 +74,9 @@ namespace BTCPayServer.Controllers
[HttpGet("rates")] [HttpGet("rates")]
[BitpayAPIConstraint] [BitpayAPIConstraint]
public async Task<IActionResult> GetRates(string currencyPairs, string storeId = null, CancellationToken cancellationToken = default) public async Task<IActionResult> GetRates(string currencyPairs, string storeId = null, string cryptoCode = null, CancellationToken cancellationToken = default)
{ {
var result = await GetRates2(currencyPairs, storeId, cancellationToken); var result = await GetRates2(currencyPairs, storeId, cryptoCode, cancellationToken);
return (result as JsonResult)?.Value is not Rate[] rates return (result as JsonResult)?.Value is not Rate[] rates
? result ? result
: Json(new DataWrapper<Rate[]>(rates)); : Json(new DataWrapper<Rate[]>(rates));
@@ -84,7 +84,7 @@ namespace BTCPayServer.Controllers
[AllowAnonymous] [AllowAnonymous]
[HttpGet("api/rates")] [HttpGet("api/rates")]
public async Task<IActionResult> GetRates2(string currencyPairs, string storeId, CancellationToken cancellationToken) public async Task<IActionResult> GetRates2(string currencyPairs, string storeId, string cryptoCode = null, CancellationToken cancellationToken = default)
{ {
var store = CurrentStore ?? await _storeRepo.FindStore(storeId); var store = CurrentStore ?? await _storeRepo.FindStore(storeId);
if (store == null) if (store == null)
@@ -95,7 +95,12 @@ namespace BTCPayServer.Controllers
} }
if (currencyPairs == null) if (currencyPairs == null)
{ {
currencyPairs = store.GetStoreBlob().GetDefaultCurrencyPairString(); var blob = store.GetStoreBlob();
currencyPairs = blob.GetDefaultCurrencyPairString();
if (string.IsNullOrEmpty(currencyPairs) && !string.IsNullOrWhiteSpace(cryptoCode))
{
currencyPairs = $"{blob.DefaultCurrency}_{cryptoCode}".ToUpperInvariant();
}
if (string.IsNullOrEmpty(currencyPairs)) if (string.IsNullOrEmpty(currencyPairs))
{ {
var result = Json(new BitpayErrorsModel() { Error = "You need to setup the default currency pairs in 'Store Settings / Rates' or specify 'currencyPairs' query parameter (eg. BTC_USD,LTC_CAD)." }); var result = Json(new BitpayErrorsModel() { Error = "You need to setup the default currency pairs in 'Store Settings / Rates' or specify 'currencyPairs' query parameter (eg. BTC_USD,LTC_CAD)." });