1
1
namespace Stripe . Infrastructure
2
2
{
3
3
using System ;
4
+ using System . Collections . Generic ;
4
5
using Newtonsoft . Json ;
5
6
using Newtonsoft . Json . Linq ;
6
7
7
8
internal class PaymentIntentSourceActionConverter : JsonConverter
8
9
{
9
10
public override bool CanWrite => false ;
10
11
12
+ protected Dictionary < PaymentIntentSourceActionType , Func < string , IPaymentIntentSourceActionValue > > TypesToMapperFuncs
13
+ => new Dictionary < PaymentIntentSourceActionType , Func < string , IPaymentIntentSourceActionValue > > ( )
14
+ {
15
+ { PaymentIntentSourceActionType . AuthorizeWithUrl , Mapper < PaymentIntentSourceActionAuthorizeWithUrl > . MapFromJson } ,
16
+ } ;
17
+
11
18
public override bool CanConvert ( Type objectType )
12
19
{
13
- throw new NotImplementedException ( ) ;
20
+ return objectType == typeof ( PaymentIntentSourceAction ) ;
14
21
}
15
22
16
23
public override void WriteJson ( JsonWriter writer , object value , JsonSerializer serializer )
@@ -22,27 +29,52 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
22
29
23
30
public override object ReadJson ( JsonReader reader , Type objectType , object existingValue , JsonSerializer serializer )
24
31
{
25
- var sourceAction = new PaymentIntentSourceAction ( ) ;
26
-
27
32
if ( reader . TokenType == JsonToken . Null )
28
33
{
29
- sourceAction . Type = PaymentIntentSourceActionType . None ;
30
- return sourceAction ;
34
+ return null ;
31
35
}
32
36
33
37
var incoming = JObject . Load ( reader ) ;
38
+ var sourceAction = new PaymentIntentSourceAction ( ) ;
39
+
40
+ // Parse type
41
+ var typeString = incoming . SelectToken ( "type" ) ? . ToString ( ) ;
42
+ PaymentIntentSourceActionType type ;
43
+ if ( ! TryParseEnum < PaymentIntentSourceActionType > ( typeString , out type ) )
44
+ {
45
+ type = PaymentIntentSourceActionType . Unknown ;
46
+ }
47
+
48
+ sourceAction . Type = type ;
34
49
35
- if ( incoming . SelectToken ( "type" ) ? . ToString ( ) == "authorize_with_url" )
50
+ // Parse value according to type
51
+ if ( this . TypesToMapperFuncs . ContainsKey ( type ) )
36
52
{
37
- sourceAction . Type = PaymentIntentSourceActionType . AuthorizeWithUrl ;
38
- sourceAction . AuthorizeWithUrl = Mapper < PaymentIntentSourceActionAuthorizeWithUrl > . MapFromJson ( incoming . SelectToken ( "value" ) ? . ToString ( ) ) ;
53
+ var mapperFunc = this . TypesToMapperFuncs [ type ] ;
54
+ var valueString = incoming . SelectToken ( "value" ) ? . ToString ( ) ;
55
+ sourceAction . Value = mapperFunc ( valueString ) ;
39
56
}
40
57
else
41
58
{
42
- sourceAction . Type = PaymentIntentSourceActionType . Unknown ;
59
+ sourceAction . Value = null ;
43
60
}
44
61
45
62
return sourceAction ;
46
63
}
64
+
65
+ protected static bool TryParseEnum < T > ( string str , out T value )
66
+ {
67
+ try
68
+ {
69
+ value = JsonConvert . DeserializeObject < T > ( $ "\" { str } \" ") ;
70
+ }
71
+ catch ( JsonSerializationException )
72
+ {
73
+ value = default ( T ) ;
74
+ return false ;
75
+ }
76
+
77
+ return true ;
78
+ }
47
79
}
48
80
}
0 commit comments