Skip to content

Commit

Permalink
UsCompany response converter (#395)
Browse files Browse the repository at this point in the history
* Added entityDetailsResponse check

* Mods at unit testing for USCompany

* Removed line comment

* Fixed breaking lines
  • Loading branch information
leandro-tylkovitch-cko authored May 16, 2024
1 parent af1bea3 commit 808c043
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/CheckoutSdk/Accounts/Company.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Checkout.Common;
using Checkout.Accounts.Regional;
using Checkout.Common;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Checkout.Accounts
Expand Down
1 change: 1 addition & 0 deletions src/CheckoutSdk/Accounts/IAccountsClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Checkout.Accounts.Payout.Request;
using Checkout.Accounts.Payout.Response;
using Checkout.Accounts.Regional.US;
using Checkout.Common;
using System;
using System.Threading;
Expand Down
6 changes: 4 additions & 2 deletions src/CheckoutSdk/Accounts/OnboardEntityDetailsResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Checkout.Common;
using Checkout.Accounts.Regional;
using Checkout.Common;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Checkout.Accounts
Expand All @@ -18,7 +20,7 @@ public class OnboardEntityDetailsResponse : Resource
public ContactDetails ContactDetails { get; set; }

public Profile Profile { get; set; }

public Company Company { get; set; }

public Individual Individual { get; set; }
Expand Down
65 changes: 65 additions & 0 deletions src/CheckoutSdk/Accounts/Regional/CompanyTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Checkout.Accounts.Regional.US;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Reflection;

namespace Checkout.Accounts.Regional
{
public class CompanyTypeConverter : JsonConverter
{
public override bool CanWrite => false;

public override bool CanConvert(Type objectType)
{
return typeof(OnboardEntityDetailsResponse).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
}

public override void WriteJson(
JsonWriter writer,
object value,
Newtonsoft.Json.JsonSerializer serializer)
{
throw new NotImplementedException();
}

public override object ReadJson(
JsonReader reader,
Type objectType,
object existingValue,
Newtonsoft.Json.JsonSerializer serializer)
{

if (reader.TokenType == JsonToken.Null)
return null;

var jObject = JToken.Load(reader);
if (jObject.Type == JTokenType.Object)
{
var businessTypeToken = jObject["company"]?["business_type"];
if (businessTypeToken != null && businessTypeToken.Type == JTokenType.String )
{
var enumString = businessTypeToken.Value<string>();

var businessType = CheckoutUtils.GetEnumFromStringMemberValue<BusinessType>(enumString);
if (businessType != null)
{
var target = new OnboardEntityDetailsResponse();
serializer.Populate(jObject.CreateReader(), target);
return target;
}

var usBusinessType = CheckoutUtils.GetEnumFromStringMemberValue<USBusinessType>(enumString);
if (usBusinessType != null)
{
var target = new OnboardEntityDetailsUSCompanyResponse();
serializer.Populate(jObject.CreateReader(), target);
return target;
}
}
}

throw new JsonSerializationException($"Unexpected token or value when parsing enum. Token: {reader.TokenType}, Value: {jObject}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Checkout.Accounts.Regional.US
{
public class OnboardEntityDetailsUSCompanyResponse : OnboardEntityDetailsResponse
{
public new USCompany Company { get; set; }
}
}
4 changes: 3 additions & 1 deletion src/CheckoutSdk/Accounts/Regional/US/USCompany.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Checkout.Accounts.Regional.US
using Newtonsoft.Json;

namespace Checkout.Accounts.Regional.US
{
public class USCompany : Company
{
Expand Down
2 changes: 2 additions & 0 deletions src/CheckoutSdk/JsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Checkout.Accounts.Payout.Response.Util;
using Checkout.Accounts.Regional;
using Checkout.Instruments.Create.Util;
using Checkout.Instruments.Get.Util;
using Checkout.Instruments.Update.Util;
Expand Down Expand Up @@ -50,6 +51,7 @@ private static JsonSerializerSettings CreateSerializerSettings(Action<JsonSerial
ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() },
Converters = new JsonConverter[]
{
new CompanyTypeConverter(),
new StringEnumConverter(),
// Instruments CS2
new CreateInstrumentResponseTypeConverter(),
Expand Down
1 change: 1 addition & 0 deletions test/CheckoutSdkTest/Accounts/AccountsClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ private async Task ShouldUpdateEntityUsCompany()
response.ShouldNotBeNull();
response.Id.ShouldBe(responseObject.Id);
response.Reference.ShouldBe(responseObject.Reference);

}

[Fact]
Expand Down
79 changes: 78 additions & 1 deletion test/CheckoutSdkTest/Accounts/AccountsIntegrationTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Checkout.Accounts.Regional.US;
using Checkout.Common;
using Checkout.Instruments;
using Shouldly;
Expand Down Expand Up @@ -89,7 +90,7 @@ public async Task ShouldCreateGetAndUpdateOnboardEntity()
verifyUpdated.ShouldNotBeNull();
onboardEntityRequest.Individual.FirstName.ShouldBe(verifyUpdated.Individual.FirstName);
}

[Fact(Skip = "unavailable")]
public async Task ShouldThrowConflictWhenCreatingExistingEntity()
{
Expand Down Expand Up @@ -209,6 +210,82 @@ private async Task ShouldCreateAndRetrievePaymentInstrument()
queryResponse.ShouldNotBeNull();
queryResponse.Data.ShouldNotBeNull();
}


[Fact]
private async Task ShouldCreateAndRetrievePaymentInstrumentUSCompany()
{
CheckoutApi api = GetAccountsCheckoutApi();

var entityRequest = new OnboardEntityRequest
{
Reference = RandomString(15),
ContactDetails = BuildContactDetails(),
Profile = BuildProfile(),
Company = new USCompany()
{
BusinessRegistrationNumber = "01234567",
BusinessType = USBusinessType.PrivateCorporation,
LegalName = "Super Hero Masks Inc.",
TradingName = "Super Hero Masks",
PrincipalAddress = GetAddress(),
RegisteredAddress = GetAddress(),
Representatives = new List<Representative>
{
new Representative
{
FirstName = "John",
LastName = "Doe",
Address = GetAddress(),
Identification = new Identification { NationalIdNumber = "AB123456C", }
}
}
}
};

var entityResponse = await api.AccountsClient().CreateEntity(entityRequest);

var entityDetailsResponse = await api.AccountsClient().GetEntity(entityResponse.Id);

var file = await UploadFile();

var instrumentRequest = new PaymentInstrumentRequest
{
Label = "Barclays",
Type = InstrumentType.BankAccount,
Currency = Currency.GBP,
Country = CountryCode.GB,
DefaultDestination = false,
Document = new InstrumentDocument { Type = "bank_statement", FileId = file.Id },
InstrumentDetails = new InstrumentDetailsFasterPayments
{
AccountNumber = "12334454", BankCode = "050389"
}
};

var instrumentResponse = await api.AccountsClient().CreatePaymentInstrument(entityResponse.Id, instrumentRequest);
instrumentResponse.ShouldNotBeNull();
instrumentResponse.Id.ShouldNotBeNull();

entityDetailsResponse.ShouldNotBeNull();

var instrumentDetails = await api.AccountsClient().RetrievePaymentInstrumentDetails(entityResponse.Id, instrumentResponse.Id);
instrumentDetails.ShouldNotBeNull();
instrumentDetails.Id.ShouldNotBeNull();
instrumentDetails.Status.ShouldNotBeNull();
instrumentDetails.Label.ShouldNotBeNull();
instrumentDetails.Type.ShouldNotBeNull();
instrumentDetails.Currency.ShouldNotBeNull();
instrumentDetails.Country.ShouldNotBeNull();
instrumentDetails.Document.ShouldNotBeNull();

var queryResponse = await api.AccountsClient().QueryPaymentInstruments(entityResponse.Id);
queryResponse.ShouldNotBeNull();
queryResponse.Data.ShouldNotBeNull();
}




private static string RandomString(int length)
{
Expand Down
11 changes: 11 additions & 0 deletions test/CheckoutSdkTest/CheckoutSdkTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@
<Content Include="Resources\get_financial_actions_response.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Remove="Resources\OnBoardSubEntityCompanyResponse.json" />
<Content Include="Resources\OnBoardSubEntityCompanyResponse.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Remove="Resources\OnBoardSubEntityUSCompanyResponse.json" />
<Content Include="Resources\OnBoardSubEntityUSCompanyResponse.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Update="Resources\OnBoardSubEntityCompanyResponse.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Folder Include="Apm\Previous" />
Expand Down
24 changes: 24 additions & 0 deletions test/CheckoutSdkTest/JsonSerializerTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Checkout.Accounts;
using Checkout.Accounts.Regional.US;
using Checkout.Financial;
using Checkout.Issuing.Cards;
using Checkout.Issuing.Cards.Requests.Create;
Expand Down Expand Up @@ -126,6 +128,28 @@ public void ShouldSerializeDateTimeFormatsFromJson()
financialActionsQueryResponse.ShouldNotBeNull();
financialActionsQueryResponse.Data[0].ProcessedOn.ShouldNotBeNull();
}

[Fact]
public void ShouldSerializeOnBoardSubEntityCompanyFromJson()
{
var fileContent = GetJsonFileContent("./Resources/OnBoardSubEntityCompanyResponse.json");
OnboardEntityDetailsResponse onboardEntityDetailsResponse =
(OnboardEntityDetailsResponse)new JsonSerializer().Deserialize(fileContent,
typeof(OnboardEntityDetailsResponse));
onboardEntityDetailsResponse.ShouldNotBeNull();
onboardEntityDetailsResponse.Company.BusinessType.ShouldBeOfType<BusinessType>();
}

[Fact]
public void ShouldSerializeOnBoardSubEntityUSCompanyFromJson()
{
var fileContent = GetJsonFileContent("./Resources/OnBoardSubEntityUSCompanyResponse.json");
OnboardEntityDetailsUSCompanyResponse onboardEntityDetailsResponse =
(OnboardEntityDetailsUSCompanyResponse)new JsonSerializer().Deserialize(fileContent,
typeof(OnboardEntityDetailsUSCompanyResponse));
onboardEntityDetailsResponse.ShouldNotBeNull();
onboardEntityDetailsResponse.Company.BusinessType.ShouldBeOfType<USBusinessType>();
}

[Fact]
public void ShouldDeserializeDateTimeFormatsFromStrings()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"id": "ent_saf19jffasdf",
"contact_details": {
"phone": {
"number": "2345678910"
},
"email_addresses": {
"primary": "[email protected]"
}
},
"profile": {
"urls": [
"https://www.superheroexample.com"
],
"mccs": [
"5311"
],
"default_holding_currency": "GBP",
"holding_currencies": [
"GBP"
]
},
"company": {
"business_registration_number": "12345678",
"business_type": "public_limited_company",
"legal_name": "Acme Corporation Inc.",
"trading_name": "Acme Corporation",
"principal_address": {
"address_line1": "Flat 456",
"city": "London",
"zip": "SW1A 1AA",
"country": "GB"
},
"registered_address": {
"address_line1": "Flat 456",
"city": "London",
"zip": "SW1A 1AA",
"country": "GB"
},
"representatives": [
{
"id": "rep_1d1j98j1h991dlkjdajsjd",
"first_name": "John",
"last_name": "Smith",
"address": {
"address_line1": "Flat 456",
"city": "London",
"zip": "SW1A 1AA",
"country": "GB"
},
"identification": {
"national_id_number": "AB123456C",
"document": {
"type": "driving_license",
"front": "file_wxglze3wwywujg4nna5fb7ldli",
"back": "file_adglze3wwywujg4nna5fb7l1sg"
}
},
"phone": {
"number": "2345678910"
},
"roles": [
"authorised_signatory",
"ubo"
]
}
]
}
}
Loading

0 comments on commit 808c043

Please sign in to comment.