Skip to content

Commit

Permalink
Merge pull request #215 from Mangopay/feature/conversions_quote
Browse files Browse the repository at this point in the history
Conversion Quote
  • Loading branch information
mihaimoiseanu authored Mar 7, 2024
2 parents ec2f176 + 9914b27 commit eb32e85
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 115 deletions.
134 changes: 134 additions & 0 deletions MangoPay.SDK.Tests/ApiConversionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using MangoPay.SDK.Core.Enumerations;
using MangoPay.SDK.Entities;
using MangoPay.SDK.Entities.GET;
using MangoPay.SDK.Entities.POST;
using NUnit.Framework;

namespace MangoPay.SDK.Tests
{
[TestFixture]
public class ApiConversionsTest : BaseTest
{
[Test]
public async Task Test_GetConversionRate()
{
var conversionRate = await Api.Conversions.GetConversionRate("EUR", "GBP");

Assert.IsNotNull(conversionRate);
Assert.IsNotNull(conversionRate.ClientRate);
Assert.IsNotNull(conversionRate.MarketRate);
}

[Test]
public async Task Test_CreateInstantConversion()
{
var createdInstantConversion = await CreateInstantConversion();

Assert.IsNotNull(createdInstantConversion);
Assert.IsNotNull(createdInstantConversion.CreditedFunds.Amount);
Assert.IsNotNull(createdInstantConversion.DebitedFunds.Amount);
Assert.AreEqual(createdInstantConversion.Status, TransactionStatus.SUCCEEDED);
Assert.AreEqual(createdInstantConversion.Type, TransactionType.CONVERSION);
}

[Test]
public async Task Test_GetInstantConversion()
{
var createdInstantConversion = await CreateInstantConversion();
var returnedInstantConversion = await Api.Conversions.GetInstantConversion(createdInstantConversion.Id);

Assert.IsNotNull(returnedInstantConversion);
Assert.IsNotNull(returnedInstantConversion.CreditedFunds.Amount);
Assert.IsNotNull(returnedInstantConversion.DebitedFunds.Amount);
Assert.AreEqual(returnedInstantConversion.Status, TransactionStatus.SUCCEEDED);
Assert.AreEqual(returnedInstantConversion.Type, TransactionType.CONVERSION);
}

[Test]
public async Task Test_CreateConversionQuote()
{
var createdConversionQuote = await CreateConversionQuote();

Assert.IsNotNull(createdConversionQuote);
Assert.IsNotNull(createdConversionQuote.CreditedFunds);
Assert.IsNotNull(createdConversionQuote.DebitedFunds);
Assert.IsNotNull(createdConversionQuote.ConversionRateResponse);
Assert.AreEqual("ACTIVE", createdConversionQuote.Status);
}

[Test]
public async Task Test_GetConversionQuote()
{
var createdConversionQuote = await CreateConversionQuote();
var returnedConversionQuote = await Api.Conversions.GetConversionQuote(createdConversionQuote.Id);

Assert.IsNotNull(returnedConversionQuote);
Assert.IsNotNull(returnedConversionQuote.CreditedFunds);
Assert.IsNotNull(returnedConversionQuote.DebitedFunds);
Assert.IsNotNull(returnedConversionQuote.ConversionRateResponse);
Assert.AreEqual("ACTIVE", returnedConversionQuote.Status);
}

[Test]
public async Task Test_CreateQuotedConversion()
{
var john = await GetJohn();
var wallet =
new WalletPostDTO(new List<string> { john.Id }, "WALLET IN GBP WITH MONEY", CurrencyIso.GBP);
var creditedWallet = await Api.Wallets.CreateAsync(wallet);

var debitedWallet = await GetJohnsWalletWithMoney();

var quote = await CreateConversionQuote();
var quotedConversionPostDTO = new QuotedConversionPostDTO(
quoteId: quote.Id,
authorId: debitedWallet.Owners[0],
debitedWalletId: debitedWallet.Id,
creditedWalletId: creditedWallet.Id,
tag: "Created using the Mangopay .NET SDK"
);

var quotedConversion = await this.Api.Conversions.CreateQuotedConversion(quotedConversionPostDTO);

Assert.IsNotNull(quotedConversion);
Assert.AreEqual(TransactionStatus.SUCCEEDED, quotedConversion.Status);
Assert.AreEqual(TransactionNature.REGULAR, quotedConversion.Nature);
}

private async Task<ConversionDTO> CreateInstantConversion()
{
var john = await GetJohn();
var wallet =
new WalletPostDTO(new List<string> { john.Id }, "WALLET IN GBP WITH MONEY", CurrencyIso.GBP);
var creditedWallet = await Api.Wallets.CreateAsync(wallet);

var debitedWallet = await GetJohnsWalletWithMoney();

var instantConversion = new ConversionPostDTO(
john.Id,
debitedWallet.Id,
creditedWallet.Id,
new Money { Amount = 30, Currency = CurrencyIso.EUR },
new Money { Currency = CurrencyIso.GBP },
new Money { Amount = 10, Currency = CurrencyIso.GBP },
"create instant conversion"
);

return await Api.Conversions.CreateInstantConversion(instantConversion);
}

private async Task<ConversionQuoteDTO> CreateConversionQuote()
{
var conversionQuote = new ConversionQuotePostDTO(
new Money { Amount = 30, Currency = CurrencyIso.EUR },
new Money { Currency = CurrencyIso.GBP },
90,
"Created using the Mangopay .NET SDK"
);

return await Api.Conversions.CreateConversionQuote(conversionQuote);
}
}
}
73 changes: 0 additions & 73 deletions MangoPay.SDK.Tests/ApiInstantConversionTest.cs

This file was deleted.

