Skip to content

Commit

Permalink
Add Get Part Payment CampaignCode info (#306)
Browse files Browse the repository at this point in the history
* Add Get Part Payment CampaignCode info

* Add MonthlyAmount for GetPartPaymentCampaignCodeInfo

* Add comment about utility property
  • Loading branch information
dusanbobicic committed Aug 28, 2024
1 parent 3a48378 commit daebefc
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/Svea.WebPay.SDK.Tests/Checkout/PartPaymentCampaignTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Svea.WebPay.SDK.CheckoutApi;
using Svea.WebPay.SDK.Json;
using Svea.WebPay.SDK.Tests.Helpers;
using Svea.WebPay.SDK.Tests.Helpers.DataSamples;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Xunit;

namespace Svea.WebPay.SDK.Tests.Checkout
{
public class PartPaymentCampaignTests : TestBase
{

[Fact]
public async Task ShouldReturnPartPaymentCampaigns()
{
// Arrange
var expectedOrder = JsonSerializer.Deserialize<List<CampaignCodeInfo>>(UtilityDataSamples.GetPartPaymentCampaigns, JsonSerialization.Settings);
var sveaClient = SveaClient(CreateHandlerMock(UtilityDataSamples.GetPartPaymentCampaigns));

// Act
var actualPartPaymentCampaigns = await sveaClient.Checkout.Utility.GetPartPaymentCampaigns(true);

// Assert
Assert.Equal(expectedOrder.Count, actualPartPaymentCampaigns.Count);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Svea.WebPay.SDK.Tests.Helpers.DataSamples
{
public static class UtilityDataSamples
{
public static string GetPartPaymentCampaigns = @"[
{
""CampaignCode"": 25,
""Description"": ""sample string 26"",
""PaymentPlanType"": 0,
""ContractLengthInMonths"": 27,
""MonthlyAnnuityFactor"": 28.0,
""InitialFee"": 29.0,
""NotificationFee"": 30.0,
""InterestRatePercent"": 31.0,
""NumberOfInterestFreeMonths"": 32,
""NumberOfPaymentFreeMonths"": 33,
""FromAmount"": 34.0,
""ToAmount"": 35.0,
""MonthlyAmount"": 188.00
},
{
""CampaignCode"": 25,
""Description"": ""sample string 26"",
""PaymentPlanType"": 0,
""ContractLengthInMonths"": 27,
""MonthlyAnnuityFactor"": 28.0,
""InitialFee"": 29.0,
""NotificationFee"": 30.0,
""InterestRatePercent"": 31.0,
""NumberOfInterestFreeMonths"": 32,
""NumberOfPaymentFreeMonths"": 33,
""FromAmount"": 34.00,
""ToAmount"": 35.00,
""MonthlyAmount"": 188.00
}
]";
}
}
26 changes: 25 additions & 1 deletion src/Svea.WebPay.SDK/CheckoutApi/CampaignCodeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@
{
public class CampaignCodeInfo
{
public CampaignCodeInfo(long campaignCode, string description, PaymentPlanTypeCode paymentPlanType, int contractLengthInMonths, decimal monthlyAnnuityFactor, decimal initialFee, decimal notificationFee, decimal interestRatePercent, int numberOfInterestFreeMonths, int numberOfPaymentFreeMonths, decimal fromAmount, decimal amount)
public CampaignCodeInfo() { }
/// <summary>
///
/// </summary>
/// <param name="campaignCode"></param>
/// <param name="description"></param>
/// <param name="paymentPlanType"></param>
/// <param name="contractLengthInMonths"></param>
/// <param name="monthlyAnnuityFactor"></param>
/// <param name="initialFee"></param>
/// <param name="notificationFee"></param>
/// <param name="interestRatePercent"></param>
/// <param name="numberOfInterestFreeMonths"></param>
/// <param name="numberOfPaymentFreeMonths"></param>
/// <param name="fromAmount"></param>
/// <param name="amount"></param>
/// <param name="monthlyAmount">Is only calculated when an amount was added to the request.</param>
public CampaignCodeInfo(long campaignCode, string description, PaymentPlanTypeCode paymentPlanType, int contractLengthInMonths, decimal monthlyAnnuityFactor,
decimal initialFee, decimal notificationFee, decimal interestRatePercent, int numberOfInterestFreeMonths,
int numberOfPaymentFreeMonths, decimal fromAmount, decimal amount, decimal? monthlyAmount = null)
{
CampaignCode = campaignCode;
Description = description;
Expand All @@ -16,6 +35,7 @@ public CampaignCodeInfo(long campaignCode, string description, PaymentPlanTypeCo
NumberOfPaymentFreeMonths = numberOfPaymentFreeMonths;
FromAmount = fromAmount;
ToAmount = amount;
MonthlyAmount = monthlyAmount;
}

public long CampaignCode { get; set; }
Expand All @@ -30,5 +50,9 @@ public CampaignCodeInfo(long campaignCode, string description, PaymentPlanTypeCo
public int NumberOfPaymentFreeMonths { get; set; }
public decimal FromAmount { get; set; }
public decimal ToAmount { get; set; }
/// <summary>
/// Is only calculated when an amount was added to the request.
/// </summary>
public decimal? MonthlyAmount { get; set; }
}
}
8 changes: 7 additions & 1 deletion src/Svea.WebPay.SDK/CheckoutApi/Checkout.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Svea.WebPay.SDK.CheckoutApi.Utility;
using System;
using System.Threading.Tasks;

namespace Svea.WebPay.SDK.CheckoutApi
Expand All @@ -10,6 +11,7 @@ public class Checkout
public Checkout(SveaHttpClient sveaHttpClient)
{
_sveaHttpClient = sveaHttpClient;
Utility = new CheckoutUtility(sveaHttpClient);
}

/// <summary>
Expand Down Expand Up @@ -53,5 +55,9 @@ public async Task<Data> UpdateOrder(long orderId, UpdateOrderModel updateOrderMo
var data = await _sveaHttpClient.HttpPut<Data>(url, updateOrderModel, configureAwait);
return data;
}
/// <summary>
/// Checkout utility methods that are not related to orders.
/// </summary>
public CheckoutUtility Utility { get; }
}
}
33 changes: 33 additions & 0 deletions src/Svea.WebPay.SDK/CheckoutApi/Utility/CheckoutUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Svea.WebPay.SDK.CheckoutApi.Utility
{
public class CheckoutUtility
{
private readonly SveaHttpClient _sveaHttpClient;

public CheckoutUtility(SveaHttpClient sveaHttpClient)
{
_sveaHttpClient = sveaHttpClient;
}

/// <summary>
/// This request returns a list of available B2C/B2B part payment campaigns.
/// </summary>
/// <param name="isCompany">Required parameter indicating if it is for the company or private person</param>
/// <param name="amount">Is used to calculate a monthly amount. Setting the parameter amount will also filter out campaigns that do not meet the amount requirement.</param>
/// <param name="configureAwait"></param>
/// <returns>A list of campaigns.</returns>
/// <remarks>For example, this method can be used so "Part payment widget" can be displayed in Product pages.
/// MonthlyAmount from Response is the value used in the widget.</remarks>
public async Task<List<CampaignCodeInfo>> GetPartPaymentCampaigns(bool isCompany, long? amount = null, bool configureAwait = false)
{
var url = $"/api/util/GetAvailablePartPaymentCampaigns?isCompany={isCompany}" + (amount.HasValue ? $"&amount={amount}" : "");
var uri = new Uri(url, UriKind.Relative);
var data = await _sveaHttpClient.HttpGet<List<CampaignCodeInfo>>(uri, configureAwait);
return data;
}
}
}

0 comments on commit daebefc

Please sign in to comment.