-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
PaymentAmount
to validate refund amounts
- Loading branch information
Showing
45 changed files
with
7,426 additions
and
302 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
10 changes: 10 additions & 0 deletions
10
...p.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/IMoneyDistributor.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,10 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace EasyAbp.EShop.Orders.Orders; | ||
|
||
public interface IMoneyDistributor | ||
{ | ||
Task<MoneyDistributionResult<TKey>> DistributeAsync<TKey>(string currency, Dictionary<TKey, decimal> currentAmounts, | ||
decimal totalDistributionAmount); | ||
} |
25 changes: 25 additions & 0 deletions
25
...rs/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/MoneyDistributionResult.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.Collections.Generic; | ||
using JetBrains.Annotations; | ||
using Volo.Abp; | ||
|
||
namespace EasyAbp.EShop.Orders.Orders; | ||
|
||
public class MoneyDistributionResult<TKey> | ||
{ | ||
[NotNull] | ||
public string Currency { get; } | ||
|
||
public Dictionary<TKey, decimal> AmountsAfterDistribution { get; } | ||
|
||
public Dictionary<TKey, decimal> Distributions { get; } | ||
|
||
public MoneyDistributionResult( | ||
[NotNull] string currency, | ||
Dictionary<TKey, decimal> amountsAfterDistribution, | ||
Dictionary<TKey, decimal> distributions) | ||
{ | ||
Currency = Check.NotNullOrWhiteSpace(currency, nameof(currency)); | ||
AmountsAfterDistribution = Check.NotNull(amountsAfterDistribution, nameof(amountsAfterDistribution)); | ||
Distributions = Check.NotNull(distributions, nameof(distributions)); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...op.Orders/src/EasyAbp.EShop.Orders.Domain/EasyAbp/EShop/Orders/Orders/MoneyDistributor.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,57 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using NodaMoney; | ||
using Volo.Abp.DependencyInjection; | ||
|
||
namespace EasyAbp.EShop.Orders.Orders; | ||
|
||
public class MoneyDistributor : IMoneyDistributor, ITransientDependency | ||
{ | ||
public virtual Task<MoneyDistributionResult<TKey>> DistributeAsync<TKey>(string currency, | ||
Dictionary<TKey, decimal> currentAmounts, decimal totalDistributionAmount) | ||
{ | ||
var distributions = new Dictionary<TKey, decimal>(); | ||
var originalAmountSum = currentAmounts.Sum(x => x.Value); | ||
var remainingDistributionAmount = totalDistributionAmount; | ||
|
||
foreach (var key in currentAmounts.Keys) | ||
{ | ||
var calculatedDistributionAmount = new Money( | ||
currentAmounts[key] / originalAmountSum * | ||
totalDistributionAmount, currency, MidpointRounding.ToZero); | ||
|
||
var distributionAmount = currentAmounts[key] + calculatedDistributionAmount.Amount < 0 | ||
? currentAmounts[key] | ||
: calculatedDistributionAmount.Amount; | ||
|
||
distributions[key] = distributionAmount; | ||
currentAmounts[key] += distributionAmount; | ||
remainingDistributionAmount -= distributionAmount; | ||
} | ||
|
||
foreach (var key in currentAmounts.OrderByDescending(x => x.Value).Select(x => x.Key)) | ||
{ | ||
if (remainingDistributionAmount == decimal.Zero) | ||
{ | ||
break; | ||
} | ||
|
||
var distributionAmount = currentAmounts[key] + remainingDistributionAmount < 0 | ||
? currentAmounts[key] | ||
: remainingDistributionAmount; | ||
|
||
distributions[key] += distributionAmount; | ||
currentAmounts[key] += distributionAmount; | ||
remainingDistributionAmount -= distributionAmount; | ||
} | ||
|
||
if (remainingDistributionAmount != decimal.Zero) | ||
{ | ||
throw new ApplicationException("The MoneyDistributor failed to distribute the remaining"); | ||
} | ||
|
||
return Task.FromResult(new MoneyDistributionResult<TKey>(currency, currentAmounts, distributions)); | ||
} | ||
} |
Oops, something went wrong.