Class DefaultPaymentDistributionService
java.lang.Object
com.broadleafcommerce.orderoperation.service.payment.DefaultPaymentDistributionService
- All Implemented Interfaces:
PaymentDistributionService
A service responsible for distributing payment amounts based on the payments' ratios from an
Order
.- Author:
- Sunny Yu
-
Constructor Summary
ConstructorDescriptionDefaultPaymentDistributionService
(SplitRoundingHelper splitRoundingHelper, com.broadleafcommerce.common.extension.TypeFactory typeFactory) -
Method Summary
Modifier and TypeMethodDescriptionprotected com.broadleafcommerce.order.client.domain.payment.PaymentDistributionDetail
buildPaymentDistributionDetail
(@NonNull PaymentSummary paymentSummary, @NonNull Map<String, BigDecimal> distributionRatioByPaymentId) List<com.broadleafcommerce.order.client.domain.payment.PaymentDistributionDetail>
buildPaymentDistributionDetails
(@NonNull com.broadleafcommerce.order.client.domain.Order order, @NonNull List<PaymentSummary> orderPayments) distributePaymentAmounts
(@NonNull PaymentDistributionDto dto) Distributes a payment amount across different payments based on the details specified inPaymentDistributionDto
.protected javax.money.MonetaryAmount
getAmountToExecute
(@NonNull javax.money.MonetaryAmount distributedAmount, @NonNull ExecutablePayment executablePayment, @NonNull javax.money.MonetaryAmount amountRemaining) protected Map<String,
BigDecimal> getPaymentDistributionRatios
(@NonNull com.broadleafcommerce.order.client.domain.Order order, @NonNull List<PaymentSummary> payments) Gets a map of payment distribution ratios by payment ids.protected RoundingMode
Gets theRoundingMode
to use for roundingMonetaryAmount
.protected int
Gets the scale to use for roundingMonetaryAmount
.protected SplitRoundingHelper
protected com.broadleafcommerce.common.extension.TypeFactory
protected boolean
shouldUseExecutableAmount
(javax.money.MonetaryAmount executableAmount, javax.money.MonetaryAmount totalToExecute) Determines if theExecutablePayment.getExecutableAmount()
should be used instead.protected void
validatePaymentDistributionDetails
(@NonNull List<com.broadleafcommerce.order.client.domain.payment.PaymentDistributionDetail> paymentDistributionDetails) Validates the payment distribution details to ensure that the ratios add up to 100%.protected void
validatePaymentDistributionDto
(@NonNull PaymentDistributionDto dto)
-
Constructor Details
-
DefaultPaymentDistributionService
public DefaultPaymentDistributionService(SplitRoundingHelper splitRoundingHelper, com.broadleafcommerce.common.extension.TypeFactory typeFactory)
-
-
Method Details
-
buildPaymentDistributionDetails
public List<com.broadleafcommerce.order.client.domain.payment.PaymentDistributionDetail> buildPaymentDistributionDetails(@NonNull @NonNull com.broadleafcommerce.order.client.domain.Order order, @NonNull @NonNull List<PaymentSummary> orderPayments) Description copied from interface:PaymentDistributionService
Builds a list ofPaymentDistributionDetails
from the givenOrder
and itsPaymentSummaries
.By default, the
PaymentDistributionDetail.getRatio()
is calculated by dividing eachPaymentSummary.getAmount()
byOrderPricing.getTotal()
.- Specified by:
buildPaymentDistributionDetails
in interfacePaymentDistributionService
- Parameters:
order
- theOrder
to buildPaymentDistributionDetails
fororderPayments
- thePaymentSummaries
for theOrder
- Returns:
- a list of
PaymentDistributionDetails
from the givenOrder
and itsPaymentSummaries
.
-
distributePaymentAmounts
public PaymentDistributionResult distributePaymentAmounts(@NonNull @NonNull PaymentDistributionDto dto) Description copied from interface:PaymentDistributionService
Distributes a payment amount across different payments based on the details specified inPaymentDistributionDto
.- Specified by:
distributePaymentAmounts
in interfacePaymentDistributionService
- Parameters:
dto
- thePaymentDistributionDto
containing the details for the payment distributions- Returns:
- the
PaymentDistributionResult
describing the result of the distributions
-
getPaymentDistributionRatios
protected Map<String,BigDecimal> getPaymentDistributionRatios(@NonNull @NonNull com.broadleafcommerce.order.client.domain.Order order, @NonNull @NonNull List<PaymentSummary> payments) Gets a map of payment distribution ratios by payment ids.- Parameters:
order
- theOrder
to get the total frompayments
- thePaymentSummaries
for the order- Returns:
- a map of payment distribution ratios by payment ids
-
buildPaymentDistributionDetail
protected com.broadleafcommerce.order.client.domain.payment.PaymentDistributionDetail buildPaymentDistributionDetail(@NonNull @NonNull PaymentSummary paymentSummary, @NonNull @NonNull Map<String, BigDecimal> distributionRatioByPaymentId) -
validatePaymentDistributionDetails
protected void validatePaymentDistributionDetails(@NonNull @NonNull List<com.broadleafcommerce.order.client.domain.payment.PaymentDistributionDetail> paymentDistributionDetails) Validates the payment distribution details to ensure that the ratios add up to 100%.- Parameters:
paymentDistributionDetails
- thePaymentDistributionDetails
to validate
-
validatePaymentDistributionDto
-
getAmountToExecute
protected javax.money.MonetaryAmount getAmountToExecute(@NonNull @NonNull javax.money.MonetaryAmount distributedAmount, @NonNull @NonNull ExecutablePayment executablePayment, @NonNull @NonNull javax.money.MonetaryAmount amountRemaining) -
shouldUseExecutableAmount
protected boolean shouldUseExecutableAmount(javax.money.MonetaryAmount executableAmount, javax.money.MonetaryAmount totalToExecute) Determines if theExecutablePayment.getExecutableAmount()
should be used instead.The payment's executable amount should be used when the amounts difference is less than or equal to a penny. This is useful to ensure that the payment isn't left with just a penny available to execute later due to rounding.
For example, let's say the payment has a executable amount of $10.99, but the
totalToExecute
is calculated to be $10.98 due to the distribution ratio and rounding. In this case, the executable amount should be used instead.- Parameters:
executableAmount
- theExecutablePayment.getExecutableAmount()
totalToExecute
- the amount to execute- Returns:
- true if the payment's executable amount should be used instead, otherwise false
-
getRoundingScale
protected int getRoundingScale()Gets the scale to use for roundingMonetaryAmount
.Custom rounding scale should be added here.
- Returns:
- the scale to use for rounding
MonetaryAmount
-
getRoundingMode
Gets theRoundingMode
to use for roundingMonetaryAmount
.Custom rounding mode should be added here.
- Returns:
- the
RoundingMode
to use for roundingMonetaryAmount
-
getSplitRoundingHelper
-
getTypeFactory
protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory()
-