Class DefaultPaymentRefundService

    • Constructor Detail

      • DefaultPaymentRefundService

        public DefaultPaymentRefundService​(PaymentPriorityStrategy paymentPriorityStrategy,
                                           OrderProvider<com.broadleafcommerce.order.client.domain.Order> orderProvider,
                                           ReturnAuthorizationProvider<com.broadleafcommerce.order.client.domain.ReturnAuthorization> returnAuthorizationProvider,
                                           PaymentProvider paymentProvider,
                                           com.broadleafcommerce.common.extension.TypeFactory typeFactory)
    • Method Detail

      • refundAndCompletePendingReturnConfirmations

        public List<TransactionExecutionResponse> refundAndCompletePendingReturnConfirmations​(@NonNull
                                                                                              @NonNull com.broadleafcommerce.order.client.domain.Order order,
                                                                                              @NonNull
                                                                                              @NonNull List<com.broadleafcommerce.order.client.domain.OrderFulfillment> orderFulfillments,
                                                                                              @NonNull
                                                                                              @NonNull com.broadleafcommerce.order.client.domain.ReturnAuthorization returnAuthorization,
                                                                                              @Nullable
                                                                                              com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Description copied from interface: PaymentRefundService
        Finds all pending ReturnConfirmations on the given ReturnAuthorization (for ReturnAuthorizationItems whose ReturnAuthorizationItem.getReturnType() supports refunds), issues refunds for them, and updates the ReturnConfirmation.getStatus() based on the refund responses.
        Specified by:
        refundAndCompletePendingReturnConfirmations in interface PaymentRefundService
        Parameters:
        order - the order for which the refunds need to be issued
        orderFulfillments - the list of all of the order's fulfillments
        returnAuthorization - the return authorization containing pending confirmations that need to be refunded and completed
        contextInfo - context information surrounding multitenant state
        Returns:
        the updated order, or null if there were no pending return confirmations on the return authorization
      • extractPendingReturnConfirmationsByFulfillmentId

        protected Map<String,​List<com.broadleafcommerce.order.client.domain.ReturnConfirmation>> extractPendingReturnConfirmationsByFulfillmentId​(com.broadleafcommerce.order.client.domain.ReturnAuthorization returnAuthorization,
                                                                                                                                                        List<com.broadleafcommerce.order.client.domain.OrderFulfillment> orderFulfillments)
        Extracts all of the returnAuthorization's pending return confirmations (for ReturnAuthorizationItems whose ReturnAuthorizationItem.getReturnType() supports refunds) and groups them by order fulfillment ID.
        Parameters:
        returnAuthorization - the return authorization whose pending return confirmations need to be extracted
        orderFulfillments - the list of all of the order's fulfillments
        Returns:
        the pending return confirmations from the returnAuthorization grouped by order fulfillment ID
      • returnTypeIsRefundable

        protected boolean returnTypeIsRefundable​(com.broadleafcommerce.order.client.domain.ReturnAuthorizationItem item)
      • sortPaymentsByPriority

        protected List<PaymentSummary> sortPaymentsByPriority​(List<PaymentSummary> paymentSummaries,
                                                              @Nullable
                                                              com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Given a list of PaymentSummaries, sorts them in order of which should be refunded first.

        Any custom logic for refund payment priority should be added here.

        Parameters:
        paymentSummaries - the list of PaymentSummaries to sort based on refund priority
        contextInfo - context information surrounding sandboxing and multitenant state
        Returns:
        a new list representing the sorted result
      • validateEntireRequestedAmountIsAvailableForRefund

        protected void validateEntireRequestedAmountIsAvailableForRefund​(Map<String,​List<com.broadleafcommerce.order.client.domain.ReturnConfirmation>> pendingReturnConfirmationsByFulfillmentId,
                                                                         List<DefaultPaymentRefundService.PaymentWithRefundableAmount> paymentWithRefundableAmounts,
                                                                         com.broadleafcommerce.order.client.domain.Order order)
        Sums the total amount that needs to be refunded for pendingReturnConfirmationsByFulfillmentId, and compares it to the sum of available refundable amounts in paymentWithRefundableAmounts.

        Will throw PaymentRefundException if the total refundable amount is insufficient.

        This validation should be performed as early as possible to prevent partial error states.

        Parameters:
        pendingReturnConfirmationsByFulfillmentId - a map from fulfillment ID to all pending return confirmations for it
        paymentWithRefundableAmounts - a list of DefaultPaymentRefundService.PaymentWithRefundableAmount containing the refundable PaymentSummary along with its amount available to refund
        order - the order for which this return is occurring
        Throws:
        PaymentRefundException - if the available refundable amount cannot cover the total to refund
      • validateEntireRequestedAmountIsAvailableForRefund

        protected void validateEntireRequestedAmountIsAvailableForRefund​(javax.money.MonetaryAmount totalToRefund,
                                                                         List<DefaultPaymentRefundService.PaymentWithRefundableAmount> paymentWithRefundableAmounts,
                                                                         com.broadleafcommerce.order.client.domain.Order order)
        Compares the total amount to refund to the sum of available refundable amounts in paymentWithRefundableAmounts.

        Will throw PaymentRefundException if the total refundable amount is insufficient.

        This validation should be performed as early as possible to prevent partial error states.

        Parameters:
        totalToRefund - the amount to refund
        paymentWithRefundableAmounts - a list of DefaultPaymentRefundService.PaymentWithRefundableAmount containing the refundable PaymentSummary along with its amount available to refund
        order - the order for which this refund is occurring
        Throws:
        PaymentRefundException - if the available refundable amount cannot cover the total to refund
      • issueRefunds

        protected List<TransactionExecutionResponse> issueRefunds​(List<? extends DefaultPaymentRefundService.PaymentRefundPackage> paymentRefundPackages,
                                                                  com.broadleafcommerce.order.client.domain.Order order,
                                                                  @Nullable
                                                                  com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Given a list of ReturnPaymentRefundPackages, executes the refunds against the paymentProvider to actually issue the refund.

        Returns the responses for each refund.

        Parameters:
        paymentRefundPackages - a list of payment refund packages whose refunds need to be executed
        order - the Order that this refund is for
        contextInfo - context information surrounding multitenant state
        Returns:
        the refund execution responses after issuing all refunds
        Throws:
        PaymentTransactionFailureException - if any of the refunds were not successfully executed
      • completePendingConfirmationsAndPersist

        @Deprecated(since="1.7.2",
                    forRemoval=true)
        protected com.broadleafcommerce.order.client.domain.ReturnAuthorization completePendingConfirmationsAndPersist​(com.broadleafcommerce.order.client.domain.ReturnAuthorization returnAuthorization,
                                                                                                                       String orderId,
                                                                                                                       @Nullable
                                                                                                                       com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Deprecated, for removal: This API element is subject to removal in a future version.
        since 1.7.2, the status of the return confirmations are updated in updateReturnAuthorization(List, ReturnAuthorization, String, ContextInfo) based on the refund transaction responses.
        Finds each of the pending ReturnConfirmations on the ReturnAuthorization, marks them as completed, and persists the return authorization.
        Parameters:
        returnAuthorization - the return authorization whose pending return confirmations should be marked completed
        orderId - the ID of the order the return authorization is for
        contextInfo - context information surrounding multitenant state
        Returns:
        the updated return authorization
      • updateReturnAuthorization

        protected com.broadleafcommerce.order.client.domain.ReturnAuthorization updateReturnAuthorization​(@NonNull
                                                                                                          @NonNull List<DefaultPaymentRefundService.ReturnPaymentRefundPackage> paymentRefundPackages,
                                                                                                          @NonNull
                                                                                                          @NonNull com.broadleafcommerce.order.client.domain.ReturnAuthorization returnAuthorization,
                                                                                                          @NonNull
                                                                                                          @NonNull String orderId,
                                                                                                          @Nullable
                                                                                                          com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Updates the given ReturnAuthorization and its ReturnConfirmations based on the refund transaction responses.
        Parameters:
        paymentRefundPackages - the PaymentRefundPackages containing all the refund transaction responses
        returnAuthorization - the ReturnAuthorization to update
        orderId - the order id
        contextInfo - context information surrounding multitenant state
        Returns:
        the updated return authorization based on refund transaction responses
      • updateReturnAuthorizationAmounts

        protected void updateReturnAuthorizationAmounts​(@NonNull
                                                        @NonNull com.broadleafcommerce.order.client.domain.ReturnAuthorization returnAuthorization,
                                                        @NonNull
                                                        @NonNull javax.money.MonetaryAmount amountRefunded,
                                                        @NonNull
                                                        @NonNull javax.money.MonetaryAmount amountFailedToRefund,
                                                        @NonNull
                                                        @NonNull javax.money.MonetaryAmount refundAmountAwaitingResult)
        Updates the amounts of the given ReturnAuthorization.
        Parameters:
        returnAuthorization - the ReturnAuthorization to update
        amountRefunded - the amount successfully refunded
        amountFailedToRefund - the amount failed to refund
        refundAmountAwaitingResult - the refund amount awaiting result
      • updateReturnConfirmation

        protected void updateReturnConfirmation​(@NonNull
                                                @NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation,
                                                @NonNull
                                                @NonNull javax.money.MonetaryAmount amountRefunded,
                                                @NonNull
                                                @NonNull javax.money.MonetaryAmount amountFailedToRefund,
                                                @NonNull
                                                @NonNull javax.money.MonetaryAmount refundAmountAwaitingResult)
        Updates the amounts and the status of the given ReturnConfirmation.
        Parameters:
        returnConfirmation - the ReturnAuthorization to update
        amountRefunded - the amount successfully refunded
        amountFailedToRefund - the amount failed to refund
        refundAmountAwaitingResult - the refund amount awaiting result
      • determineReturnConfirmationStatus

        protected String determineReturnConfirmationStatus​(@NonNull
                                                           @NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation,
                                                           @NonNull
                                                           @NonNull javax.money.MonetaryAmount amountSuccess,
                                                           @NonNull
                                                           @NonNull javax.money.MonetaryAmount amountFailed,
                                                           @NonNull
                                                           @NonNull javax.money.MonetaryAmount amountAwaitingResult)
        Determines the status of the given ReturnConfirmation based on the given amounts.

        Any custom logic should be added here.

        Parameters:
        returnConfirmation - the ReturnConfirmation to determine the status for
        amountSuccess - the amount that was successfully refunded
        amountFailed - the amount failed to refund
        amountAwaitingResult - the refund amount that is awaiting transaction result
        Returns:
        the ReturnConfirmationStatus
      • refundFulfillment

        public List<TransactionExecutionResponse> refundFulfillment​(javax.money.MonetaryAmount amountToRefund,
                                                                    com.broadleafcommerce.order.client.domain.Order order,
                                                                    com.broadleafcommerce.order.client.domain.OrderFulfillment fulfillment,
                                                                    @Nullable
                                                                    com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Description copied from interface: PaymentRefundService
        Issues refund(s) for the given amount for the OrderFulfillment.
        Specified by:
        refundFulfillment in interface PaymentRefundService
        Parameters:
        amountToRefund - the amount to refund
        order - the order for which the refund(s) need to be issued
        fulfillment - the fulfillment to refund
        contextInfo - context information surrounding multitenant state
        Returns:
        the updated order
      • getAmountOrZero

        protected <T> javax.money.MonetaryAmount getAmountOrZero​(@NonNull
                                                                 T object,
                                                                 @NonNull
                                                                 @NonNull Function<T,​javax.money.MonetaryAmount> amountGetter,
                                                                 @NonNull
                                                                 @NonNull javax.money.CurrencyUnit currency)
      • getOrderProvider

        protected OrderProvider<com.broadleafcommerce.order.client.domain.Order> getOrderProvider()
      • getReturnAuthorizationProvider

        protected ReturnAuthorizationProvider<com.broadleafcommerce.order.client.domain.ReturnAuthorization> getReturnAuthorizationProvider()
      • getTypeFactory

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