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(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);
|
|
|
|
}
|
2021-04-26 05:55:03 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bindingContext.Result = ModelBindingResult.Failed();
|
|
|
|
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Invalid payment id");
|
|
|
|
}
|
2020-06-24 03:34:09 +02:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|