Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an interface for PaymentIntentSourceAction #1329

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Stripe
{
public interface IPaymentIntentSourceActionValue
{
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
namespace Stripe
{
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Stripe.Infrastructure;

[JsonConverter(typeof(StringEnumConverter))]
public enum PaymentIntentSourceActionType
{
Unknown,

[EnumMember(Value = "authorize_with_url")]
AuthorizeWithUrl,
None,
}

[JsonConverter(typeof(PaymentIntentSourceActionConverter))]
public class PaymentIntentSourceAction : StripeEntity
{
public PaymentIntentSourceActionType Type { get; set; }

public PaymentIntentSourceActionAuthorizeWithUrl AuthorizeWithUrl { get; set; }
public IPaymentIntentSourceActionValue Value { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Stripe
{
using Newtonsoft.Json;

public class PaymentIntentSourceActionAuthorizeWithUrl : StripeEntity
public class PaymentIntentSourceActionAuthorizeWithUrl : StripeEntity, IPaymentIntentSourceActionValue
{
[JsonProperty("url")]
public string Url { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
namespace Stripe.Infrastructure
{
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

internal class PaymentIntentSourceActionConverter : JsonConverter
{
public override bool CanWrite => false;

protected Dictionary<PaymentIntentSourceActionType, Func<string, IPaymentIntentSourceActionValue>> TypesToMapperFuncs
=> new Dictionary<PaymentIntentSourceActionType, Func<string, IPaymentIntentSourceActionValue>>()
{
{ PaymentIntentSourceActionType.AuthorizeWithUrl, Mapper<PaymentIntentSourceActionAuthorizeWithUrl>.MapFromJson },
};

public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
return objectType == typeof(PaymentIntentSourceAction);
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
Expand All @@ -22,27 +29,52 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var sourceAction = new PaymentIntentSourceAction();

if (reader.TokenType == JsonToken.Null)
{
sourceAction.Type = PaymentIntentSourceActionType.None;
return sourceAction;
return null;
}

var incoming = JObject.Load(reader);
var sourceAction = new PaymentIntentSourceAction();

// Parse type
var typeString = incoming.SelectToken("type")?.ToString();
PaymentIntentSourceActionType type;
if (!TryParseEnum<PaymentIntentSourceActionType>(typeString, out type))
{
type = PaymentIntentSourceActionType.Unknown;
}

sourceAction.Type = type;

if (incoming.SelectToken("type")?.ToString() == "authorize_with_url")
// Parse value according to type
if (this.TypesToMapperFuncs.ContainsKey(type))
{
sourceAction.Type = PaymentIntentSourceActionType.AuthorizeWithUrl;
sourceAction.AuthorizeWithUrl = Mapper<PaymentIntentSourceActionAuthorizeWithUrl>.MapFromJson(incoming.SelectToken("value")?.ToString());
var mapperFunc = this.TypesToMapperFuncs[type];
var valueString = incoming.SelectToken("value")?.ToString();
sourceAction.Value = mapperFunc(valueString);
}
else
{
sourceAction.Type = PaymentIntentSourceActionType.Unknown;
sourceAction.Value = null;
}

return sourceAction;
}

protected static bool TryParseEnum<T>(string str, out T value)
{
try
{
value = JsonConvert.DeserializeObject<T>($"\"{str}\"");
}
catch (JsonSerializationException)
{
value = default(T);
return false;
}

return true;
}
}
}
37 changes: 34 additions & 3 deletions src/StripeTests/Entities/PaymentIntents/PaymentIntentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,23 @@ public void DeserializeWithExpansions()
}

[Fact]
public void DeserializeNextSourceAction()
public void DeserializeNextSourceActionNull()
{
var json = GetResourceAsString("api_fixtures.payment_intent_with_next_source_action.json");
var json = GetResourceAsString("api_fixtures.payment_intent.action_null.json");
var intent = Mapper<PaymentIntent>.MapFromJson(json);

Assert.NotNull(intent);
Assert.IsType<PaymentIntent>(intent);
Assert.NotNull(intent.Id);
Assert.Equal("payment_intent", intent.Object);

Assert.Null(intent.NextSourceAction);
}

[Fact]
public void DeserializeNextSourceActionAuthorizeWithUrl()
{
var json = GetResourceAsString("api_fixtures.payment_intent.action_authorize_with_url.json");
var intent = Mapper<PaymentIntent>.MapFromJson(json);

Assert.NotNull(intent);
Expand All @@ -61,7 +75,24 @@ public void DeserializeNextSourceAction()
Assert.Equal("payment_intent", intent.Object);

Assert.Equal(PaymentIntentSourceActionType.AuthorizeWithUrl, intent.NextSourceAction.Type);
Assert.Equal("https://stripe.com", intent.NextSourceAction.AuthorizeWithUrl.Url);
var authorizeWithUrl = intent.NextSourceAction.Value as PaymentIntentSourceActionAuthorizeWithUrl;
Assert.NotNull(authorizeWithUrl);
Assert.Equal("https://stripe.com", authorizeWithUrl.Url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test with None?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! Added two more tests, one where next_source_action is null and one where it's a type that the converter cannot handle.

}

[Fact]
public void DeserializeNextSourceActionUnknown()
{
var json = GetResourceAsString("api_fixtures.payment_intent.action_unknown.json");
var intent = Mapper<PaymentIntent>.MapFromJson(json);

Assert.NotNull(intent);
Assert.IsType<PaymentIntent>(intent);
Assert.NotNull(intent.Id);
Assert.Equal("payment_intent", intent.Object);

Assert.Equal(PaymentIntentSourceActionType.Unknown, intent.NextSourceAction.Type);
Assert.Null(intent.NextSourceAction.Value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"id": "pi_123",
"object": "payment_intent",
"allowed_source_types": [
"card"
],
"amount": 1099,
"amount_capturable": 1000,
"amount_received": 0,
"application": null,
"application_fee": null,
"canceled_at": null,
"capture_method": "automatic",
"charges": {
"object": "list",
"data": [
{
"id": "ch_123",
"object": "charge"
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/charges?payment_intent=pi_123"
},
"client_secret": null,
"confirmation_method": "publishable",
"created": 123456789,
"currency": "usd",
"customer": null,
"description": "PaymentIntent Description",
"livemode": false,
"metadata": {
"order_id": "123456789"
},
"next_source_action": null,
"on_behalf_of": null,
"receipt_email": "test@stripe.com",
"return_url": null,
"shipping": {
"address": {
"city": "city",
"country": "USA",
"line1": "123 Main St",
"line2": "Apt B",
"postal_code": "90210",
"state": "NC"
},
"carrier": "Carrier",
"name": "Namey Namerson",
"phone": null,
"tracking_number": "12345"
},
"source": "src_123",
"statement_descriptor": "PaymentIntent Statement Descriptor",
"status": "succeeded",
"transfer_data": {
"amount": 1234
},
"transfer_group": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"id": "pi_123",
"object": "payment_intent",
"allowed_source_types": [
"card"
],
"amount": 1099,
"amount_capturable": 1000,
"amount_received": 0,
"application": null,
"application_fee": null,
"canceled_at": null,
"capture_method": "automatic",
"charges": {
"object": "list",
"data": [
{
"id": "ch_123",
"object": "charge"
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/charges?payment_intent=pi_123"
},
"client_secret": null,
"confirmation_method": "publishable",
"created": 123456789,
"currency": "usd",
"customer": null,
"description": "PaymentIntent Description",
"livemode": false,
"metadata": {
"order_id": "123456789"
},
"next_source_action": {
"type": "foo",
"value": {
"bar": [1, 2, 3]
}
},
"on_behalf_of": null,
"receipt_email": "test@stripe.com",
"return_url": null,
"shipping": {
"address": {
"city": "city",
"country": "USA",
"line1": "123 Main St",
"line2": "Apt B",
"postal_code": "90210",
"state": "NC"
},
"carrier": "Carrier",
"name": "Namey Namerson",
"phone": null,
"tracking_number": "12345"
},
"source": "src_123",
"statement_descriptor": "PaymentIntent Statement Descriptor",
"status": "succeeded",
"transfer_data": {
"amount": 1234
},
"transfer_group": null
}