Skip to content

Commit

Permalink
Create Json converter to parse mandate payment specific responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Viincenttt committed Jun 4, 2024
1 parent 9187c71 commit 0899d28
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
25 changes: 25 additions & 0 deletions src/Mollie.Api/Framework/Factories/MandateResponseFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using Mollie.Api.Models.Mandate.Response;
using Mollie.Api.Models.Mandate.Response.PaymentSpecificParameters;
using Mollie.Api.Models.Payment;

namespace Mollie.Api.Framework.Factories;

internal class MandateResponseFactory {
public MandateResponse Create(string? paymentMethod) {
if (string.IsNullOrEmpty(paymentMethod)) {
return Activator.CreateInstance<MandateResponse>();
}

switch (paymentMethod) {
case PaymentMethod.PayPal:
return Activator.CreateInstance<PayPalMandateResponse>();
case PaymentMethod.DirectDebit:
return Activator.CreateInstance<SepaDirectDebitMandateResponse>();
case PaymentMethod.CreditCard:
return Activator.CreateInstance<CreditCardMandateResponse>();
default:
return Activator.CreateInstance<MandateResponse>();
}
}
}
2 changes: 2 additions & 0 deletions src/Mollie.Api/Framework/JsonConverterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ private JsonSerializerSettings CreateDefaultJsonDeserializerSettings() {
new BalanceReportResponseJsonConverter(new BalanceReportResponseFactory()),
// Add a special converter for the balance transaction responses, because we need to create specific classes based on the transaction type
new BalanceTransactionJsonConverter(new BalanceTransactionFactory()),
// Add a special converter for mandate responses, because we need to create specific classes based on the payment method
new MandateResponseConverter(new MandateResponseFactory())
}
};
}
Expand Down
31 changes: 31 additions & 0 deletions src/Mollie.Api/JsonConverters/MandateResponseConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Mollie.Api.Framework.Factories;
using Mollie.Api.Models.Mandate.Response;
using Newtonsoft.Json.Linq;

namespace Mollie.Api.JsonConverters;

internal class MandateResponseConverter : JsonCreationConverter<MandateResponse> {
private readonly MandateResponseFactory _mandateResponseFactory;

public MandateResponseConverter(MandateResponseFactory mandateResponseFactory) {
_mandateResponseFactory = mandateResponseFactory;
}

protected override MandateResponse Create(Type objectType, JObject jObject) {
string? paymentMethod = GetPaymentMethod(jObject);

return _mandateResponseFactory.Create(paymentMethod);
}

private string? GetPaymentMethod(JObject jObject) {
if (FieldExists("method", jObject)) {
string paymentMethodValue = (string) jObject["method"]!;
if (!string.IsNullOrEmpty(paymentMethodValue)) {
return paymentMethodValue;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Mollie.Api.Models.Mandate.Response.PaymentSpecificParameters;

public record PayPalMandateResponse {
public record PayPalMandateResponse : MandateResponse {
public required PayPalMandateResponseDetails Details { get; set; }
}

Expand Down
36 changes: 32 additions & 4 deletions tests/Mollie.Tests.Integration/Api/MandateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
using Mollie.Api.Models.Mandate.Request;
using Mollie.Api.Models.Mandate.Request.PaymentSpecificParameters;
using Mollie.Api.Models.Mandate.Response;
using Mollie.Api.Models.Mandate.Response.PaymentSpecificParameters;
using Mollie.Api.Models.Payment;
using Mollie.Api.Models.Payment.Response.PaymentSpecificParameters;
using Mollie.Tests.Integration.Framework;

namespace Mollie.Tests.Integration.Api;
Expand Down Expand Up @@ -56,12 +58,12 @@ public async Task ListMandatesNeverReturnsMoreCustomersThenTheNumberOfRequestedM
}

[DefaultRetryFact]
public async Task CanCreateMandate() {
public async Task CanCreateSepaDirectDebitMandate() {
// We can only test this if there are customers
ListResponse<CustomerResponse> customers = await _customerClient.GetCustomerListAsync();
if (customers.Count > 0) {
// If: We create a new mandate request
SepaDirectDebitMandateRequest mandateRequest = new SepaDirectDebitMandateRequest {
SepaDirectDebitMandateRequest mandateRequest = new () {
ConsumerAccount = "NL26ABNA0516682814",
ConsumerName = "John Doe",
Method = PaymentMethod.DirectDebit
Expand All @@ -71,8 +73,34 @@ public async Task CanCreateMandate() {
MandateResponse mandateResponse = await _mandateClient.CreateMandateAsync(customers.Items.First().Id, mandateRequest);

// Then: Make sure we created a new mandate
mandateResponse.Details!.ConsumerAccount.Should().Be(mandateRequest.ConsumerAccount);
mandateResponse.Details.ConsumerName.Should().Be(mandateRequest.ConsumerName);
mandateResponse.Should().BeOfType<SepaDirectDebitMandateResponse>();
var sepaDirectDebitResponse = (SepaDirectDebitMandateResponse)mandateResponse;
sepaDirectDebitResponse.Details.ConsumerAccount.Should().Be(mandateRequest.ConsumerAccount);
sepaDirectDebitResponse.Details.ConsumerName.Should().Be(mandateRequest.ConsumerName);
}
}

[DefaultRetryFact]
public async Task CanCreatePayPalMandate() {
// We can only test this if there are customers
ListResponse<CustomerResponse> customers = await _customerClient.GetCustomerListAsync();
if (customers.Count > 0) {
// If: We create a new mandate request
PayPalMandateRequest mandateRequest = new () {
ConsumerName = "John Doe",
Method = PaymentMethod.PayPal,
PaypalBillingAgreementId = "paypal-billing-agreement-id",
ConsumerEmail = "[email protected]"
};

// When: We send the mandate request
MandateResponse mandateResponse = await _mandateClient.CreateMandateAsync(customers.Items.First().Id, mandateRequest);

// Then: Make sure we created a new mandate
mandateResponse.Should().BeOfType<PayPalMandateResponse>();
var paypalMandateResponse = (PayPalMandateResponse)mandateResponse;
paypalMandateResponse.Details.ConsumerAccount.Should().Be(mandateRequest.ConsumerEmail);
paypalMandateResponse.Details.ConsumerName.Should().Be(mandateRequest.ConsumerName);
}
}

Expand Down

0 comments on commit 0899d28

Please sign in to comment.