/** Returns a string of the generated rules using data from the passed in object. */ protected String generateRules(Object objects) throws TaskException { // get filename if (parameters.get(PARAM_TEMPLATE_FILENAME.getName()) == null) { throw new TaskException( "No '" + PARAM_TEMPLATE_FILENAME.getName() + "' parameter specified."); } File templateFilename = new File(getAbsolutePath((String) parameters.get(PARAM_TEMPLATE_FILENAME.getName()))); // create a new engine (we need to set the file path) VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.setProperty( VelocityEngine.FILE_RESOURCE_LOADER_PATH, templateFilename.getParent()); try { velocityEngine.init(); } catch (Exception e) { LOG.error("Error initializing template engine."); throw new TaskException(e); } VelocityContext velocityContext = new VelocityContext(); // add parameters using reflection addParameters(velocityContext, objects); // generate rules and return StringWriter result = new StringWriter(); try { velocityEngine.mergeTemplate(templateFilename.getName(), velocityContext, result); } catch (Exception e) { LOG.error("Error generating rules."); throw new TaskException(e); } return result.toString(); }
/** * Adds parameters to the velocity context using reflection. All 'get' methods that return Objects * are added to the context, with the chars after 'get' (first char lowercase) as the key. */ private void addParameters(VelocityContext context, Object objects) throws TaskException { Class<?> objectsClass = objects.getClass(); Method methods[] = objectsClass.getMethods(); for (Method method : methods) { // If method starts with 'get', returns an Object and // takes no parameters, execute it and save result for // the velocity context. String methodName = method.getName(); if (methodName.length() > 3 && methodName.startsWith("get") && !method.getReturnType().isPrimitive() && method.getParameterTypes().length == 0) { // use the chars after the 'get' as the key String key = methodName.substring(3, 4).toLowerCase(); if (methodName.length() > 4) { key += methodName.substring(4); } try { context.put(key, method.invoke(objects)); } catch (Exception e) { LOG.error("Error invoking " + methodName + " method via reflection."); throw new TaskException(e); } } // add the passed in Object itself to the context context.put("data", objects); } }
public static boolean validate(BillingProcessConfigurationWS ws) { boolean retValue = true; //validate nextRunDate - Unique if there is already a successful run for that date //(if a process failed, it is fine to run it again) //TODO Should I Util.truncateDate before using the ws.nextRunDate? BillingProcessDTO billingProcessDTO=new BillingProcessDAS().isPresent(ws.getEntityId(), 0, ws.getNextRunDate()); if ( billingProcessDTO != null) { for (ProcessRunDTO run: billingProcessDTO.getProcessRuns()) { //if status is not failed i.e. for the same date, if the process is either running or finished if (!Constants.PROCESS_RUN_STATUS_FAILED.equals(run.getStatus().getId()) ) { LOG.error("Trying to set this configuration: " + ws + " but already has this: " + run); SessionInternalError exception = new SessionInternalError( "There is already a billing process for the give date." + ws.getNextRunDate()); String messages[] = new String[1]; messages[0] = new String("BillingProcessConfigurationWS,nextRunDate,billing.configuration.error.unique.nextrundate,"); exception.setErrorMessages(messages); throw exception; } } } ProcessRunDTO run = new ProcessRunDAS().getLatestSuccessful(ws.getEntityId()); //The nextRunDate must be greater than the latest successful one if (run != null && run.getBillingProcess().getBillingDate() != null && !run.getBillingProcess().getBillingDate().before(ws.getNextRunDate())) { LOG.error("Trying to set this configuration: " + ws + " but the it should be in the future " + run.getBillingProcess()); SessionInternalError exception = new SessionInternalError("The new next date needs to be in the future from the last successful run"); String messages[] = new String[1]; messages[0] = new String("BillingProcessConfigurationWS,nextRunDate," + "billing.configuration.error.past.nextrundate," + run.getBillingProcess().getBillingDate()); exception.setErrorMessages(messages); throw exception; } return retValue; }
public boolean preAuth(PaymentDTOEx payment) throws PluggableTaskException { try { validateParameters(); Map<String, Object> data = getChargeData(payment); PaymentAuthorizationDTO response = makeCall(data, false); PaymentAuthorizationDTO authDtoEx = new PaymentAuthorizationDTO(response); PaymentAuthorizationBL bl = new PaymentAuthorizationBL(); bl.create(authDtoEx, payment.getId()); payment.setAuthorization(authDtoEx); return false; } catch (Exception e) { log.error("error trying to pre-authorize", e); return true; } }
private void addAVSFields(Integer userId, Map<String, Object> data) { try { ContactBL contact = new ContactBL(); contact.set(userId); ContactDTO entity = contact.getEntity(); data.put("city", entity.getCity()); data.put("email", entity.getEmail()); data.put("customerAccountCode", userId.toString()); data.put("phone", entity.getPhoneNumber()); data.put("state", entity.getStateProvince()); data.put("street", entity.getAddress1() + " " + entity.getAddress2()); data.put("zipCode", entity.getPostalCode()); data.put("isOrganization", Boolean.FALSE); } catch (Exception e) { log.error("Exception when trying to add the AVS fields", e); } }
public boolean process(PaymentDTOEx paymentInfo) throws PluggableTaskException { boolean retValue = false; if (paymentInfo.getPayoutId() != null) { return true; } try { if (!new PaymentInformationBL().isCreditCard(paymentInfo.getInstrument())) { log.error("Can't process without a credit card"); throw new TaskException("Credit card not present in payment"); } if (paymentInfo.getIsRefund() == 1 && (paymentInfo.getPayment() == null || paymentInfo.getPayment().getAuthorization() == null)) { log.error("Can't process refund without a payment with an authorization record"); throw new TaskException("Refund without previous authorization"); } validateParameters(); Map<String, Object> data; if (paymentInfo.getIsRefund() == 0) { data = getChargeData(paymentInfo); } else { data = getRefundData(paymentInfo); } if ("true".equals(getOptionalParameter(PARAMETER_AVS.getName(), "false"))) { addAVSFields(paymentInfo.getUserId(), data); /* #12686 - We do not want to log credit card numbers */ Map<String, Object> dataLog = (Map<String, Object>) ((HashMap<String, Object>) data).clone(); // dataLog.remove("accountNumber"); dataLog.put("accountNumber", "******"); // TODO: check this log log.debug("returning after avs " + dataLog); } PaymentAuthorizationDTO response = makeCall(data, true); paymentInfo.setAuthorization(response); if ("1".equals(response.getCode1())) { paymentInfo.setPaymentResult(new PaymentResultDAS().find(Constants.RESULT_OK)); log.debug("result is ok"); } else { paymentInfo.setPaymentResult(new PaymentResultDAS().find(Constants.RESULT_FAIL)); log.debug("result is fail"); } PaymentAuthorizationBL bl = new PaymentAuthorizationBL(); bl.create(response, paymentInfo.getId()); } catch (MalformedURLException e) { log.error("MalformedURLException exception when calling Atlas", e); paymentInfo.setPaymentResult(new PaymentResultDAS().find(Constants.RESULT_UNAVAILABLE)); retValue = true; } catch (XmlRpcException e) { log.error("XmlRpcException exception when calling Atlas", e); paymentInfo.setPaymentResult(new PaymentResultDAS().find(Constants.RESULT_UNAVAILABLE)); retValue = false; } catch (PluggableTaskException e) { log.error("PluggableTaskException", e); throw e; } catch (Exception e) { log.error("Exception", e); throw new PluggableTaskException(e); } log.debug("returning " + retValue); return retValue; }
private PaymentAuthorizationDTO makeCall(Map<String, Object> data, boolean isCharge) throws XmlRpcException, MalformedURLException, PluggableTaskException { URL callURL = null; if ("true".equals(getOptionalParameter(PARAMETER_TEST.getName(), "false"))) { callURL = new URL(TEST_URL); log.debug("Running Atlas task in test mode!"); } else { callURL = new URL(URL); } String merchantAccountCode = ensureGetParameter(PARAMETER_MERCHANT_ACCOUNT_CODE.getName()); int merchantCode = Integer.parseInt(merchantAccountCode); merchantCode = merchantCode - (merchantCode % 1000); XmlRpcClient paymentProcessor = new XmlRpcClient(); paymentProcessor.setTransportFactory(new XmlRpcCommonsTransportFactory(paymentProcessor)); XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); config.setServerURL(callURL); config.setEnabledForExtensions(true); config.setConnectionTimeout(CONNECTION_TIME_OUT); config.setReplyTimeout(REPLY_TIME_OUT); paymentProcessor.setConfig(config); List<Map<String, Object>> transactionRequestList = new ArrayList<Map<String, Object>>(1); transactionRequestList.add(data); Map<String, Object> configParam = new HashMap<String, Object>(); if (isCharge) { configParam.put("waitConfirmation", "ignore"); } else { configParam.put("waitConfirmation", "terminate"); } Object[] params = new Object[] { String.valueOf(merchantCode), ensureGetParameter(PARAMETER_PASSWORD.getName()), transactionRequestList, configParam }; Object[] resresponse = (Object[]) paymentProcessor.execute("XMLRPCGates.processRetail", params); Map<String, Object> transactionResponseMap = (Map<String, Object>) resresponse[0]; log.debug("Got response:" + transactionResponseMap); boolean isCredit = "credit-response".equals(transactionResponseMap.get("type")); PaymentAuthorizationDTO dbRow = new PaymentAuthorizationDTO(); if (!isCredit && "A01".equals(transactionResponseMap.get("responseCode"))) { dbRow.setCode1("1"); // code if 1 it is ok } else if (isCredit && "A02".equals(transactionResponseMap.get("responseCode"))) { dbRow.setCode1("1"); } else if ('A' != ((String) transactionResponseMap.get("responseCode")).charAt(0)) { dbRow.setCode1("2"); } else { dbRow.setCode1("0"); } dbRow.setCode3((String) transactionResponseMap.get("responseCode")); dbRow.setResponseMessage((String) transactionResponseMap.get("responseMessage")); dbRow.setApprovalCode((String) transactionResponseMap.get("processorCode")); dbRow.setAvs((String) transactionResponseMap.get("avsResultCode")); dbRow.setTransactionId((String) transactionResponseMap.get("referenceNumber")); dbRow.setProcessor("Intrannuity"); return dbRow; }