btcpayserver/BTCPayServer/ModelBinders/PaymentMethodIdModelBinder.cs

38 lines
1.2 KiB
C#
Raw Normal View History

2020-06-24 03:34:09 +02:00
using System.Reflection;
using System.Threading.Tasks;
using BTCPayServer.Payments;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace BTCPayServer.ModelBinders
{
public class PaymentMethodIdModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (!typeof(PaymentMethodId).GetTypeInfo().IsAssignableFrom(bindingContext.ModelType))
2020-06-24 03:34:09 +02:00
{
return Task.CompletedTask;
}
ValueProviderResult val = bindingContext.ValueProvider.GetValue(
bindingContext.ModelName);
string key = val.FirstValue as string;
if (key == null)
{
return Task.CompletedTask;
}
if (PaymentMethodId.TryParse(key, out var paymentId))
{
bindingContext.Result = ModelBindingResult.Success(paymentId);
}
else
{
bindingContext.Result = ModelBindingResult.Failed();
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Invalid payment id");
}
2020-06-24 03:34:09 +02:00
return Task.CompletedTask;
}
}
}