Class FulfillmentCaptureWebhookListener


  • public class FulfillmentCaptureWebhookListener
    extends Object
    Listens for TransactionWebhookEvent with type DefaultTransactionTypes.CAPTURE and changes the OrderFulfillment.getStatus().

    This listener does not determine the OrderFulfillment.getStatus() based on the TransactionWebhookEvent result, it rather reacts to the fact that a capture transaction result for the OrderFulfillment is received, and determines the status based on the latest transaction state for the OrderFulfillment.

    Author:
    Sunny Yu
    • Constructor Detail

      • FulfillmentCaptureWebhookListener

        public FulfillmentCaptureWebhookListener​(com.broadleafcommerce.common.messaging.service.IdempotentMessageConsumptionService idempotentMessageService,
                                                 com.broadleafcommerce.common.extension.TypeFactory typeFactory,
                                                 PaymentCaptureAttemptedProducer paymentCaptureCompletionProducer,
                                                 FulfillmentOperationService fulfillmentOperationService,
                                                 OrderProvider<com.broadleafcommerce.order.client.domain.Order> orderProvider,
                                                 FulfillmentProvider<com.broadleafcommerce.order.client.domain.OrderFulfillment> fulfillmentProvider,
                                                 PaymentProvider paymentProvider,
                                                 ResourceLockProvider resourceLockProvider)
    • Method Detail

      • listen

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

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

        protected boolean shouldProcessEventForFulfillment​(@NonNull
                                                           @NonNull com.broadleafcommerce.order.client.domain.OrderFulfillment fulfillment)
        Determines if the given OrderFulfillment should be processed. This will ensure that the OrderFulfillment will remain OrderFulfillmentStatus.PAYMENT_CAPTURE_FAILED if any of the previous captures have already failed.

        Any custom logic should be added here.

        Parameters:
        fulfillment - the OrderFulfillment to check
        Returns:
        true if the given OrderFulfillment should be processed, otherwise false
      • getTotalFailedToCapture

        protected javax.money.MonetaryAmount getTotalFailedToCapture​(@NonNull
                                                                     @NonNull com.broadleafcommerce.order.client.domain.OrderFulfillment fulfillment,
                                                                     @NonNull
                                                                     @NonNull javax.money.MonetaryAmount totalCaptured,
                                                                     @NonNull
                                                                     @NonNull javax.money.MonetaryAmount totalCaptureAwaiting)
        Determines the total failed to capture based on the given OrderFulfillment and MonetaryAmounts.
        Parameters:
        fulfillment - the OrderFulfillment to determine the amount failed to capture for
        totalCaptured - the total captured for the OrderFulfillment
        totalCaptureAwaiting - the total capture awaiting result for the OrderFulfillment
        Returns:
        the total failed to capture based on the given OrderFulfillment and MonetaryAmounts
      • changeFulfillmentStatusAndSendEvent

        protected void changeFulfillmentStatusAndSendEvent​(@NonNull
                                                           @NonNull com.broadleafcommerce.order.client.domain.OrderFulfillment fulfillment,
                                                           @NonNull
                                                           @NonNull com.broadleafcommerce.order.client.domain.Order order,
                                                           @NonNull
                                                           @NonNull PaymentCaptureAttemptResultStatus resultStatus,
                                                           @Nullable
                                                           com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Changes the OrderFulfillment.getStatus() based on the given PaymentCaptureAttemptResultStatus and send event.
        Parameters:
        fulfillment - the OrderFulfillment to change status for
        order - the Order that the OrderFulfillment belongs to
        resultStatus - the PaymentCaptureAttemptResultStatus
        contextInfo - context information around sandbox and multitenant state
      • getPaymentSummariesForOrderFulfillment

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

        protected javax.money.MonetaryAmount identifyAmountToCapture​(@NonNull
                                                                     @NonNull com.broadleafcommerce.order.client.domain.OrderFulfillment fulfillment)
        Determines the total amount that should be captured for the fulfilled OrderFulfillment.
        Parameters:
        fulfillment - The fulfillment to be captured
        Returns:
        the amount of money to capture based on the fulfillment
      • 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
      • changeFulfillmentStatus

        protected com.broadleafcommerce.order.client.domain.OrderFulfillment changeFulfillmentStatus​(@NonNull
                                                                                                     @NonNull PaymentCaptureAttemptResultStatus result,
                                                                                                     @NonNull
                                                                                                     @NonNull com.broadleafcommerce.order.client.domain.OrderFulfillment fulfillment,
                                                                                                     @Nullable
                                                                                                     com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Changes the OrderFulfillment.getStatus() based on the given PaymentCaptureAttemptResultStatus.
        Parameters:
        result - the PaymentCaptureAttemptResultStatus
        fulfillment - the OrderFulfillment to change status for
        contextInfo - context information surrounding multitenant state
        Returns:
        the OrderFulfillment with the updated status
      • sendPaymentCaptureAttemptResult

        protected void sendPaymentCaptureAttemptResult​(@NonNull
                                                       @NonNull String result,
                                                       @NonNull
                                                       @NonNull com.broadleafcommerce.order.client.domain.Order order,
                                                       @NonNull
                                                       @NonNull com.broadleafcommerce.order.client.domain.OrderFulfillment fulfillment,
                                                       @Nullable
                                                       com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Produces & sends an event indicating that the order's payments were just successfully captured so that other systems/services can react accordingly.
        Parameters:
        result - the PaymentCaptureAttemptResultStatus of attempting the capture, indicating types of success or failure
        order - The most up-to-date order with partially or fully captured payments
        fulfillment - The fulfillment which had payment captured
        contextInfo - Context information around sandbox and multitenant state.
      • getIdempotentMessageService

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

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

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

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