6 changes: 5 additions & 1 deletion MangoPay.SDK/Core/APIs/ApiBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ public abstract class ApiBase

{ MethodKey.GetConversionRate,new ApiEndPoint("/conversions/rate/{0}/{1}",RequestType.GET)},
{ MethodKey.CreateInstantConversion,new ApiEndPoint("/conversions/instant-conversion",RequestType.POST)},
{ MethodKey.GetInstantConversion,new ApiEndPoint("/conversions/{0}",RequestType.GET)}
{ MethodKey.GetInstantConversion,new ApiEndPoint("/conversions/{0}",RequestType.GET)},
{ MethodKey.CreateConversionQuote,new ApiEndPoint("/conversions/quote",RequestType.POST)},
{ MethodKey.GetConversionQuote, new ApiEndPoint("/conversions/quote/{0}", RequestType.GET)},
{ MethodKey.CreateQuotedConversion, new ApiEndPoint("/conversions/quoted-conversion", RequestType.POST)},

};

/// <summary>Creates new API instance.</summary>
Expand Down
58 changes: 58 additions & 0 deletions MangoPay.SDK/Core/APIs/ApiConversions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Threading.Tasks;
using MangoPay.SDK.Core.Enumerations;
using MangoPay.SDK.Entities.GET;
using MangoPay.SDK.Entities.POST;

namespace MangoPay.SDK.Core.APIs
{
public class ApiConversions : ApiBase
{
public ApiConversions(MangoPayApi root) : base(root)
{
}

public async Task<ConversionRateDTO> GetConversionRate(string debitedCurrency, string creditedCurrency)
{
return await this.GetObjectAsync<ConversionRateDTO>(MethodKey.GetConversionRate,
entitiesId: new[] { debitedCurrency, creditedCurrency });
}

public async Task<ConversionDTO> CreateInstantConversion(ConversionPostDTO conversion,
string idempotentKey = null)
{
return await
this.CreateObjectAsync<ConversionDTO, ConversionPostDTO>(MethodKey.CreateInstantConversion,
conversion, idempotentKey);
}

public async Task<ConversionDTO> GetInstantConversion(string id)
{
return await this.GetObjectAsync<ConversionDTO>(MethodKey.GetInstantConversion,
entitiesId: id);
}

public async Task<ConversionQuoteDTO> CreateConversionQuote(ConversionQuotePostDTO conversionQuote,
string idempotentKey = null)
{
return await this.CreateObjectAsync<ConversionQuoteDTO, ConversionQuotePostDTO>(
MethodKey.CreateConversionQuote,
conversionQuote,
idempotentKey);
}

public async Task<ConversionQuoteDTO> GetConversionQuote(string id)
{
return await this.GetObjectAsync<ConversionQuoteDTO>(MethodKey.GetConversionQuote, entitiesId: id);
}

public async Task<QuotedConversionDTO> CreateQuotedConversion(
QuotedConversionPostDTO quotedConversionPostDto,
string idempotentKey = null)
{
return await this.CreateObjectAsync<QuotedConversionDTO, QuotedConversionPostDTO>(
MethodKey.CreateQuotedConversion,
quotedConversionPostDto,
idempotentKey);
}
}
}
34 changes: 0 additions & 34 deletions MangoPay.SDK/Core/APIs/ApiInstantConversion.cs

This file was deleted.

2 changes: 1 addition & 1 deletion MangoPay.SDK/Core/APIs/ApiOAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public async Task<OAuthTokenDTO> CreateTokenAsync(CreateOAuthTokenPostDTO entity
restTool.AddRequestHttpHeader(Constants.CONTENT_TYPE, Constants.APPLICATION_X_WWW_FORM_URLENCODED);

return await restTool.RequestAsync<OAuthTokenDTO, CreateOAuthTokenPostDTO>(endPoint, null, entity);
}
}
}
}
5 changes: 4 additions & 1 deletion MangoPay.SDK/Core/Enumerations/MethodKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ public enum MethodKey

GetConversionRate,
CreateInstantConversion,
GetInstantConversion
GetInstantConversion,
CreateConversionQuote,
GetConversionQuote,
CreateQuotedConversion,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace MangoPay.SDK.Entities.GET
{
public class InstantConversionDTO: EntityBase
public class ConversionDTO: EntityBase
{
/// <summary>The unique identifier of the user at the source of the transaction.</summary>
public string AuthorId { get; set; }
Expand Down
26 changes: 26 additions & 0 deletions MangoPay.SDK/Entities/GET/ConversionQuoteDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using MangoPay.SDK.Core.Enumerations;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace MangoPay.SDK.Entities.GET
{
public class ConversionQuoteDTO : EntityBase
{
/// <summary>The date and time at which the quote expires</summary>
[JsonConverter(typeof(Core.UnixDateTimeConverter))]
public DateTime ExpirationDate { get; set; }

/// <summary>The status of the transaction.</summary>
public string Status { get; set; }

/// <summary>The sell funds</summary>
public Money DebitedFunds { get; set; }

/// <summary>The buy funds</summary>
public Money CreditedFunds { get; set; }

/// <summary>Real time indicative market rate of a specific currency pair</summary>
public ConversionRateDTO ConversionRateResponse { get; set; }
}
}
11 changes: 11 additions & 0 deletions MangoPay.SDK/Entities/GET/QuotedConversionDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MangoPay.SDK.Entities.GET
{
public class QuotedConversionDTO : TransactionDTO
{
/// <summary>The unique identifier of the active quote which guaranteed the rate for the conversion.</summary>
public string QuoteId { get; set; }

/// <summary>Information about the conversion rate used during the transaction.</summary>
public ConversionRateDTO ConversionRateResponse { get; set; }
}
}
Loading

0 comments on commit eb32e85

Please sign in to comment.