Skip to content

Commit

Permalink
Merge pull request #243 from Mangopay/bugfix/cardinfo-type-enum
Browse files Browse the repository at this point in the history
[Bugfix] added custom converted for CardInfo.Type
  • Loading branch information
iulian03 authored Feb 10, 2025
2 parents 02fcf3d + 051115e commit e240e17
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
52 changes: 52 additions & 0 deletions MangoPay.SDK.Tests/ApiPayInsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
using System.Threading.Tasks;
using MangoPay.SDK.Core;
using MangoPay.SDK.Core.Enumerations;
using MangoPay.SDK.Core.Serializers;
using MangoPay.SDK.Entities;
using MangoPay.SDK.Entities.GET;
using MangoPay.SDK.Entities.POST;
using MangoPay.SDK.Entities.PUT;
using Newtonsoft.Json;
using NUnit.Framework;

namespace MangoPay.SDK.Tests
Expand Down Expand Up @@ -1986,5 +1988,55 @@ public async Task Test_PayIns_Create_Recurring_MIT_CheckCardInfo()
Assert.Fail(ex.Message);
}
}

[Test]
public async Task Test_Serialize_CardInfo()
{
var settings = new JsonSerializerSettings
{
Converters = { new CardInfoTypeConverter() }
};

var cardInfo = new CardInfo();
cardInfo.Type = null;

var payIn = new PayInCardDirectDTO();
payIn.CardInfo = cardInfo;

string json = JsonConvert.SerializeObject(payIn, settings);
Assert.IsTrue(json.Contains("\"Type\":null"));

cardInfo.Type = CardInfoType.CREDIT;
json = JsonConvert.SerializeObject(payIn, settings);
Assert.IsTrue(json.Contains("\"Type\":\"CREDIT\""));

cardInfo.Type = CardInfoType.CHARGE_CARD;
json = JsonConvert.SerializeObject(payIn, settings);
Assert.IsTrue(json.Contains("\"Type\":\"CHARGE CARD\""));
}

[Test]
public async Task Test_Dserialize_CardInfo()
{
var settings = new JsonSerializerSettings
{
Converters = { new CardInfoTypeConverter() }
};

string json =
"{\"CardInfo\":{\"BIN\":null,\"IssuingBank\":null,\"IssuerCountryCode\":0,\"Type\":null,\"Brand\":null,\"SubType\":null}}";
var deserialized = JsonConvert.DeserializeObject<PayInCardDirectDTO>(json, settings);
Assert.AreEqual(null, deserialized.CardInfo.Type);

json =
"{\"CardInfo\":{\"BIN\":null,\"IssuingBank\":null,\"IssuerCountryCode\":0,\"Type\":\"DEBIT\",\"Brand\":null,\"SubType\":null}}";
deserialized = JsonConvert.DeserializeObject<PayInCardDirectDTO>(json, settings);
Assert.AreEqual(CardInfoType.DEBIT, deserialized.CardInfo.Type);

json =
"{\"CardInfo\":{\"BIN\":null,\"IssuingBank\":null,\"IssuerCountryCode\":0,\"Type\":\"CHARGE_CARD\",\"Brand\":null,\"SubType\":null}}";
deserialized = JsonConvert.DeserializeObject<PayInCardDirectDTO>(json, settings);
Assert.AreEqual(CardInfoType.CHARGE_CARD, deserialized.CardInfo.Type);
}
}
}
35 changes: 35 additions & 0 deletions MangoPay.SDK/Core/Serializers/CardInfoTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using MangoPay.SDK.Core.Enumerations;
using Newtonsoft.Json;

namespace MangoPay.SDK.Core.Serializers
{
/// <summary>
/// Serialize "CHARGE_CARD" as "CHARGE CARD"
/// </summary>
public class CardInfoTypeConverter : JsonConverter<CardInfoType?>
{
public override void WriteJson(JsonWriter writer, CardInfoType? value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
return;
}
var stringValue = value == CardInfoType.CHARGE_CARD ? "CHARGE CARD" : value.ToString();
writer.WriteValue(stringValue);
}

public override CardInfoType? ReadJson(JsonReader reader, Type objectType, CardInfoType? existingValue,
bool hasExistingValue, JsonSerializer serializer)
{
if (reader.Value == null)
{
return null;
}
var value = reader.Value.ToString();
var cardInfo = value == "CHARGE CARD" ? CardInfoType.CHARGE_CARD : (CardInfoType)Enum.Parse(typeof(CardInfoType), value);
return cardInfo;
}
}
}
3 changes: 3 additions & 0 deletions MangoPay.SDK/Entities/CardInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using MangoPay.SDK.Core.Enumerations;
using MangoPay.SDK.Core.Serializers;
using Newtonsoft.Json;

namespace MangoPay.SDK.Entities
{
Expand All @@ -15,6 +17,7 @@ public class CardInfo
public CountryIso IssuerCountryCode;

/// <summary>The type of card product: DEBIT, CREDIT, CHARGE CARD.</summary>
[JsonConverter(typeof(CardInfoTypeConverter))]
public CardInfoType? Type;

/// <summary>The card brand. Examples include: AMERICAN EXPRESS, DISCOVER, JCB, MASTERCARD, VISA, etc.</summary>
Expand Down

0 comments on commit e240e17

Please sign in to comment.