mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 14:34:23 +01:00
Fix: DateTime passed to bitpay API weren't assumed UTC, remove DateTime.Now references (#3206)
This commit is contained in:
41
BTCPayServer/ModelBinders/BitpayDateTimeOffsetModelBinder.cs
Normal file
41
BTCPayServer/ModelBinders/BitpayDateTimeOffsetModelBinder.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.VisualBasic.CompilerServices;
|
||||
|
||||
namespace BTCPayServer.ModelBinders
|
||||
{
|
||||
public class BitpayDateTimeOffsetModelBinder : IModelBinder
|
||||
{
|
||||
public Task BindModelAsync(ModelBindingContext bindingContext)
|
||||
{
|
||||
if (!typeof(DateTimeOffset).GetTypeInfo().IsAssignableFrom(bindingContext.ModelType) &&
|
||||
!typeof(DateTimeOffset?).GetTypeInfo().IsAssignableFrom(bindingContext.ModelType))
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
ValueProviderResult val = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
|
||||
string v = val.FirstValue as string;
|
||||
if (v == null)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var sec = DateTimeOffset.ParseExact(v, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
|
||||
bindingContext.Result = ModelBindingResult.Success(sec);
|
||||
}
|
||||
catch
|
||||
{
|
||||
bindingContext.Result = ModelBindingResult.Failed();
|
||||
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Invalid date (MM/dd/yyyy)");
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user