Class FinalizeCartAwaitingPaymentResultJobListener

java.lang.Object
com.broadleafcommerce.cartoperation.service.messaging.finalizecart.FinalizeCartAwaitingPaymentResultJobListener

public class FinalizeCartAwaitingPaymentResultJobListener extends Object
Scheduled Job listener that is responsible for finalizing or rolling back Carts that have the status of DefaultCartStatuses.AWAITING_PAYMENT_RESULT.

If all the cart payments have results and match the cart's total, then the cart is finalized. If there's any failed payment for the cart, meaning that cart payments have results but don't match the cart's total, the checkout is rolled back.

Author:
Sunny Yu
  • Field Details

  • Constructor Details

  • Method Details

    • listen

      @StreamListener("triggeredJobEventInputFinalizeCartAwaitingPaymentResult") public void listen(org.springframework.messaging.Message<com.broadleafcommerce.common.messaging.domain.ScheduledJobRef> message)
    • process

      protected void process(@NonNull @NonNull org.springframework.messaging.Message<com.broadleafcommerce.common.messaging.domain.ScheduledJobRef> message)
      Initiates the scheduled job to finalize cart awaiting payment result based on the provided message.
      Parameters:
      message - the Message containing the scheduled job detail
    • processCarts

      protected void processCarts(@NonNull @NonNull List<com.broadleafcommerce.cart.client.domain.Cart> carts, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo updateContext)
      Processes the given Carts.
      Parameters:
      carts - the Carts to process
      updateContext - the ContextInfo with OperationType.UPDATE
    • prepareCartsForJob

      protected List<com.broadleafcommerce.cart.client.domain.Cart> prepareCartsForJob(@NonNull @NonNull List<com.broadleafcommerce.cart.client.domain.Cart> carts, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Prepares the given Carts before executing the scheduled job. By default, it simply sets the status to DefaultCartStatuses.PAYMENT_RESULT_CHECK_IN_PROGRESS.
      Parameters:
      carts - the Carts to prepare for the scheduled job
      contextInfo - context information around sandbox and multitenant state
      Returns:
      the updated Carts
    • processCart

      protected boolean processCart(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Rolls back or finalizes checkout for the given Cart based on whether its payments failed or succeeded. If the payments for the given Cart is still awaiting result, this would return false.
      Parameters:
      cart - the Cart to roll back or finalize checkout for
      contextInfo - context information around sandbox and multitenant state
      Returns:
      Whether the cart was successfully processed
    • getBatchSize

      protected int getBatchSize(@NonNull @NonNull com.broadleafcommerce.common.messaging.domain.ScheduledJobRef jobRef)
      Gets the batch size to use when fetching payment transactions. Defaults to 10.

      We want a generally small batch size because it will take time to process finalizing carts awaiting payment result. If the batch size is too large, then the time between gathering carts & their relevant payments will be long enough to potentially make the payments & out-of-date by the time that we attempt to process them.

      Parameters:
      jobRef - the ScheduledJobRef containing the scheduled job details
      Returns:
      the batch size to use when fetching carts
    • paymentsMatchCartTotal

      protected boolean paymentsMatchCartTotal(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @NonNull @NonNull List<PaymentSummary> cartPayments, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Determines if all the payments for the given Cart add up to cart's total.

      If all the cart's payments have results but the amounts don't add up to cart's total, that means one or more payments failed, as failed transactions are excluded from the amounts in PaymentSummary.

      Parameters:
      cart - the Cart to check against
      cartPayments - the PaymentSummaries owned by the cart
      contextInfo - context information around sandbox and multitenant state
      Returns:
      true if all the payments for the given Cart match the cart's total, otherwise false
      Throws:
      UnsupportedOperationException - if the checkout transaction type is not supported
    • allPaymentsHaveResult

      protected boolean allPaymentsHaveResult(@NonNull @NonNull List<PaymentSummary> payments)
      Determines if all the payments have a result from the payment gateway rather than pending.
      Parameters:
      payments - the PaymentSummaries to check
      Returns:
      true if all the payments have a result, otherwise false
    • resetCartsToPendingPaymentIfNecessary

      protected void resetCartsToPendingPaymentIfNecessary(@NonNull @NonNull List<String> cartsToReset, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Resets the given cart ids back to DefaultCartStatuses.AWAITING_PAYMENT_RESULT.

      This is useful when the one or more payments is still pending for the cart by the time the job is executed, or exception is thrown when finalizing or rolling back the cart.

      Parameters:
      cartsToReset - cart ids that should be reset to DefaultCartStatuses.AWAITING_PAYMENT_RESULT
      contextInfo - context information around sandbox and multitenant state
    • getCheckoutTransactionType

      protected String getCheckoutTransactionType(PaymentSummary paymentSummary, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Based on the provided PaymentSummary, determine which TransactionType should be used to execute the payment stage of the checkout.
      Parameters:
      paymentSummary - One of the cart's payment summaries
      Returns:
      The desired transaction type for the gateway
    • getCheckoutTransactionType

      protected String getCheckoutTransactionType(@NonNull @NonNull String gatewayType, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • getApplicationIdFromContext

      @Nullable protected String getApplicationIdFromContext(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • getTenantIdFromContext

      @Nullable protected String getTenantIdFromContext(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • 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
    • buildUpdateContextInfoFromCart

      protected com.broadleafcommerce.data.tracking.core.context.ContextInfo buildUpdateContextInfoFromCart(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart)
      Builds a ContextInfo with OperationType.UPDATE from the given Cart.

      This is useful to make requests for Cart in the correct context, since scheduled job itself doesn't have application context. This method makes it easier to achieve that rather than creating new endpoints for each operation to ignore application context.

      Parameters:
      cart - the Cart to build the context info from
      Returns:
      a ContextInfo with OperationType.UPDATE from the given Cart
    • buildContextInfoForRead

      protected com.broadleafcommerce.data.tracking.core.context.ContextInfo buildContextInfoForRead(org.springframework.messaging.Message<com.broadleafcommerce.common.messaging.domain.ScheduledJobRef> message)
      Builds a ContextInfo object for OperationType.READ.
      Parameters:
      message - Contains scheduled job details about the context
      Returns:
      a ContextInfo object for OperationType.READ
    • buildContextInfoForUpdate

      protected com.broadleafcommerce.data.tracking.core.context.ContextInfo buildContextInfoForUpdate(@NonNull @NonNull com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfoForRead)
      Builds a ContextInfo object for OperationType.UPDATE
      Parameters:
      contextInfoForRead - the ContextInfo with OperationType.READ to build from
      Returns:
      a ContextInfo object for OperationType.UPDATE
    • getIdempotentMessageService

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

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

      protected ResourceLockProvider getResourceLockProvider()
    • getCartProvider

      protected CartProvider getCartProvider()
    • getPaymentProvider

      protected PaymentProvider<PaymentSummary> getPaymentProvider()
    • getCheckoutWorkflow

      protected CheckoutWorkflow getCheckoutWorkflow()
    • getCheckoutService

      protected CheckoutService getCheckoutService()
    • getCheckoutProperties

      protected CartOperationCheckoutProperties getCheckoutProperties()