Class ReturnConfirmationRefundWebhookListener
- java.lang.Object
-
- com.broadleafcommerce.orderoperation.service.messaging.transactionwebhook.ReturnConfirmationRefundWebhookListener
-
public class ReturnConfirmationRefundWebhookListener extends Object
Listens for refundTransactionWebhookEvent
and updates theReturnAuthorization
and theReturnConfirmation
associated with theTransactionWebhookEvent
.This listener does not update the
ReturnAuthorization
and theReturnConfirmation
based on theTransactionWebhookEvent
result, it rather reacts to the fact that a refund transaction result for theReturnConfirmation
is received, and then updates theReturnConfirmation
andReturnAuthorization
based on the latest transaction state for theReturnConfirmation
.- Author:
- Sunny Yu
-
-
Constructor Summary
Constructors Constructor Description 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 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 givenReturnConfirmation
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 theMonetaryAmounts
for each given object using the givenamountGetter 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 theOrder
associated to the givenTransactionWebhookEvent
.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 ofPaymentSummaries
for theReturnConfirmation
.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)
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 aPair
ofReturnAuthorization
and theReturnConfirmation
matching the givenreturnConfirmationId
.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 givenReturnConfirmation
andMonetaryAmounts
.protected javax.money.MonetaryAmount
getTotalShouldBeRefunded(@NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation)
Gets the total amount that should be refunded for the givenReturnConfirmation
.void
listen(org.springframework.messaging.Message<TransactionWebhookEvent> message)
protected void
process(@NonNull TransactionWebhookEvent event)
Processes the givenTransactionWebhookEvent
and updates the associatedReturnAuthorization
andReturnConfirmation
accordingly.protected void
processMessage(@NonNull org.springframework.messaging.Message<TransactionWebhookEvent> message)
protected boolean
shouldProcessEvent(@NonNull TransactionWebhookEvent event)
Determines if the givenTransactionWebhookEvent
should be processed.protected boolean
shouldProcessEventForReturnConfirmation(@NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation)
Determines if the givenReturnConfirmation
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 givenContextInfos
based on the givenContextState
.protected void
updateRefundAmounts(@NonNull com.broadleafcommerce.order.client.domain.ReturnAuthorization returnAuthorization)
Updates the refund amounts of the givenReturnAuthorization
based on itsReturnConfirmations
.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 givenReturnConfirmation
based on the givenMonetaryAmounts
.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 givenReturnConfirmation
andReturnAuthorization
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)
-
shouldProcessEvent
protected boolean shouldProcessEvent(@NonNull @NonNull TransactionWebhookEvent event)
Determines if the givenTransactionWebhookEvent
should be processed.Any custom logic should be added here.
- Parameters:
event
- theTransactionWebhookEvent
to check- Returns:
- true if the given
TransactionWebhookEvent
should be processed, otherwise false
-
process
protected void process(@NonNull @NonNull TransactionWebhookEvent event)
Processes the givenTransactionWebhookEvent
and updates the associatedReturnAuthorization
andReturnConfirmation
accordingly.- Parameters:
event
- theTransactionWebhookEvent
to process
-
getOrder
protected com.broadleafcommerce.order.client.domain.Order getOrder(@NonNull @NonNull TransactionWebhookEvent transactionWebhookEvent, @NonNull @NonNull com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Gets theOrder
associated to the givenTransactionWebhookEvent
.- Parameters:
transactionWebhookEvent
- theTransactionWebhookEvent
to get theOrder
forcontextInfo
- context information surrounding multitenant state- Returns:
- the
Order
associated to the givenTransactionWebhookEvent
-
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 givenContextInfos
based on the givenContextState
.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
- theContextState
to get the application context fromcontextInfos
- theContextInfos
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)
- Parameters:
transactionWebhookEvent
- theTransactionWebhookEvent
to get theReturnConfirmation
and theReturnAuthorization
fororderId
- theOrder.getId()
associated with theTransactionWebhookEvent
contextInfo
- context information surrounding multitenant state- Returns:
- a
Pair
ofReturnConfirmation
and theReturnAuthorization
for theTransactionWebhookEvent
-
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 aPair
ofReturnAuthorization
and theReturnConfirmation
matching the givenreturnConfirmationId
.- Parameters:
returnAuthorizations
- the list ofReturnAuthorizations
to get theReturnConfirmation
fromreturnConfirmationId
- theReturnConfirmation.getId()
to filter by- Returns:
- a
Pair
ofReturnAuthorization
and theReturnConfirmation
matching the givenreturnConfirmationId
-
shouldProcessEventForReturnConfirmation
protected boolean shouldProcessEventForReturnConfirmation(@NonNull @NonNull com.broadleafcommerce.order.client.domain.ReturnConfirmation returnConfirmation)
Determines if the givenReturnConfirmation
should be processed. This will ensure that theReturnConfirmation
will not be processed again if it's alreadyReturnConfirmationStatus.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 thePaymentSummaries
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 asReturnConfirmationStatus.COMPLETED
, and this method will ensure that the 2nd webhook is not processed.- Parameters:
returnConfirmation
- theReturnConfirmation
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 givenReturnConfirmation
andReturnAuthorization
based on the latest transaction state.- Parameters:
confirmation
- theReturnConfirmation
to updatereturnAuth
- theReturnAuthorization
to updateorder
- theOrder
associated with theReturnAuthorization
event
- theTransactionWebhookEvent
for the completed refund transactioncontextInfo
- 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 ofPaymentSummaries
for theReturnConfirmation
.- Parameters:
cartType
- the type of the cart that owns the paymentscartId
- the id of the cart that owns the paymentsreturnConfirmationId
- theReturnConfirmation.getId()
to filter the transactions forcontextInfo
- context information surrounding multitenant state- Returns:
- a list of
PaymentSummaries
for theReturnConfirmation
-
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 givenReturnConfirmation
andMonetaryAmounts
.- Parameters:
returnConfirmation
- theReturnConfirmation
to determine the amount failed to refund fortotalRefunded
- the total refunded for theReturnConfirmation
totalRefundAwaiting
- the total refund awaiting result for theReturnConfirmation
- Returns:
- the total failed to refund based on the given
ReturnConfirmation
andMonetaryAmounts
-
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 givenReturnConfirmation
.- Parameters:
returnConfirmation
- theReturnConfirmation
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 givenReturnConfirmation
based on the givenMonetaryAmounts
.- Parameters:
returnConfirmation
- theReturnConfirmation
to updatetotalRefunded
- the total amount that was successfully refundedtotalRefundAwaiting
- the total amount that is awaiting resulttotalFailedToRefund
- 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 givenReturnAuthorization
based on itsReturnConfirmations
.- Parameters:
returnAuthorization
- theReturnAuthorization
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 givenReturnConfirmation
based on the given amounts.Any custom logic should be added here.
- Parameters:
returnConfirmation
- theReturnConfirmation
to determine the status foramountSuccess
- the amount that was successfully refundedamountFailed
- the amount failed to refundamountAwaitingResult
- 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 theMonetaryAmounts
for each given object using the givenamountGetter function
.- Type Parameters:
T
- the type of the object- Parameters:
objects
- a list of objects to sumamountGetter
- theFunction
used to get theMonetaryAmounts
from the given objectscurrency
- theCurrencyUnit
- 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()
-
getPaymentProvider
protected PaymentProvider getPaymentProvider()
-
getResourceLockProvider
protected ResourceLockProvider getResourceLockProvider()
-
-