Class ReturnConfirmationRefundWebhookListener


  • public class ReturnConfirmationRefundWebhookListener
    extends Object
    Listens for refund TransactionWebhookEvent and updates the ReturnAuthorization and the ReturnConfirmation associated with the TransactionWebhookEvent.

    This listener does not update the ReturnAuthorization and the ReturnConfirmation based on the TransactionWebhookEvent result, it rather reacts to the fact that a refund transaction result for the ReturnConfirmation is received, and then updates the ReturnConfirmation and ReturnAuthorization based on the latest transaction state for the ReturnConfirmation.

    Author:
    Sunny Yu
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected String determineReturnConfirmationStatus​(@NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation, @NonNull javax.money.MonetaryAmount amountSuccess, @NonNull javax.money.MonetaryAmount amountFailed, @NonNull javax.money.MonetaryAmount amountAwaitingResult)
      Determines the status of the given ReturnConfirmation based on the given amounts.
      protected <T> javax.money.MonetaryAmount getAmountTotal​(@NonNull List<T> objects, @NonNull Function<T,​javax.money.MonetaryAmount> amountGetter, @NonNull javax.money.CurrencyUnit currency)
      Helper method to sum up the MonetaryAmounts for each given object using the given amountGetter function.
      protected FulfillmentProvider<com.broadleafcommerce.order.client.domain.OrderFulfillment> getFulfillmentProvider()  
      protected com.broadleafcommerce.common.messaging.service.IdempotentMessageConsumptionService getIdempotentMessageService()  
      protected com.broadleafcommerce.order.client.domain.Order getOrder​(@NonNull TransactionWebhookEvent transactionWebhookEvent, @NonNull com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Gets the Order associated to the given TransactionWebhookEvent.
      protected OrderProvider<com.broadleafcommerce.order.client.domain.Order> getOrderProvider()  
      protected PaymentProvider getPaymentProvider()  
      protected List<PaymentSummary> getPaymentSummariesForReturnConfirmation​(@NonNull String cartType, @NonNull String cartId, @NonNull String returnConfirmationId, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Gets a list of PaymentSummaries for the ReturnConfirmation.
      protected ResourceLockProvider getResourceLockProvider()  
      protected org.apache.commons.lang3.tuple.Pair<com.broadleafcommerce.order.client.domain.ReturnAuthorization,​com.broadleafcommerce.order.client.domain.ReturnConfirmation> getReturnAuthorizationAndConfirmation​(@NonNull TransactionWebhookEvent transactionWebhookEvent, @NonNull String orderId, @NonNull com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Gets a Pair of ReturnConfirmation and the ReturnAuthorization for the TransactionWebhookEvent.
      protected org.apache.commons.lang3.tuple.Pair<com.broadleafcommerce.order.client.domain.ReturnAuthorization,​com.broadleafcommerce.order.client.domain.ReturnConfirmation> getReturnAuthorizationAndConfirmation​(@NonNull List<com.broadleafcommerce.order.client.domain.ReturnAuthorization> returnAuthorizations, @NonNull String returnConfirmationId)
      Gets a Pair of ReturnAuthorization and the ReturnConfirmation matching the given returnConfirmationId.
      protected ReturnAuthorizationProvider<com.broadleafcommerce.order.client.domain.ReturnAuthorization> getReturnAuthorizationProvider()  
      protected javax.money.MonetaryAmount getTotalFailedToRefund​(@NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation, @NonNull javax.money.MonetaryAmount totalRefunded, @NonNull javax.money.MonetaryAmount totalRefundAwaiting)
      Determines the total failed to refund based on the given ReturnConfirmation and MonetaryAmounts.
      protected javax.money.MonetaryAmount getTotalShouldBeRefunded​(@NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation)
      Gets the total amount that should be refunded for the given ReturnConfirmation.
      void listen​(org.springframework.messaging.Message<TransactionWebhookEvent> message)  
      protected void process​(@NonNull TransactionWebhookEvent event)
      Processes the given TransactionWebhookEvent and updates the associated ReturnAuthorization and ReturnConfirmation accordingly.
      protected void processMessage​(@NonNull org.springframework.messaging.Message<TransactionWebhookEvent> message)  
      protected boolean shouldProcessEvent​(@NonNull TransactionWebhookEvent event)
      Determines if the given TransactionWebhookEvent should be processed.
      protected boolean shouldProcessEventForReturnConfirmation​(@NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation)
      Determines if the given ReturnConfirmation should be processed.
      protected void updateContextInfosApplicationContext​(@NonNull com.broadleafcommerce.data.tracking.core.filtering.business.domain.ContextState contextState, @NonNull com.broadleafcommerce.data.tracking.core.context.ContextInfo... contextInfos)
      Updates the application context of the given ContextInfos based on the given ContextState.
      protected void updateRefundAmounts​(@NonNull com.broadleafcommerce.order.client.domain.ReturnAuthorization returnAuthorization)
      Updates the refund amounts of the given ReturnAuthorization based on its ReturnConfirmations.
      protected void updateRefundAmounts​(@NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation, @NonNull javax.money.MonetaryAmount totalRefunded, @NonNull javax.money.MonetaryAmount totalRefundAwaiting, @NonNull javax.money.MonetaryAmount totalFailedToRefund)
      Updates the refund amount fields for the given ReturnConfirmation based on the given MonetaryAmounts.
      protected com.broadleafcommerce.order.client.domain.ReturnAuthorization updateReturnConfirmationAndAuthorization​(@NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation confirmation, @NonNull com.broadleafcommerce.order.client.domain.ReturnAuthorization returnAuth, @NonNull com.broadleafcommerce.order.client.domain.Order order, @NonNull TransactionWebhookEvent event, @NonNull com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Updates the given ReturnConfirmation and ReturnAuthorization based on the latest transaction state.
    • Constructor Detail

      • ReturnConfirmationRefundWebhookListener

        public ReturnConfirmationRefundWebhookListener​(com.broadleafcommerce.common.messaging.service.IdempotentMessageConsumptionService idempotentMessageService,
                                                       FulfillmentProvider<com.broadleafcommerce.order.client.domain.OrderFulfillment> fulfillmentProvider,
                                                       OrderProvider<com.broadleafcommerce.order.client.domain.Order> orderProvider,
                                                       ReturnAuthorizationProvider<com.broadleafcommerce.order.client.domain.ReturnAuthorization> returnAuthorizationProvider,
                                                       PaymentProvider paymentProvider,
                                                       ResourceLockProvider resourceLockProvider)
    • Method Detail

      • listen

        @StreamListener("orderOperationsRefundTransactionWebhookInput")
        public void listen​(org.springframework.messaging.Message<TransactionWebhookEvent> message)
      • processMessage

        protected void processMessage​(@NonNull
                                      @NonNull org.springframework.messaging.Message<TransactionWebhookEvent> message)
      • getOrder

        protected com.broadleafcommerce.order.client.domain.Order getOrder​(@NonNull
                                                                           @NonNull TransactionWebhookEvent transactionWebhookEvent,
                                                                           @NonNull
                                                                           @NonNull com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Gets the Order associated to the given TransactionWebhookEvent.
        Parameters:
        transactionWebhookEvent - the TransactionWebhookEvent to get the Order for
        contextInfo - context information surrounding multitenant state
        Returns:
        the Order associated to the given TransactionWebhookEvent
      • updateContextInfosApplicationContext

        protected void updateContextInfosApplicationContext​(@NonNull
                                                            @NonNull com.broadleafcommerce.data.tracking.core.filtering.business.domain.ContextState contextState,
                                                            @NonNull
                                                            @NonNull com.broadleafcommerce.data.tracking.core.context.ContextInfo... contextInfos)
        Updates the application context of the given ContextInfos based on the given ContextState.

        This is necessary since the payment transaction webhook only has tenant context and not application context, but application context is needed for any operations against ReturnAuthorization.

        Parameters:
        contextState - the ContextState to get the application context from
        contextInfos - the ContextInfos to update
      • getReturnAuthorizationAndConfirmation

        protected org.apache.commons.lang3.tuple.Pair<com.broadleafcommerce.order.client.domain.ReturnAuthorization,​com.broadleafcommerce.order.client.domain.ReturnConfirmation> getReturnAuthorizationAndConfirmation​(@NonNull
                                                                                                                                                                                                                              @NonNull TransactionWebhookEvent transactionWebhookEvent,
                                                                                                                                                                                                                              @NonNull
                                                                                                                                                                                                                              @NonNull String orderId,
                                                                                                                                                                                                                              @NonNull
                                                                                                                                                                                                                              @NonNull com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Gets a Pair of ReturnConfirmation and the ReturnAuthorization for the TransactionWebhookEvent.
        Parameters:
        transactionWebhookEvent - the TransactionWebhookEvent to get the ReturnConfirmation and the ReturnAuthorization for
        orderId - the Order.getId() associated with the TransactionWebhookEvent
        contextInfo - context information surrounding multitenant state
        Returns:
        a Pair of ReturnConfirmation and the ReturnAuthorization for the TransactionWebhookEvent
      • getReturnAuthorizationAndConfirmation

        protected org.apache.commons.lang3.tuple.Pair<com.broadleafcommerce.order.client.domain.ReturnAuthorization,​com.broadleafcommerce.order.client.domain.ReturnConfirmation> getReturnAuthorizationAndConfirmation​(@NonNull
                                                                                                                                                                                                                              @NonNull List<com.broadleafcommerce.order.client.domain.ReturnAuthorization> returnAuthorizations,
                                                                                                                                                                                                                              @NonNull
                                                                                                                                                                                                                              @NonNull String returnConfirmationId)
        Gets a Pair of ReturnAuthorization and the ReturnConfirmation matching the given returnConfirmationId.
        Parameters:
        returnAuthorizations - the list of ReturnAuthorizations to get the ReturnConfirmation from
        returnConfirmationId - the ReturnConfirmation.getId() to filter by
        Returns:
        a Pair of ReturnAuthorization and the ReturnConfirmation matching the given returnConfirmationId
      • shouldProcessEventForReturnConfirmation

        protected boolean shouldProcessEventForReturnConfirmation​(@NonNull
                                                                  @NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation)
        Determines if the given ReturnConfirmation should be processed. This will ensure that the ReturnConfirmation will not be processed again if it's already ReturnConfirmationStatus.COMPLETED.

        For example, let's say there are two async refund transactions for the same return confirmation, and we're receiving the webhook for the 1st successful refund. While we are processing the 1st webhook and before querying the PaymentSummaries for the confirmation, the 2nd refund result is updated in PaymentTransactionServices. In this case, when we query the PaymentSummaries for the confirmation, both refunds are successful but we have not yet received the 2nd webhook. Since this listener will look at the latest transaction state for the confirmation, it will mark the confirmation as ReturnConfirmationStatus.COMPLETED, and this method will ensure that the 2nd webhook is not processed.

        Parameters:
        returnConfirmation - the ReturnConfirmation to check
        Returns:
        true if the given ReturnConfirmation should be processed, otherwise false
      • updateReturnConfirmationAndAuthorization

        protected com.broadleafcommerce.order.client.domain.ReturnAuthorization updateReturnConfirmationAndAuthorization​(@NonNull
                                                                                                                         @NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation confirmation,
                                                                                                                         @NonNull
                                                                                                                         @NonNull com.broadleafcommerce.order.client.domain.ReturnAuthorization returnAuth,
                                                                                                                         @NonNull
                                                                                                                         @NonNull com.broadleafcommerce.order.client.domain.Order order,
                                                                                                                         @NonNull
                                                                                                                         @NonNull TransactionWebhookEvent event,
                                                                                                                         @NonNull
                                                                                                                         @NonNull com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Updates the given ReturnConfirmation and ReturnAuthorization based on the latest transaction state.
        Parameters:
        confirmation - the ReturnConfirmation to update
        returnAuth - the ReturnAuthorization to update
        order - the Order associated with the ReturnAuthorization
        event - the TransactionWebhookEvent for the completed refund transaction
        contextInfo - context information surrounding multitenant state
      • getPaymentSummariesForReturnConfirmation

        protected List<PaymentSummary> getPaymentSummariesForReturnConfirmation​(@NonNull
                                                                                @NonNull String cartType,
                                                                                @NonNull
                                                                                @NonNull String cartId,
                                                                                @NonNull
                                                                                @NonNull String returnConfirmationId,
                                                                                @Nullable
                                                                                com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Gets a list of PaymentSummaries for the ReturnConfirmation.
        Parameters:
        cartType - the type of the cart that owns the payments
        cartId - the id of the cart that owns the payments
        returnConfirmationId - the ReturnConfirmation.getId() to filter the transactions for
        contextInfo - context information surrounding multitenant state
        Returns:
        a list of PaymentSummaries for the ReturnConfirmation
      • getTotalFailedToRefund

        protected javax.money.MonetaryAmount getTotalFailedToRefund​(@NonNull
                                                                    @NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation,
                                                                    @NonNull
                                                                    @NonNull javax.money.MonetaryAmount totalRefunded,
                                                                    @NonNull
                                                                    @NonNull javax.money.MonetaryAmount totalRefundAwaiting)
        Determines the total failed to refund based on the given ReturnConfirmation and MonetaryAmounts.
        Parameters:
        returnConfirmation - the ReturnConfirmation to determine the amount failed to refund for
        totalRefunded - the total refunded for the ReturnConfirmation
        totalRefundAwaiting - the total refund awaiting result for the ReturnConfirmation
        Returns:
        the total failed to refund based on the given ReturnConfirmation and MonetaryAmounts
      • getTotalShouldBeRefunded

        protected javax.money.MonetaryAmount getTotalShouldBeRefunded​(@NonNull
                                                                      @NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation)
        Gets the total amount that should be refunded for the given ReturnConfirmation.
        Parameters:
        returnConfirmation - the ReturnConfirmation to get the total should be refunded for
        Returns:
        the total amount that should be refunded for the given ReturnConfirmation
      • updateRefundAmounts

        protected void updateRefundAmounts​(@NonNull
                                           @NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation,
                                           @NonNull
                                           @NonNull javax.money.MonetaryAmount totalRefunded,
                                           @NonNull
                                           @NonNull javax.money.MonetaryAmount totalRefundAwaiting,
                                           @NonNull
                                           @NonNull javax.money.MonetaryAmount totalFailedToRefund)
        Updates the refund amount fields for the given ReturnConfirmation based on the given MonetaryAmounts.
        Parameters:
        returnConfirmation - the ReturnConfirmation to update
        totalRefunded - the total amount that was successfully refunded
        totalRefundAwaiting - the total amount that is awaiting result
        totalFailedToRefund - the total amount that failed to refund
      • updateRefundAmounts

        protected void updateRefundAmounts​(@NonNull
                                           @NonNull com.broadleafcommerce.order.client.domain.ReturnAuthorization returnAuthorization)
        Updates the refund amounts of the given ReturnAuthorization based on its ReturnConfirmations.
        Parameters:
        returnAuthorization - the ReturnAuthorization to update the refund amount fields for
      • 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
      • getAmountTotal

        protected <T> javax.money.MonetaryAmount getAmountTotal​(@NonNull
                                                                @NonNull List<T> objects,
                                                                @NonNull
                                                                @NonNull Function<T,​javax.money.MonetaryAmount> amountGetter,
                                                                @NonNull
                                                                @NonNull javax.money.CurrencyUnit currency)
        Helper method to sum up the MonetaryAmounts for each given object using the given amountGetter function.
        Type Parameters:
        T - the type of the object
        Parameters:
        objects - a list of objects to sum
        amountGetter - the Function used to get the MonetaryAmounts from the given objects
        currency - the CurrencyUnit
        Returns:
        the total amount summed from the given objects
      • getIdempotentMessageService

        protected com.broadleafcommerce.common.messaging.service.IdempotentMessageConsumptionService getIdempotentMessageService()
      • getFulfillmentProvider

        protected FulfillmentProvider<com.broadleafcommerce.order.client.domain.OrderFulfillment> getFulfillmentProvider()
      • getOrderProvider

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

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