/*
* #%L
* BroadleafCommerce Framework Web
* %%
* Copyright (C) 2009 - 2013 Broadleaf Commerce
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.mycompany.sample.vendor.nullPaymentGateway.web.processor;
import org.broadleafcommerce.common.payment.dto.PaymentRequestDTO;
import org.broadleafcommerce.common.payment.dto.PaymentResponseDTO;
import org.broadleafcommerce.common.payment.service.PaymentGatewayHostedService;
import org.broadleafcommerce.common.vendor.service.exception.PaymentException;
import com.mycompany.sample.vendor.nullPaymentGateway.service.payment.NullPaymentGatewayConstants;
import org.springframework.stereotype.Component;
import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor;
import org.thymeleaf.standard.expression.StandardExpressionProcessor;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
/**
* <p>A Thymeleaf processor that will generate a Mock Hosted Link given a passed in PaymentRequestDTO.</p>
*
* <pre><code>
* <form blc:null_payment_hosted_action="${paymentRequestDTO}" complete_checkout="${false}" method="POST">
* <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" align="left" style="margin-right:7px;" alt="Submit Form" />
* </form>
* </code></pre>
*
* In order to use this sample processor, you will need to component scan
* the package "com.mycompany.sample".
*
* This should NOT be used in production, and is meant solely for demonstration
* purposes only.
*
* @author Elbert Bautista (elbertbautista)
*/
@Component("blNullPaymentGatewayHostedActionProcessor")
public class NullPaymentGatewayHostedActionProcessor extends AbstractAttributeModifierAttrProcessor {
@Resource(name = "blNullPaymentGatewayHostedService")
private PaymentGatewayHostedService paymentGatewayHostedService;
/**
* Sets the name of this processor to be used in Thymeleaf template
*/
public NullPaymentGatewayHostedActionProcessor() {
super("null_payment_hosted_action");
}
@Override
public int getPrecedence() {
return 10000;
}
@Override
protected Map<String, String> getModifiedAttributeValues(Arguments arguments, Element element, String attributeName) {
Map<String, String> attrs = new HashMap<String, String>();
PaymentRequestDTO requestDTO = (PaymentRequestDTO) StandardExpressionProcessor.processExpression(arguments, element.getAttributeValue(attributeName));
String url = "";
if (requestDTO != null) {
if ( element.getAttributeValue("complete_checkout") != null) {
Boolean completeCheckout = (Boolean) StandardExpressionProcessor.processExpression(arguments,
element.getAttributeValue("complete_checkout"));
element.removeAttribute("complete_checkout");
requestDTO.completeCheckoutOnCallback(completeCheckout);
}
try {
PaymentResponseDTO responseDTO = paymentGatewayHostedService.requestHostedEndpoint(requestDTO);
url = responseDTO.getResponseMap().get(NullPaymentGatewayConstants.HOSTED_REDIRECT_URL).toString();
} catch (PaymentException e) {
throw new RuntimeException("Unable to Create Null Payment Gateway Hosted Link", e);
}
}
attrs.put("action", url);
return attrs;
}
@Override
protected ModificationType getModificationType(Arguments arguments, Element element, String attributeName, String newAttributeName) {
return ModificationType.SUBSTITUTION;
}
@Override
protected boolean removeAttributeIfEmpty(Arguments arguments, Element element, String attributeName, String newAttributeName) {
return true;
}
@Override
protected boolean recomputeProcessorsAfterExecution(Arguments arguments, Element element, String attributeName) {
return false;
}
}