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");
}
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;
}
Aggregations