Class DefaultPaymentDistributionService

java.lang.Object
com.broadleafcommerce.orderoperation.service.payment.DefaultPaymentDistributionService
All Implemented Interfaces:
PaymentDistributionService

public class DefaultPaymentDistributionService extends Object implements PaymentDistributionService
A service responsible for distributing payment amounts based on the payments' ratios from an Order.
Author:
Sunny Yu
  • 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 of PaymentDistributionDetails from the given Order and its PaymentSummaries.

      By default, the PaymentDistributionDetail.getRatio() is calculated by dividing each PaymentSummary.getAmount() by OrderPricing.getTotal().

      Specified by:
      buildPaymentDistributionDetails in interface PaymentDistributionService
      Parameters:
      order - the Order to build PaymentDistributionDetails for
      orderPayments - the PaymentSummaries for the Order
      Returns:
      a list of PaymentDistributionDetails from the given Order and its PaymentSummaries.
    • 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 in PaymentDistributionDto.
      Specified by:
      distributePaymentAmounts in interface PaymentDistributionService
      Parameters:
      dto - the PaymentDistributionDto 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 - the Order to get the total from
      payments - the PaymentSummaries 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 - the PaymentDistributionDetails to validate
    • validatePaymentDistributionDto

      protected void validatePaymentDistributionDto(@NonNull @NonNull PaymentDistributionDto dto)
    • 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 the ExecutablePayment.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 - the ExecutablePayment.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 rounding MonetaryAmount.

      Custom rounding scale should be added here.

      Returns:
      the scale to use for rounding MonetaryAmount
    • getRoundingMode

      protected RoundingMode getRoundingMode()
      Gets the RoundingMode to use for rounding MonetaryAmount.

      Custom rounding mode should be added here.

      Returns:
      the RoundingMode to use for rounding MonetaryAmount
    • getSplitRoundingHelper

      protected SplitRoundingHelper getSplitRoundingHelper()
    • getTypeFactory

      protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory()