Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titlePAYEEZYCRMBOPayeezyPaymentGatewaySettingBean.java
linenumberstrue
collapsetrue
package payeezy.businessobject.paymentgateway.payeezy;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import com.crm.businessobject.CRMValidatorBean;
import com.crm.businessobject.paymentgateway.CRMBOPaymentGatewayParameterBean;
import com.crm.businessobject.paymentgateway.CRMBOPaymentGatewayParameterValueBean;
import com.crm.businessobject.paymentgateway.CRMBOPaymentGatewaySettingBean;
import com.crm.businessobject.platform.CRMBOCurrencyBean;
import com.crm.dataobject.CRMDO;
import com.crm.dataobject.paymentgateway.CRMDOPaymentGatewayParameter;
import com.crm.dataobject.paymentgateway.CRMDOPaymentGatewayParameterValue;
import com.crm.dataobject.paymentgateway.CRMDOPaymentGatewaySetting;
import com.crm.dataobject.paymentgateway.PaymentGatewayCardBrand;
import com.crm.dataobject.paymentgateway.PaymentGatewayLifeCycleState;
import com.crm.dataobject.platform.CRMDOCurrency;
import com.crm.exception.DAOException;
import com.crm.exception.MandatoryFieldException;
import com.crm.framework.service.ServiceUtil;
import payeezy.dataobject.paymentgateway.payeezy.PayeezyParameter;
import payeezy.dataobject.paymentgateway.payeezy.PayeezyMode;
/**
 * Session Bean implementation class PAYEEZYCRMBOPayeezyPaymentGatewaySettingBean
 */
@Stateless
@LocalBean
public class PAYEEZYCRMBOPayeezyPaymentGatewaySettingBean extends CRMBOPaymentGatewaySettingBean {
       
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@EJB private CRMValidatorBean validatorBean;
	@EJB private CRMBOCurrencyBean currencyBean;
	@EJB private CRMBOPaymentGatewayParameterBean paymentGatewayParameterBean;
	@EJB private CRMBOPaymentGatewayParameterValueBean paymentGatewayParameterValueBean;
	
