Class DefaultCheckoutService

  • All Implemented Interfaces:
    CheckoutService

    public class DefaultCheckoutService
    extends Object
    implements CheckoutService
    Default implementation of the CheckoutService that is responsible for processing checkout requests
    Author:
    Chad Harchar (charchar), Chris Kittrell (ckittrell)
    • Method Detail

      • processCheckout

        public CheckoutResponse processCheckout​(@NonNull
                                                @NonNull CheckoutProcessDto processDto,
                                                @NonNull
                                                @NonNull Map<String,​String> securityCodes,
                                                @Nullable
                                                com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Description copied from interface: CheckoutService
        Process checkout against the provided CheckoutProcessDto.
        Specified by:
        processCheckout in interface CheckoutService
        Parameters:
        processDto - The CheckoutProcessDto for processing checkout
        securityCodes - Secure codes that are passed to the gateway for verification during authorization or capture transactions
        contextInfo - Context information around sandbox and multitenant state
        Returns:
        The CheckoutResponse containing the result of the checkout process
      • validateCheckoutRequest

        @Deprecated(since="1.8.0",
                    forRemoval=true)
        protected void validateCheckoutRequest​(@NonNull
                                               @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                               @NonNull
                                               @NonNull String requestId)
        Deprecated, for removal: This API element is subject to removal in a future version.
        Validates the cart for checkout.
        Parameters:
        cart - The cart to validate
        requestId - The checkout request id
      • sendCartPendingPaymentFailedEvent

        public void sendCartPendingPaymentFailedEvent​(@NonNull
                                                      @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                      @Nullable
                                                      com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Description copied from interface: CheckoutService
        Send a message to notify external services (and internal listeners) of the cart pending payment failed, so that they can react accordingly.
        Specified by:
        sendCartPendingPaymentFailedEvent in interface CheckoutService
        Parameters:
        cart - The cart in which one or more of its pending payments failed.
        contextInfo - Context information around sandbox and multitenant state.
      • validateResendCheckoutCompletionEvent

        protected org.apache.commons.lang3.tuple.Pair<Boolean,​String> validateResendCheckoutCompletionEvent​(@NonNull
                                                                                                                  @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                                                                                  @Nullable
                                                                                                                  String requestId)
        Validates if the checkout completion event can be resent for the given Cart
        Parameters:
        cart - the Cart used to validate if the event can be resent
        requestId - the checkout request id
        Returns:
        a Pair with key of validation result and value of validation message, if any
      • validateResendCheckoutRollbackEvent

        protected org.apache.commons.lang3.tuple.Pair<Boolean,​String> validateResendCheckoutRollbackEvent​(@NonNull
                                                                                                                @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                                                                                @NonNull
                                                                                                                @NonNull String requestId)
        Validates if the checkout rollback event can be resent for the given Cart
        Parameters:
        cart - the Cart used to validate if the event can be resent
        requestId - the checkout request id
        Returns:
        a Pair with key of validation result and value of validation message, if any
      • validateResendPendingPaymentFailedEvent

        protected org.apache.commons.lang3.tuple.Pair<Boolean,​String> validateResendPendingPaymentFailedEvent​(@NonNull
                                                                                                                    @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                                                                                    @Nullable
                                                                                                                    String requestId)
        Validates if the pending payment failed event can be resent for the given Cart
        Parameters:
        cart - the Cart used to validate if the event can be resent
        requestId - the checkout request id
        Returns:
        a Pair with key of validation result and value of validation message, if any
      • sendEvent

        protected SendCheckoutEventResponse sendEvent​(@NonNull
                                                      @NonNull BiFunction<com.broadleafcommerce.cart.client.domain.Cart,​String,​org.apache.commons.lang3.tuple.Pair<Boolean,​String>> validateEventCanResend,
                                                      @NonNull
                                                      @NonNull org.apache.camel.util.function.TriConsumer<com.broadleafcommerce.cart.client.domain.Cart,​String,​com.broadleafcommerce.data.tracking.core.context.ContextInfo> sendConsumer,
                                                      String cartId,
                                                      @Nullable
                                                      String requestId,
                                                      @Nullable
                                                      com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Sends the event with the given TriConsumer, cart id, and ContextInfo.
        Parameters:
        validateEventCanResend - the BiFunction used to validate if the event can be resent for the cart
        sendConsumer - the TriConsumer used to send the event
        cartId - the cart id
        contextInfo - context information around sandbox and multitenant state
        Returns:
        a SendCheckoutEventResponse containing the details for this request
      • sendPendingPaymentFailedEventToProvider

        protected void sendPendingPaymentFailedEventToProvider​(@NonNull
                                                               @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                               @Nullable
                                                               com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Send the CartPendingPaymentFailedEvent to CartProvider for durable sending.
        Parameters:
        cart - The cart that is being evaluated for checkout.
        contextInfo - Context information around sandbox and multitenant state.
      • getLatestCheckoutRequestId

        protected String getLatestCheckoutRequestId​(@NonNull
                                                    @NonNull com.broadleafcommerce.cart.client.domain.Cart cart)
        Gets the latest checkout request id for the given Cart.
        Parameters:
        cart - the Cart to get the latest checkout request id from
        Returns:
        the latest checkout request id for the given Cart
      • validateCheckoutRequest

        protected void validateCheckoutRequest​(@NonNull
                                               @NonNull CheckoutProcessDto processDto,
                                               @Nullable
                                               com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Validates the cart for checkout
        Parameters:
        processDto - The CheckoutProcessDto to validate
        contextInfo - Context information around sandbox and multitenant state.
      • validateRequestId

        protected void validateRequestId​(@NonNull
                                         @NonNull CheckoutProcessDto processDto)
        Validates that the current requestId is not already associated to the cart
        Parameters:
        processDto - The CheckoutProcessDto containing the request id to validate
        Throws:
        CheckoutWorkflowException - Thrown if the current requestId is already present in the cart's past checkout submissions
      • prepareCartForCheckout

        protected CheckoutProcessDto prepareCartForCheckout​(@NonNull
                                                            @NonNull CheckoutProcessDto processDto,
                                                            @Nullable
                                                            com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Prepares the cart for checkout.

        Updates the Cart.getStatus() and Cart.getCheckoutSubmissions() by default.

        Parameters:
        processDto - The CheckoutProcessDto containing the cart to prepare for checkout
        contextInfo - Context information around sandbox and multitenant state
        Returns:
        the CheckoutProcessDto containing the latest cart
      • lockPayments

        @Deprecated(since="1.7.7",
                    forRemoval=true)
        protected Map<String,​String> lockPayments​(@NonNull
                                                        @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                        @Nullable
                                                        com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Deprecated, for removal: This API element is subject to removal in a future version.
        Sends request to PaymentTransactionServices to lock the cart's related payments. This is done to ensure that all of the work done within the CheckoutWorkflow and checkout process in general is consistently interacting with payments that will not change. With these locks in place, only this executing process will have access to modify the payments, until the lock TTL expires. Note: make sure that the checkout process can sufficiently be contained within the payment lock TTL. This can be configured in PaymentTransactionServices.
        Parameters:
        cart - the cart whose payments are to be locked
        contextInfo - The context of the user's request
        Returns:
        A map of payment id to lock token.
      • updateCartStatus

        protected com.broadleafcommerce.cart.client.domain.Cart updateCartStatus​(@NonNull
                                                                                 @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                                                 @NonNull
                                                                                 @NonNull String cartStatus,
                                                                                 @Nullable
                                                                                 com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      • updateCartStatus

        protected com.broadleafcommerce.cart.client.domain.Cart updateCartStatus​(@NonNull
                                                                                 @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                                                 @NonNull
                                                                                 @NonNull String cartStatus,
                                                                                 boolean updateCart,
                                                                                 @Nullable
                                                                                 com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      • removeStaleCartItems

        protected CheckoutProcessDto removeStaleCartItems​(@NonNull
                                                          @NonNull CheckoutProcessDto processDto,
                                                          @NonNull
                                                          @NonNull CheckoutException e,
                                                          @Nullable
                                                          com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Removes CartItems from the Cart that can no longer be found in the catalog & reprices the cart.
        Parameters:
        processDto - The CheckoutProcessDto that contains the cart with stale cart items
        e - The CheckoutException thrown due to the presence of stale cart items
        contextInfo - context information around sandbox and multitenant state
        Returns:
        The updated CheckoutProcessDto containing the repriced (but not updated) cart that no longer includes stale cart items
      • shouldResetCartStatus

        protected boolean shouldResetCartStatus​(@NonNull
                                                @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                @NonNull
                                                @NonNull String originalCartStatus,
                                                @NonNull
                                                @NonNull RuntimeException e)
        Determines whether or not the cart's status should be reset after encountering an unexpected exception during checkout.
        Parameters:
        cart - The cart related to the checkout request
        originalCartStatus - The status of the cart prior to attempting checkout
        e - The CheckoutWorkflowException that caused the checkout to fail
        Returns:
        whether or not the cart's status should be reset after encountering an unexpected exception during checkout.
      • finalizeCart

        @Deprecated(since="1.8.0",
                    forRemoval=true)
        protected com.broadleafcommerce.cart.client.domain.Cart finalizeCart​(@NonNull
                                                                             @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                                             @NonNull
                                                                             @NonNull Map<String,​String> paymentLockTokens,
                                                                             @Nullable
                                                                             com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Deprecated, for removal: This API element is subject to removal in a future version.
        Following a successful checkout, this method is responsible for recording the finalized state of the cart.
        Parameters:
        cart - The cart that has completed checkout, but needs to be marked as finalized
        paymentLockTokens - Tokens that grant this service access to act upon the payment. The presence of these tokens indicates that this execution flow owns the lock.
        contextInfo - context information around sandbox and multitenant state
        Returns:
        The finalized cart
      • finalizeCart

        protected CheckoutProcessDto finalizeCart​(@NonNull
                                                  @NonNull CheckoutProcessDto processDto,
                                                  @Nullable
                                                  com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Following a successful checkout, this method is responsible for recording the finalized state of the cart.
        Parameters:
        processDto - the CheckoutProcessDto containing cart that has completed checkout, but needs to be marked as finalized
        contextInfo - context information around sandbox and multitenant state
        Returns:
        The CheckoutProcessDto containing the finalized cart
      • finalizePayments

        protected void finalizePayments​(@NonNull
                                        @NonNull CheckoutProcessDto processDto,
                                        @Nullable
                                        com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      • createOrderNumber

        protected String createOrderNumber​(@NonNull
                                           @NonNull com.broadleafcommerce.cart.client.domain.Cart cart)
        Creates a new order number for a submitted cart.
        Returns:
        New order number for submitted cart.
      • sendCheckoutCompletionMessage

        protected void sendCheckoutCompletionMessage​(@NonNull
                                                     @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                     @NonNull
                                                     @NonNull String requestId,
                                                     @Nullable
                                                     com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Builds a CheckoutProcessDto and delegates to sendCheckoutCompletionMessage(CheckoutProcessDto, ContextInfo).

        Useful to send CheckoutCompletionEvent outside of the checkout context.

        Parameters:
        cart - The cart that has completed checkout. This should be the primary content of the out-going message.
        requestId - The checkout request id
        contextInfo - Context information around sandbox and multitenant state.
      • sendCheckoutCompletionMessage

        protected void sendCheckoutCompletionMessage​(@NonNull
                                                     @NonNull CheckoutProcessDto processDto,
                                                     @Nullable
                                                     com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Send a message to notify external services (and internal listeners) of the completed checkout so that they can react accordingly.
        Parameters:
        processDto - The CheckoutProcessDto containing details about the successful checkout
        contextInfo - Context information around sandbox and multitenant state.
      • addCartLinks

        protected void addCartLinks​(@NonNull
                                    @NonNull com.broadleafcommerce.cart.client.domain.CheckoutCart checkoutCart,
                                    @Nullable
                                    com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      • getFulfillmentGroupLink

        protected com.broadleafcommerce.cart.client.domain.CartLink getFulfillmentGroupLink​(@NonNull
                                                                                            @NonNull com.broadleafcommerce.cart.client.domain.CheckoutCart checkoutCart,
                                                                                            @Nullable
                                                                                            com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      • getCartItemLink

        protected com.broadleafcommerce.cart.client.domain.CartLink getCartItemLink​(@NonNull
                                                                                    @NonNull com.broadleafcommerce.cart.client.domain.CheckoutCart checkoutCart,
                                                                                    @Nullable
                                                                                    com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      • populateAdjustments

        protected void populateAdjustments​(@NonNull
                                           @NonNull com.broadleafcommerce.cart.client.domain.CheckoutCart checkoutCart)
        Populates the cart item, fulfillment item, and fulfillment group Adjustments and clear unneeded fields for the given CheckoutCart.
        Parameters:
        checkoutCart - the CheckoutCart to populate the adjustments for
      • clearAdjustmentFields

        protected void clearAdjustmentFields​(@NonNull
                                             @NonNull List<com.broadleafcommerce.order.common.domain.Adjustment> adjustments)
        Clears unneeded Adjustment fields to reduce the payload size.
        Parameters:
        adjustments - a list of Adjustments to clear the unneeded fields for.
      • buildCheckoutResponseBasedOnCartStatus

        protected CheckoutResponse buildCheckoutResponseBasedOnCartStatus​(@NonNull
                                                                          @NonNull CheckoutProcessDto processDto,
                                                                          @Nullable
                                                                          com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Builds a CheckoutResponse based on the cart status.
        Parameters:
        processDto - the CheckoutProcessDto containing the cart
        contextInfo - context information around sandbox and multitenant state
        Returns:
        a CheckoutResponse based on the cart status
      • buildFailedCheckoutResponse

        protected CheckoutResponse buildFailedCheckoutResponse​(@NonNull
                                                               @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                               @NonNull
                                                               @NonNull String requestId,
                                                               @NonNull
                                                               @NonNull CheckoutException e,
                                                               @Nullable
                                                               com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      • buildFailedCheckoutResponse

        protected CheckoutResponse buildFailedCheckoutResponse​(@NonNull
                                                               @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                               @NonNull
                                                               @NonNull String requestId,
                                                               @NonNull
                                                               @NonNull PaymentLockException e,
                                                               @Nullable
                                                               com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      • buildFailedCheckoutResponse

        protected CheckoutResponse buildFailedCheckoutResponse​(@NonNull
                                                               @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                               @NonNull
                                                               @NonNull String requestId,
                                                               @NonNull
                                                               @NonNull CartApprovalValidationException e,
                                                               @Nullable
                                                               com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      • getMessage

        protected String getMessage​(@NonNull
                                    @NonNull String errorMessage,
                                    @Nullable
                                    Object... args)
      • validateAccountCartSubmissionIfNecessary

        protected void validateAccountCartSubmissionIfNecessary​(@NonNull
                                                                @NonNull CheckoutProcessDto processDto,
                                                                @Nullable
                                                                com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        If the given Cart is an account cart, validates if the cart can be submitted by the given CustomerRef.

        By default, only customers with valid permissions can submit account carts.

        Parameters:
        processDto - the CheckoutProcessDto containing the cart and customer ref to validate
        contextInfo - context information around sandbox and multitenant state
      • finalizeAccountCartIfNecessary

        protected CheckoutProcessDto finalizeAccountCartIfNecessary​(@NonNull
                                                                    @NonNull CheckoutProcessDto processDto,
                                                                    @Nullable
                                                                    com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
        Finalizes the given account Cart if necessary.

        By default, this method just updates the approver email. Any custom logic should be added here.

        Parameters:
        processDto - the CheckoutProcessDto containing the account cart that was submitted
        contextInfo - context information around sandbox and multitenant state
        Returns:
        the latest CheckoutProcessDto
      • buildUpdateCartRequest

        protected UpdateCartRequest buildUpdateCartRequest​(@NonNull
                                                           @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                           @NonNull
                                                           @NonNull String originalCartStatus)
        Builds an UpdateCartRequest based on the given properties.
        Parameters:
        cart - the Cart to update
        originalCartStatus - the original Cart.getStatus() prior checkout submission
        Returns:
        an UpdateCartRequest based on the given properties
      • buildCartActionAuditForSubmit

        protected com.broadleafcommerce.cart.client.domain.CartActionAudit buildCartActionAuditForSubmit​(@NonNull
                                                                                                         @NonNull com.broadleafcommerce.cart.client.domain.Cart cart,
                                                                                                         @NonNull
                                                                                                         @NonNull String oldCartStatus)
        Builds a CartActionAudit for the submitted account Cart.

        By default, only approvers can submit account carts.

        Parameters:
        cart - the account Cart that was submitted
        oldCartStatus - the old Cart.getStatus()
      • getCartProvider

        protected CartProvider getCartProvider()
      • getCheckoutCompletionProducer

        protected com.broadleafcommerce.common.messaging.checkout.CheckoutCompletionProducer getCheckoutCompletionProducer()
      • getMessageSource

        protected org.springframework.context.MessageSource getMessageSource()
      • getTypeFactory

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

        @Autowired
        public void setStaleCartItemsService​(StaleCartItemsService staleCartItemsService)
      • setMessageRetryTemplate

        @Autowired
        @Qualifier("checkoutCompletionMessageRetryTemplate")
        public void setMessageRetryTemplate​(org.springframework.retry.support.RetryTemplate messageRetryTemplate)
      • getMessageRetryTemplate

        protected org.springframework.retry.support.RetryTemplate getMessageRetryTemplate()
      • setPendingPaymentFailedRetryTemplate

        @Autowired
        @Qualifier("cartPendingPaymentFailedMessageRetryTemplate")
        public void setPendingPaymentFailedRetryTemplate​(org.springframework.retry.support.RetryTemplate pendingPaymentFailedRetryTemplate)
      • getPendingPaymentFailedRetryTemplate

        protected org.springframework.retry.support.RetryTemplate getPendingPaymentFailedRetryTemplate()
      • getAuthenticationUtils

        protected com.broadleafcommerce.resource.security.utils.service.AuthenticationUtils getAuthenticationUtils()
      • setAuthenticationUtils

        @Autowired
        public void setAuthenticationUtils​(com.broadleafcommerce.resource.security.utils.service.AuthenticationUtils authenticationUtils)
      • setCartApprovalValidationService

        @Autowired
        public void setCartApprovalValidationService​(CartApprovalValidationService cartApprovalValidationService)