Search in sources :

Example 1 with BroadleafTemplateModelModifierDTO

use of org.broadleafcommerce.presentation.model.BroadleafTemplateModelModifierDTO in project BroadleafCommerce by BroadleafCommerce.

the class TransparentRedirectCreditCardFormProcessor method getInjectedModelAndTagAttributes.

@Override
public BroadleafTemplateModelModifierDTO getInjectedModelAndTagAttributes(String rootTagName, Map<String, String> rootTagAttributes, BroadleafTemplateContext context) {
    PaymentRequestDTO requestDTO = (PaymentRequestDTO) context.parseExpression(rootTagAttributes.get("paymentRequestDTO"));
    Map<String, Map<String, String>> formParameters = new HashMap<>();
    Map<String, String> configurationSettings = new HashMap<>();
    // Create the configuration settings map to pass into the payment module
    Map<String, String> keysToKeep = new HashMap<>();
    for (String key : rootTagAttributes.keySet()) {
        if (key.startsWith("config-")) {
            final int trimLength = "config-".length();
            String configParam = key.substring(trimLength);
            configurationSettings.put(configParam, rootTagAttributes.get(key));
        } else {
            keysToKeep.put(key, rootTagAttributes.get(key));
        }
    }
    keysToKeep.remove("paymentRequestDTO");
    try {
        extensionManager.getProxy().createTransparentRedirectForm(formParameters, requestDTO, configurationSettings);
    } catch (PaymentException e) {
        throw new RuntimeException("Unable to Create the Transparent Redirect Form", e);
    }
    StringBuilder formActionKey = new StringBuilder("formActionKey");
    StringBuilder formHiddenParamsKey = new StringBuilder("formHiddenParamsKey");
    extensionManager.getProxy().setFormActionKey(formActionKey);
    extensionManager.getProxy().setFormHiddenParamsKey(formHiddenParamsKey);
    // Change the action attribute on the form to the Payment Gateways Endpoint
    String actionUrl = "";
    Map<String, String> actionValue = formParameters.get(formActionKey.toString());
    if (actionValue != null && actionValue.size() > 0) {
        String key = (String) actionValue.keySet().toArray()[0];
        actionUrl = actionValue.get(key);
    }
    keysToKeep.put("action", actionUrl);
    BroadleafTemplateModel model = context.createModel();
    // Append any hidden fields necessary for the Transparent Redirect
    Map<String, String> hiddenFields = formParameters.get(formHiddenParamsKey.toString());
    if (MapUtils.isNotEmpty(hiddenFields)) {
        for (String key : hiddenFields.keySet()) {
            Map<String, String> attributes = new HashMap<>();
            attributes.put("type", "hidden");
            attributes.put("name", key);
            attributes.put("value", hiddenFields.get(key));
            BroadleafTemplateElement input = context.createStandaloneElement("input", attributes, true);
            model.addElement(input);
        }
    }
    return new BroadleafTemplateModelModifierDTO(model, keysToKeep, "form");
}
Also used : HashMap(java.util.HashMap) BroadleafTemplateModel(org.broadleafcommerce.presentation.model.BroadleafTemplateModel) PaymentException(org.broadleafcommerce.common.vendor.service.exception.PaymentException) BroadleafTemplateElement(org.broadleafcommerce.presentation.model.BroadleafTemplateElement) BroadleafTemplateModelModifierDTO(org.broadleafcommerce.presentation.model.BroadleafTemplateModelModifierDTO) PaymentRequestDTO(org.broadleafcommerce.common.payment.dto.PaymentRequestDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with BroadleafTemplateModelModifierDTO

use of org.broadleafcommerce.presentation.model.BroadleafTemplateModelModifierDTO in project BroadleafCommerce by BroadleafCommerce.

the class FormProcessor method getInjectedModelAndTagAttributes.

@Override
public BroadleafTemplateModelModifierDTO getInjectedModelAndTagAttributes(String rootTagName, Map<String, String> rootTagAttributes, BroadleafTemplateContext context) {
    Map<String, String> formAttributes = new HashMap<>();
    formAttributes.putAll(rootTagAttributes);
    BroadleafTemplateModel model = context.createModel();
    BroadleafTemplateModelModifierDTO dto = new BroadleafTemplateModelModifierDTO();
    // We do this instead of checking for a POST because post is default if nothing is specified
    if (!"GET".equalsIgnoreCase(formAttributes.get("method"))) {
        try {
            String csrfToken = eps.getCSRFToken();
            String stateVersionToken = null;
            if (spps.isEnabled()) {
                stateVersionToken = spps.getStateVersionToken();
            }
            // detect multipart form
            if ("multipart/form-data".equalsIgnoreCase(formAttributes.get("enctype"))) {
                String csrfQueryParameter = "?" + eps.getCsrfTokenParameter() + "=" + csrfToken;
                if (stateVersionToken != null) {
                    csrfQueryParameter += "&" + spps.getStateVersionTokenParameter() + "=" + stateVersionToken;
                }
                // Add this into the attribute map to be used for the new <form> tag. The expression has already
                // been executed, don't need to treat the value as an expression
                String actionValue = formAttributes.get("action");
                actionValue += csrfQueryParameter;
                formAttributes.put("action", actionValue);
            } else {
                Map<String, String> csrfAttributes = new HashMap<>();
                csrfAttributes.put("type", "hidden");
                csrfAttributes.put("name", eps.getCsrfTokenParameter());
                csrfAttributes.put("value", csrfToken);
                BroadleafTemplateElement csrfTag = context.createStandaloneElement("input", csrfAttributes, true);
                model.addElement(csrfTag);
                if (stateVersionToken != null) {
                    Map<String, String> stateVersionAttributes = new HashMap<>();
                    stateVersionAttributes.put("type", "hidden");
                    stateVersionAttributes.put("name", spps.getStateVersionTokenParameter());
                    stateVersionAttributes.put("value", stateVersionToken);
                    BroadleafTemplateElement stateVersionTag = context.createStandaloneElement("input", stateVersionAttributes, true);
                    model.addElement(stateVersionTag);
                }
                dto.setModel(model);
            }
        } catch (ServiceException e) {
            throw new RuntimeException("Could not get a CSRF token for this session", e);
        }
    }
    dto.setFormParameters(formAttributes);
    dto.setReplacementTagName("form");
    return dto;
}
Also used : BroadleafTemplateElement(org.broadleafcommerce.presentation.model.BroadleafTemplateElement) ServiceException(org.broadleafcommerce.common.exception.ServiceException) HashMap(java.util.HashMap) BroadleafTemplateModel(org.broadleafcommerce.presentation.model.BroadleafTemplateModel) BroadleafTemplateModelModifierDTO(org.broadleafcommerce.presentation.model.BroadleafTemplateModelModifierDTO)

Aggregations

HashMap (java.util.HashMap)2 BroadleafTemplateElement (org.broadleafcommerce.presentation.model.BroadleafTemplateElement)2 BroadleafTemplateModel (org.broadleafcommerce.presentation.model.BroadleafTemplateModel)2 BroadleafTemplateModelModifierDTO (org.broadleafcommerce.presentation.model.BroadleafTemplateModelModifierDTO)2 Map (java.util.Map)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 PaymentRequestDTO (org.broadleafcommerce.common.payment.dto.PaymentRequestDTO)1 PaymentException (org.broadleafcommerce.common.vendor.service.exception.PaymentException)1