	public final static String GENERAL_PARAMETERS = "GENERAL_PARAMETERS";
	public final static String PROTOCOL = "PAYEEZY";
	/**
     * @see CRMBOPaymentGatewaySettingBean#CRMBOPaymentGatewaySettingBean()
     */
    public PAYEEZYCRMBOPayeezyPaymentGatewaySettingBean() {
        super();
    }
    /**
     * Constructs a payment gateway setting.
     * 
     * @param mainDTO - the main data object
     * @return a payment gateway setting
     * @throws Exception
     */
	@Override
	protected CRMDO constructDO(CRMDO mainDTO) throws Exception {
		
		CRMDOPaymentGatewaySetting paymentGatewaySetting = (CRMDOPaymentGatewaySetting)super.constructDO(mainDTO);
		
		paymentGatewaySetting.setPaymentGatewayProtocol(PROTOCOL);
		paymentGatewaySetting.setLifeCycleState(PaymentGatewayLifeCycleState.NOT_EFFECTIVE);
		return paymentGatewaySetting;		
	}
	
	
	/**
	 * Validates a payeezy payment gateway setting on save.
	 * 
	 * @param dto - the payment gateway setting to validate
	 * @throws Exception
	 */
	@Override
	protected void validateDOonSave(CRMDO dto) throws Exception {
		super.validateDOonSave(dto);
		CRMDOPaymentGatewaySetting paymentGatewaySetting = (CRMDOPaymentGatewaySetting)dto;			
		validateAuthenticationSettings(paymentGatewaySetting);
	}
    
	
	/**
	 * Validates the authentication settings of the payment gateway setting
     * 
     * @param paymentGatewaySetting - the payment gateway setting to validate
     * @throws Exception
     */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void validateAuthenticationSettings(CRMDOPaymentGatewaySetting paymentGatewaySetting) throws Exception {
		if(paymentGatewaySetting.getPaymentGatewayParameters()==null || !ServiceUtil.isInitialized(paymentGatewaySetting.getPaymentGatewayParameters()))
		{
			paymentGatewaySetting.setPaymentGatewayParameters(new HashSet(paymentGatewayParameterBean.load(paymentGatewaySetting)));
		}
		PayeezyMode mode = getMode(paymentGatewaySetting.getPaymentGatewayParameters());
		if(mode==null)
		{
			throw new MandatoryFieldException(getCRMSession(), PayeezyParameter.MODE.getLabel());
		}
		
		if(mode.equals(PayeezyMode.TEST))
		{
			if(!exists(PayeezyParameter.TEST_MERCHANT_TOKEN, paymentGatewaySetting.getPaymentGatewayParameters()))
			{
				throw new MandatoryFieldException(getCRMSession(), PayeezyParameter.TEST_MERCHANT_TOKEN.getLabel());
			}
			if(!exists(PayeezyParameter.TEST_API_KEY, paymentGatewaySetting.getPaymentGatewayParameters()))
			{
				throw new MandatoryFieldException(getCRMSession(), PayeezyParameter.TEST_API_KEY.getLabel());
			}
			if(!exists(PayeezyParameter.TEST_API_SECRET, paymentGatewaySetting.getPaymentGatewayParameters()))
			{
				throw new MandatoryFieldException(getCRMSession(), PayeezyParameter.TEST_API_SECRET.getLabel());
			}
			if(!exists(PayeezyParameter.TEST_URL, paymentGatewaySetting.getPaymentGatewayParameters()))
			{
				throw new MandatoryFieldException(getCRMSession(), PayeezyParameter.TEST_URL.getLabel());
			}
		}
		else if(mode.equals(PayeezyMode.LIVE))
		{
			if(!exists(PayeezyParameter.LIVE_MERCHANT_TOKEN, paymentGatewaySetting.getPaymentGatewayParameters()))
			{
				throw new MandatoryFieldException(getCRMSession(), PayeezyParameter.LIVE_MERCHANT_TOKEN.getLabel());
			}
			if(!exists(PayeezyParameter.LIVE_API_KEY, paymentGatewaySetting.getPaymentGatewayParameters()))
			{
				throw new MandatoryFieldException(getCRMSession(), PayeezyParameter.LIVE_API_KEY.getLabel());
			}
			if(!exists(PayeezyParameter.LIVE_API_SECRET, paymentGatewaySetting.getPaymentGatewayParameters()))
			{
				throw new MandatoryFieldException(getCRMSession(), PayeezyParameter.LIVE_API_SECRET.getLabel());
			}
			if(!exists(PayeezyParameter.LIVE_URL, paymentGatewaySetting.getPaymentGatewayParameters()))
			{
				throw new MandatoryFieldException(getCRMSession(), PayeezyParameter.LIVE_URL.getLabel());
			}
		}
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private PayeezyMode getMode(Set<CRMDOPaymentGatewayParameter> parameters) throws DAOException, Exception{
		PayeezyMode mode = null;
		if(parameters!=null)
		{
			Iterator<CRMDOPaymentGatewayParameter> iter = parameters.iterator();
			while(iter.hasNext())
			{
				CRMDOPaymentGatewayParameter parameter = iter.next();
				if(parameter!=null &&
					
						parameter.getName()!=null &&
						parameter.getName().equals(PayeezyParameter.MODE.toString()))
				{
					Set<CRMDOPaymentGatewayParameterValue> values = parameter.getParameterValues();
					
					if(values==null || !ServiceUtil.isInitialized(values))
					{
						values = new HashSet(paymentGatewayParameterValueBean.load(parameter));
					}
					
					if(values!=null)
					{
						Iterator<CRMDOPaymentGatewayParameterValue> valueIter = values.iterator();
						while(valueIter.hasNext())
						{
							CRMDOPaymentGatewayParameterValue value = valueIter.next();
							if(value!=null)
							{
								mode = PayeezyMode.valueOf(PayeezyMode.class,value.getStringValue());
								break;
							}
						}
					}
				}
				
				if(mode!=null)
				{
					break;
				}
			}
		}
		
		
		return mode;
	}
	
	private boolean exists(PayeezyParameter PayeezyParameter, Set<CRMDOPaymentGatewayParameter> parameters){
		boolean exists = false;
		
		if(parameters!=null)
		{
			for(CRMDOPaymentGatewayParameter parameter:parameters)
			{
				if(parameter!=null &&
						parameter.getName()!=null &&
						parameter.getName().equals(PayeezyParameter.toString()) &&
						parameter.getParameterValues()!=null &&
						parameter.getParameterValues().size()>0)
				{
					
					Iterator<CRMDOPaymentGatewayParameterValue> iter = parameter.getParameterValues().iterator();
					while(iter.hasNext())
					{
						CRMDOPaymentGatewayParameterValue value = iter.next();
						if(value.getStringValue()!=null)
						{
							exists  = true;
							break;
						}
					}
					
					if(exists)
					{
						break;
					}
				}
			}
		}
		
		return exists;
	}
	
	
	/**
	 * Constructs an XML file from a payment gateway setting.
	 * 
	 * @param paymentGatewaySetting - the payment gateway setting to get the values from
	 * @return the payment gateway setting
	 * @throws Exception
	 */
	@Override
	public CRMDOPaymentGatewaySetting setXMLFromObjects(CRMDOPaymentGatewaySetting paymentGatewaySetting) throws Exception {
			
		return paymentGatewaySetting;
	}
	
	/**
	 * Sets the values of a payment gateway setting from an XML file.
	 * 
	 * @param paymentGatewaySetting - the payment gateway setting to set the values for
	 * @return the updated payment gateway setting
	 * @throws Exception
	 */
	@Override
	public CRMDOPaymentGatewaySetting setObjectsFromXML(CRMDOPaymentGatewaySetting paymentGatewaySetting) throws Exception {
		
		return paymentGatewaySetting;
	}
	
	/**
     * Loads the payeezy payment gateway setting.
     * 
     * @return a payeezy payment gateway setting
     * @throws Exception
     */
	public CRMDOPaymentGatewaySetting load() throws Exception {
		return loadByProtocol(PROTOCOL);
	}
	
	/**
     * Loads the effective payeezy payment gateway setting.
     * 
     * @return the effective payeezy payment gateway setting
     * @throws Exception
     */
	public CRMDOPaymentGatewaySetting loadEffective() throws Exception {
		return loadEffective(PROTOCOL);
	}
	
	/**
     * Loads the effective payeezy payment gateway setting.
     * 
	 * @param applicationServerFiltering - if true, result will be filtered based on allowed application servers
     * @return the effective payeezy payment gateway setting
     * @throws Exception
     */
	public CRMDOPaymentGatewaySetting loadEffective(Boolean applicationServerFiltering) throws Exception {
		return loadEffective(PROTOCOL, applicationServerFiltering);
	}
	
	/**
     * Returns the mandatory fields of a payeezy payment gateway setting.
     * 
     * @param dto - the payeezy payment gateway setting to return the mandatory fields for
     * @return the mandatory fields of the payeezy payment gateway setting
     * @throws Exception
     */
	@Override
	protected LinkedHashMap<String, String> getMandatoryFields(CRMDO dto) throws Exception {
		LinkedHashMap<String, String> mandatoryFields = new LinkedHashMap<String,String>();
		
		mandatoryFields.put("key_name", "name");
		mandatoryFields.put("key_alternative_code", "altCode");
		mandatoryFields.put("key_life_cycle_state", "lifeCycleState");
		mandatoryFields.put("key_payment_gateway", "paymentGatewayProtocol");
	
		return mandatoryFields;
	}
	
	
	/**
	 * Validates the uniqueness of a payment gateway setting.
	 * 
	 * @param paymentGatewaySetting - the payment gateway setting to validate
	 * @throws Exception
	 */
	@Override
	protected void validateUniqueness(CRMDOPaymentGatewaySetting paymentGatewaySetting) throws Exception {
				
		String[] fieldNames = new String[] {"name", "altCode", "paymentGatewayProtocol"};
		String[] msgKeys = new String[] {"Name", "Alternative Code", "Protocol"};
		
		validatorBean.validateUniqueRecordAgainstDb(
				paymentGatewaySetting, 
				fieldNames, 
				msgKeys, 
				"Payeezy Payment Gateway Setting", 
				true, 
				null);
	}
	@Override
	public CRMDOPaymentGatewaySetting initializeParameters(CRMDOPaymentGatewaySetting paymentGatewaySetting) throws Exception
	{
		return paymentGatewaySetting;
	}
	
	/**
	 * Get the currencies which are supported by payeezy
	 * 
	 * @return a list of all the currencies which are supported by payeezy  
	 * @throws Exception
	 */
	public ArrayList<CRMDO> getSupportedCurrencies(CRMDOPaymentGatewaySetting paymentGatewaySetting) throws Exception {
		
		ArrayList<CRMDO> supportedCurrencies = currencyBean.loadAltenativeCurrencies();
		CRMDOCurrency defaultCurrency = getCRMSession().getGeneralSettings().getDefaultCurrency();
					
		if (supportedCurrencies!=null && supportedCurrencies.size()>0)
		{
			Boolean defaultFound=false;
			for (int i=0; i<supportedCurrencies.size(); i++)
			{
				CRMDOCurrency currency = (CRMDOCurrency) supportedCurrencies.get(i);
				if(currency.equals(defaultCurrency))
				{
					defaultFound=true;
					break;
				}
			}
			
			if(!defaultFound)
			{
				supportedCurrencies.add(defaultCurrency);			
			}
		}
				
		return supportedCurrencies;
	}
	
	/**
	 * Returns the card brand based on the payeezy card type
	 * @param cardType - the given payeezy card type
	 * @return the card brand
	 */
	public String getCardBrand(String cardType){
		String brand = null;
		if(cardType!=null)
		{
			if(cardType.equalsIgnoreCase("American Express"))
			{
				brand = PaymentGatewayCardBrand.AMERICAN_EXPRESS.toString();
			}
			else if(cardType.equalsIgnoreCase("Discover"))
			{
				brand = PaymentGatewayCardBrand.DISCOVER.toString();
			}
			else if(cardType.equalsIgnoreCase("MasterCard"))
			{
				brand = PaymentGatewayCardBrand.MASTER_CARD.toString();
			}
			else if(cardType.equalsIgnoreCase("Visa"))
			{
				brand = PaymentGatewayCardBrand.VISA.toString();
			}
			else
			{
				brand = PaymentGatewayCardBrand.UNKNOWN.toString();
			}
		}
		
		return brand;
	}
	
	/**
	 * Returns the card type based on the card brand
	 * @param cardType - the given card brand
	 * @return the card brand
	 */
	public String getCardType(String brand){
		String cardType = null;
		if(brand!=null)
		{
			if(brand.equals(PaymentGatewayCardBrand.AMERICAN_EXPRESS.toString()))
			{
				cardType ="American Express";
			}
			else if(brand.equals(PaymentGatewayCardBrand.DISCOVER.toString()))
			{
				cardType = "Discover";
			}
			else if(brand.equals(PaymentGatewayCardBrand.MASTER_CARD.toString()))
			{
				cardType = "MasterCard";
			}
			else if(brand.equals(PaymentGatewayCardBrand.VISA.toString()))
			{
				cardType = "Visa";
			}
		}
		
		return cardType;
	}
}

4.

...

Payment Gateway User Interface Class

The payment gateway user interface class should extend com.crm.process.paymentgateway.CRMUIPaymentGatewaySettingBean.

...