btcpayserver/BTCPayServer/ModelBinders/PaymentMethodIdModelBinder.cs
2021-12-27 13:15:43 +09:00

38 lines
1.2 KiB
C#

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(PaymentMethodIdModelBinder).GetTypeInfo().IsAssignableFrom(bindingContext.ModelType))
{
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");
}
return Task.CompletedTask;
}
}
}