localVars.put("orderContainsUnconfirmedCreditCard", orderContainsUnconfirmedCreditCard);
localVars.put("unconfirmedCC", unconfirmedCC);
//The Sections are all initialized to INACTIVE view
List<CheckoutSectionDTO> drawnSections = new LinkedList<CheckoutSectionDTO>();
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);
//iterate through all the drawn sections and set their state based on the state of the other sections.
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);
}
//Custom Logic to handle a state where there may have been an error on the Payment Gateway
//and the customer is booted back to the checkout page and will have to re-enter their billing address
//and payment information as there may have been an error on either. Since, to handle all gateways with the same layout
//we are breaking the Billing Address Form from the Payment Info Form, to serve a better UX, we will have hide the payment info as
//the customer will need to re-enter their billing address to try again.
//{@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);