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

[Bugfix] added custom converted for CardInfo.Type #243

Merged
merged 1 commit into from
Feb 10, 2025
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
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 @@ -66,9 +68,9 @@
var wallet = await this.GetJohnsWallet();
var user = await this.GetJohn();

var payInPost = new PayInPayPalPostDTO(user.Id, new Money { Amount = 1000, Currency = CurrencyIso.EUR }, new Money { Amount = 0, Currency = CurrencyIso.EUR }, wallet.Id, "http://test/test");

Check warning on line 71 in MangoPay.SDK.Tests/ApiPayInsTest.cs

View workflow job for this annotation

GitHub Actions / dotnet 6.0.x

'PayInPayPalPostDTO' is obsolete: 'PayInPayPalPostDTO is deprecated, please use PayInPayPalWebPostDTO instead.'

var payIn = await this.Api.PayIns.CreatePayPalAsync(payInPost);

Check warning on line 73 in MangoPay.SDK.Tests/ApiPayInsTest.cs

View workflow job for this annotation

GitHub Actions / dotnet 6.0.x

'ApiPayIns.CreatePayPalAsync(PayInPayPalPostDTO, string)' is obsolete: 'CreatePayPalAsync is deprecated, please use CreatePayPalWebAsync instead.'

Assert.IsTrue(payIn.Id.Length > 0);
Assert.IsTrue(payIn.PaymentType == PayInPaymentType.PAYPAL);
Expand Down Expand Up @@ -198,12 +200,12 @@
PostalCode = "11222",
Region = "Region"
};
var payInPost = new PayInPayPalPostDTO(user.Id, new Money { Amount = 1000, Currency = CurrencyIso.EUR }, new Money { Amount = 0, Currency = CurrencyIso.EUR }, wallet.Id, "http://test/test")

Check warning on line 203 in MangoPay.SDK.Tests/ApiPayInsTest.cs

View workflow job for this annotation

GitHub Actions / dotnet 6.0.x

'PayInPayPalPostDTO' is obsolete: 'PayInPayPalPostDTO is deprecated, please use PayInPayPalWebPostDTO instead.'
{
ShippingAddress = new ShippingAddress("recipient name", AddressForShippingAddress)
};

var payIn = await this.Api.PayIns.CreatePayPalAsync(payInPost);

Check warning on line 208 in MangoPay.SDK.Tests/ApiPayInsTest.cs

View workflow job for this annotation

GitHub Actions / dotnet 6.0.x

'ApiPayIns.CreatePayPalAsync(PayInPayPalPostDTO, string)' is obsolete: 'CreatePayPalAsync is deprecated, please use CreatePayPalWebAsync instead.'

Assert.IsNotNull(payIn.ShippingAddress);
Assert.AreEqual("recipient name", payIn.ShippingAddress.RecipientName);
Expand Down Expand Up @@ -980,15 +982,15 @@
var wallet = await this.GetJohnsWallet();
var user = await this.GetJohn();

var payInPost = new PayInPayPalPostDTO(user.Id, new Money { Amount = 1000, Currency = CurrencyIso.EUR }, new Money { Amount = 0, Currency = CurrencyIso.EUR }, wallet.Id, "http://test/test");

Check warning on line 985 in MangoPay.SDK.Tests/ApiPayInsTest.cs

View workflow job for this annotation

GitHub Actions / dotnet 6.0.x

'PayInPayPalPostDTO' is obsolete: 'PayInPayPalPostDTO is deprecated, please use PayInPayPalWebPostDTO instead.'

payIn = await this.Api.PayIns.CreatePayPalAsync(payInPost);

Check warning on line 987 in MangoPay.SDK.Tests/ApiPayInsTest.cs

View workflow job for this annotation

GitHub Actions / dotnet 6.0.x

'ApiPayIns.CreatePayPalAsync(PayInPayPalPostDTO, string)' is obsolete: 'CreatePayPalAsync is deprecated, please use CreatePayPalWebAsync instead.'

Assert.IsTrue(payIn.Id.Length > 0);
Assert.IsTrue(payIn.PaymentType == PayInPaymentType.PAYPAL);
Assert.IsTrue(payIn.ExecutionType == PayInExecutionType.WEB);

var getPayIn = await this.Api.PayIns.GetPayPalAsync(payIn.Id);

Check warning on line 993 in MangoPay.SDK.Tests/ApiPayInsTest.cs

View workflow job for this annotation

GitHub Actions / dotnet 6.0.x

'ApiPayIns.GetPayPalAsync(string)' is obsolete: 'GetPayPalAsync is deprecated, please use GetPayPalWebAsync instead.'

Assert.IsNotNull(getPayIn);
Assert.IsTrue(getPayIn.Id == payIn.Id);
Expand Down Expand Up @@ -1017,14 +1019,14 @@
Region = "Region"
};

var payInPost = new PayInPayPalPostDTO(user.Id, new Money { Amount = 1000, Currency = CurrencyIso.EUR }, new Money { Amount = 0, Currency = CurrencyIso.EUR }, wallet.Id, "http://test/test")

Check warning on line 1022 in MangoPay.SDK.Tests/ApiPayInsTest.cs

View workflow job for this annotation

GitHub Actions / dotnet 6.0.x

'PayInPayPalPostDTO' is obsolete: 'PayInPayPalPostDTO is deprecated, please use PayInPayPalWebPostDTO instead.'
{
ShippingAddress = new ShippingAddress("recipient name", addressForShippingAddress)
};

payIn = await this.Api.PayIns.CreatePayPalAsync(payInPost);

Check warning on line 1027 in MangoPay.SDK.Tests/ApiPayInsTest.cs

View workflow job for this annotation

GitHub Actions / dotnet 6.0.x

'ApiPayIns.CreatePayPalAsync(PayInPayPalPostDTO, string)' is obsolete: 'CreatePayPalAsync is deprecated, please use CreatePayPalWebAsync instead.'

var getPayIn = await this.Api.PayIns.GetPayPalAsync(payIn.Id);

Check warning on line 1029 in MangoPay.SDK.Tests/ApiPayInsTest.cs

View workflow job for this annotation

GitHub Actions / dotnet 6.0.x

'ApiPayIns.GetPayPalAsync(string)' is obsolete: 'GetPayPalAsync is deprecated, please use GetPayPalWebAsync instead.'

Assert.IsNotNull(getPayIn.ShippingAddress);
Assert.AreEqual("recipient name", getPayIn.ShippingAddress.RecipientName);
Expand Down Expand Up @@ -1986,5 +1988,55 @@
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