use of org.broadleafcommerce.core.web.checkout.section.CheckoutSectionDTO in project BroadleafCommerce by BroadleafCommerce.
the class OnePageCheckoutProcessor method populateSectionViewStates.
/**
* This method is responsible of populating the variables necessary to draw the checkout page.
* This logic is highly dependent on your layout. If your layout does not follow the same flow
* as the HeatClinic demo, you will need to override with your own custom layout implementation
*
* @param localVars
*/
protected void populateSectionViewStates(Map<String, Object> localVars) {
boolean orderInfoPopulated = hasPopulatedOrderInfo(CartState.getCart());
boolean billingPopulated = hasPopulatedBillingAddress(CartState.getCart());
boolean shippingPopulated = hasPopulatedShippingAddress(CartState.getCart());
localVars.put("orderInfoPopulated", orderInfoPopulated);
localVars.put("billingPopulated", billingPopulated);
localVars.put("shippingPopulated", shippingPopulated);
// Logic to show/hide sections based on state of the order
// show all sections including header unless specifically hidden
// (e.g. hide shipping if no shippable items in order or hide billing section if the order payment doesn't need
// an address i.e. PayPal Express)
boolean showBillingInfoSection = true;
boolean showShippingInfoSection = true;
boolean showAllPaymentMethods = true;
boolean showPaymentMethodSection = true;
int numShippableFulfillmentGroups = calculateNumShippableFulfillmentGroups();
if (numShippableFulfillmentGroups == 0) {
showShippingInfoSection = false;
}
boolean orderContainsThirdPartyPayment = false;
boolean orderContainsUnconfirmedCreditCard = false;
OrderPayment unconfirmedCC = null;
if (CartState.getCart().getPayments() != null) {
for (OrderPayment payment : CartState.getCart().getPayments()) {
if (payment.isActive() && PaymentType.THIRD_PARTY_ACCOUNT.equals(payment.getType())) {
orderContainsThirdPartyPayment = true;
}
if (payment.isActive() && (PaymentType.CREDIT_CARD.equals(payment.getType()) && !PaymentGatewayType.TEMPORARY.equals(payment.getGatewayType()))) {
orderContainsUnconfirmedCreditCard = true;
unconfirmedCC = payment;
}
}
}
// Toggle the Payment Info Section based on what payments were applied to the order
// (e.g. Third Party Account (i.e. PayPal Express) or Gift Cards/Customer Credit)
Money orderTotalAfterAppliedPayments = CartState.getCart().getTotalAfterAppliedPayments();
if (orderContainsThirdPartyPayment || orderContainsUnconfirmedCreditCard) {
showBillingInfoSection = false;
showAllPaymentMethods = false;
} else if (orderTotalAfterAppliedPayments != null && orderTotalAfterAppliedPayments.isZero()) {
// If all the applied payments (e.g. gift cards) cover the entire amount
// we don't need to show all payment method options.
showAllPaymentMethods = false;
}
localVars.put("showBillingInfoSection", showBillingInfoSection);
localVars.put("showAllPaymentMethods", showAllPaymentMethods);
localVars.put("showPaymentMethodSection", showPaymentMethodSection);
localVars.put("orderContainsThirdPartyPayment", orderContainsThirdPartyPayment);
localVars.put("orderContainsUnconfirmedCreditCard", orderContainsUnconfirmedCreditCard);
localVars.put("unconfirmedCC", unconfirmedCC);
// The Sections are all initialized to INACTIVE view
List<CheckoutSectionDTO> drawnSections = new LinkedList<>();
CheckoutSectionDTO orderInfoSection = new CheckoutSectionDTO(CheckoutSectionViewType.ORDER_INFO, orderInfoPopulated);
CheckoutSectionDTO billingInfoSection = new CheckoutSectionDTO(CheckoutSectionViewType.BILLING_INFO, billingPopulated);
CheckoutSectionDTO shippingInfoSection = new CheckoutSectionDTO(CheckoutSectionViewType.SHIPPING_INFO, shippingPopulated);
CheckoutSectionDTO paymentInfoSection = new CheckoutSectionDTO(CheckoutSectionViewType.PAYMENT_INFO, false);
String orderInfoHelpMessage = (String) localVars.get("orderInfoHelpMessage");
String billingInfoHelpMessage = (String) localVars.get("billingInfoHelpMessage");
String shippingInfoHelpMessage = (String) localVars.get("shippingInfoHelpMessage");
// Add the Order Info Section
drawnSections.add(orderInfoSection);
// Add the Billing Section
if (showBillingInfoSection) {
billingInfoSection.setHelpMessage(orderInfoHelpMessage);
drawnSections.add(billingInfoSection);
}
// Add the Shipping Section
if (showShippingInfoSection) {
if (showBillingInfoSection) {
shippingInfoSection.setHelpMessage(billingInfoHelpMessage);
} else {
shippingInfoSection.setHelpMessage(orderInfoHelpMessage);
}
drawnSections.add(shippingInfoSection);
}
// Add the Payment Section
if (showShippingInfoSection) {
paymentInfoSection.setHelpMessage(shippingInfoHelpMessage);
} else if (showBillingInfoSection) {
paymentInfoSection.setHelpMessage(billingInfoHelpMessage);
} else {
paymentInfoSection.setHelpMessage(orderInfoHelpMessage);
}
drawnSections.add(paymentInfoSection);
// Logic to toggle state between form view, saved view, and inactive view
// This is dependent on the layout of your checkout form. Override this if layout is different.
// initialize first view to always be a FORM view
CheckoutSectionDTO firstSection = drawnSections.get(0);
firstSection.setState(CheckoutSectionStateType.FORM);
for (ListIterator<CheckoutSectionDTO> itr = drawnSections.listIterator(); itr.hasNext(); ) {
CheckoutSectionDTO previousSection = null;
if (itr.hasPrevious()) {
previousSection = drawnSections.get(itr.previousIndex());
}
CheckoutSectionDTO section = itr.next();
// if the previous section is populated, set this section to a Form View
if (previousSection != null && previousSection.isPopulated()) {
section.setState(CheckoutSectionStateType.FORM);
}
// If this sections is populated then set this section to the Saved View
if (section.isPopulated()) {
section.setState(CheckoutSectionStateType.SAVED);
}
// {@see DefaultPaymentGatewayCheckoutService where payments are invalidated on an unsuccessful transaction}
if (CheckoutSectionViewType.PAYMENT_INFO.equals(section.getView())) {
if (showBillingInfoSection && !billingPopulated) {
section.setState(CheckoutSectionStateType.INACTIVE);
section.setHelpMessage(billingInfoHelpMessage);
}
}
// Finally, if the edit button is explicitly clicked, set the section to Form View
BroadleafRequestContext blcContext = BroadleafRequestContext.getBroadleafRequestContext();
HttpServletRequest request = blcContext.getRequest();
boolean editOrderInfo = BooleanUtils.toBoolean(request.getParameter("edit-order-info"));
boolean editBillingInfo = BooleanUtils.toBoolean(request.getParameter("edit-billing"));
boolean editShippingInfo = BooleanUtils.toBoolean(request.getParameter("edit-shipping"));
if (CheckoutSectionViewType.ORDER_INFO.equals(section.getView()) && editOrderInfo) {
section.setState(CheckoutSectionStateType.FORM);
} else if (CheckoutSectionViewType.BILLING_INFO.equals(section.getView()) && editBillingInfo) {
section.setState(CheckoutSectionStateType.FORM);
} else if (CheckoutSectionViewType.SHIPPING_INFO.equals(section.getView()) && editShippingInfo) {
section.setState(CheckoutSectionStateType.FORM);
}
}
localVars.put("checkoutSectionDTOs", drawnSections);
}
Aggregations