-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Json converter to parse mandate payment specific responses
- Loading branch information
1 parent
9187c71
commit 0899d28
Showing
5 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/Mollie.Api/Framework/Factories/MandateResponseFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/Mollie.Api/Models/Mandate/Response/PaymentSpecificParameters/PayPalMandateResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
} | ||
|
||
|