package com.broadleafcommerce.cartoperation.service.checkout;
import static com.broadleafcommerce.common.messaging.service.DefaultMessageLockService.MESSAGE_IDEMPOTENCY_KEY;
import org.springframework.integration.support.MessageBuilder;
...
@RequiredArgsConstructor
public class DefaultCheckoutService implements CheckoutService {
...
@Getter(AccessLevel.PROTECTED)
private final CheckoutCompletionProducer checkoutCompletionProducer; // (1)
...
/**
* Send a message to notify external services (and internal listeners) of the completed checkout
* so that they can react accordingly.
*
* @param cart The cart that has completed checkout. This should be the primary content of the
* out-going message.
* @param requestId The id representing this request to checkout
* @param contextInfo Context information around sandbox and multi-tenant state.
*/
protected void sendCheckoutCompletionMessage(@lombok.NonNull Cart cart,
@lombok.NonNull String requestId,
@Nullable ContextInfo contextInfo) {
try {
Assert.notNull(checkoutCompletionProducer,
"CheckoutCompletionProducer was not available for sending a " +
"message for checkout completion. Likely a configuration issue in " +
"which messaging for this component is disabled.");
CheckoutCompletionEvent completionEvent =
typeFactory.get(CheckoutCompletionEvent.class);
completionEvent.setCart(cart);
completionEvent.setRequestId(requestId);
completionEvent.setContextInfo(contextInfo);
checkoutCompletionProducer.checkoutCompletionOutput()
.send(MessageBuilder.withPayload(completionEvent) // (2) (3)
.setHeaderIfAbsent(MESSAGE_IDEMPOTENCY_KEY, cart.getId()) // (4)
.build());
} catch (Exception e) {
String errorMessage = String.format(
"Failed to send checkout completion message for cart (order number: %s).",
cart.getOrderNumber());
throw new CheckoutCompletionMessageException(errorMessage, e, cart);
}
}
}