From 71ace436f8dcfec5c536ad0f9f788504f3ade019 Mon Sep 17 00:00:00 2001 From: milindalaknath Date: Fri, 26 Oct 2018 11:10:09 +0530 Subject: [PATCH 1/2] [HUBDEV-2159] fixed the bug related when request send without chargingMetaData. Success message returns. --- .../impl/payment/ValidatePaymentCharge.java | 62 ++++++++++--------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/components/oneapi-validation/src/main/java/com/wso2telco/dep/oneapivalidation/service/impl/payment/ValidatePaymentCharge.java b/components/oneapi-validation/src/main/java/com/wso2telco/dep/oneapivalidation/service/impl/payment/ValidatePaymentCharge.java index 360c0af9f..8b0487c6a 100644 --- a/components/oneapi-validation/src/main/java/com/wso2telco/dep/oneapivalidation/service/impl/payment/ValidatePaymentCharge.java +++ b/components/oneapi-validation/src/main/java/com/wso2telco/dep/oneapivalidation/service/impl/payment/ValidatePaymentCharge.java @@ -1,12 +1,12 @@ /******************************************************************************* * Copyright (c) 2015-2016, WSO2.Telco Inc. (http://www.wso2telco.com) All Rights Reserved. - * + * * WSO2.Telco Inc. licences this file to you 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. @@ -22,23 +22,27 @@ import com.wso2telco.dep.oneapivalidation.util.Validation; import com.wso2telco.dep.oneapivalidation.util.ValidationRule; import org.json.JSONObject; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + - // TODO: Auto-generated Javadoc /** * The Class ValidatePaymentCharge. */ public class ValidatePaymentCharge implements IServiceValidate { - + + private Log log = LogFactory.getLog(ValidatePaymentCharge.class); + /** The validation rules. */ private final String[] validationRules = {"*", "transactions", "amount"}; - + /* (non-Javadoc) * @see com.wso2telco.oneapivalidation.service.IServiceValidate#validate(java.lang.String) */ public void validate(String json) throws CustomException { //throw new UnsupportedOperationException("Not supported yet."); - + String endUserId = null; String transactionOperationStatus = null; String originalServerReferenceCode = null; @@ -91,32 +95,34 @@ public void validate(String json) throws CustomException { description = nullOrTrimmed(chargingInfo.getString("description")); } - JSONObject chargingMetaData = (JSONObject) payAmount.get("chargingMetaData"); - - if (!chargingMetaData.isNull("onBehalfOf")) { - onBehalfOf = nullOrTrimmed(chargingMetaData.getString("onBehalfOf")); - } - if (!chargingMetaData.isNull("purchaseCategoryCode")) { - purchaseCategoryCode = nullOrTrimmed(chargingMetaData.getString("purchaseCategoryCode")); - } - if (!chargingMetaData.isNull("channel")) { - channel = nullOrTrimmed(chargingMetaData.getString("channel")); - } - if (!chargingMetaData.isNull("taxAmount")) { - if(!chargingMetaData.get("taxAmount").toString().matches(doubleValidationRegex)){ - throw new CustomException("SVC0002", "Invalid input value for message part %1", - new String[]{"taxAmount should be a whole or two digit decimal positive number"}); + if(payAmount.has("chargingMetaData")) { + JSONObject chargingMetaData = (JSONObject) payAmount.get("chargingMetaData"); + + if (!chargingMetaData.isNull("onBehalfOf")) { + onBehalfOf = nullOrTrimmed(chargingMetaData.getString("onBehalfOf")); + } + if (!chargingMetaData.isNull("purchaseCategoryCode")) { + purchaseCategoryCode = nullOrTrimmed(chargingMetaData.getString("purchaseCategoryCode")); + } + if (!chargingMetaData.isNull("channel")) { + channel = nullOrTrimmed(chargingMetaData.getString("channel")); } - taxAmount = Double.parseDouble(nullOrTrimmed(String.valueOf(chargingMetaData.get("taxAmount")))); + if (!chargingMetaData.isNull("taxAmount")) { + if (!chargingMetaData.get("taxAmount").toString().matches(doubleValidationRegex)) { + throw new CustomException("SVC0002", "Invalid input value for message part %1", + new String[]{"taxAmount should be a whole or two digit decimal positive number"}); + } + taxAmount = Double.parseDouble(nullOrTrimmed(String.valueOf(chargingMetaData.get("taxAmount")))); + } } - System.out.println("Manipulated recived JSON Object: " + json); + log.info("Manipulated received JSON Object: " + json); } catch (CustomException e){ throw new CustomException(e.getErrcode(), e.getErrmsg(), e.getErrvar()); } catch (Exception e) { - System.out.println("Manipulating recived JSON Object: " + e); + log.error("Manipulating received JSON Object: " + e); throw new CustomException("POL0299", "Unexpected Error", new String[]{""}); } @@ -135,11 +141,11 @@ public void validate(String json) throws CustomException { new ValidationRule(ValidationRule.VALIDATION_TYPE_OPTIONAL, "purchaseCategoryCode", purchaseCategoryCode), new ValidationRule(ValidationRule.VALIDATION_TYPE_OPTIONAL_PAYMENT_CHANNEL, "channel", channel), new ValidationRule(ValidationRule.VALIDATION_TYPE_OPTIONAL_DOUBLE_GE_ZERO, "taxAmount", taxAmount),}; - + Validation.checkRequestParams(rules); - + } - + /** * Null or trimmed. * From 45248a41c8f3bd8d9015833fdf21c4c3d6f6c424 Mon Sep 17 00:00:00 2001 From: milindalaknath Date: Mon, 29 Oct 2018 17:00:59 +0530 Subject: [PATCH 2/2] [HUBDEV-2161] Used JSONException and error message changed. --- .../impl/payment/ValidatePaymentCharge.java | 9 ++- .../service/impl/payment/ValidateRefund.java | 64 +++++++++++-------- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/components/oneapi-validation/src/main/java/com/wso2telco/dep/oneapivalidation/service/impl/payment/ValidatePaymentCharge.java b/components/oneapi-validation/src/main/java/com/wso2telco/dep/oneapivalidation/service/impl/payment/ValidatePaymentCharge.java index 8b0487c6a..35f02f6f0 100644 --- a/components/oneapi-validation/src/main/java/com/wso2telco/dep/oneapivalidation/service/impl/payment/ValidatePaymentCharge.java +++ b/components/oneapi-validation/src/main/java/com/wso2telco/dep/oneapivalidation/service/impl/payment/ValidatePaymentCharge.java @@ -21,6 +21,7 @@ import com.wso2telco.dep.oneapivalidation.util.UrlValidator; import com.wso2telco.dep.oneapivalidation.util.Validation; import com.wso2telco.dep.oneapivalidation.util.ValidationRule; +import org.json.JSONException; import org.json.JSONObject; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -117,9 +118,15 @@ public void validate(String json) throws CustomException { } } - log.info("Manipulated received JSON Object: " + json); + if (log.isDebugEnabled()){ + log.debug("Manipulated received JSON Object: " + json); + } + } catch (JSONException e) { + log.error("Manipulating received JSON Object: " + e); + throw new CustomException("SVC0001", "", new String[]{"Incorrect JSON Object received"}); } catch (CustomException e){ + log.error("Manipulating received JSON Object: " + e); throw new CustomException(e.getErrcode(), e.getErrmsg(), e.getErrvar()); } catch (Exception e) { log.error("Manipulating received JSON Object: " + e); diff --git a/components/oneapi-validation/src/main/java/com/wso2telco/dep/oneapivalidation/service/impl/payment/ValidateRefund.java b/components/oneapi-validation/src/main/java/com/wso2telco/dep/oneapivalidation/service/impl/payment/ValidateRefund.java index fc6a19e62..71dfecb0a 100644 --- a/components/oneapi-validation/src/main/java/com/wso2telco/dep/oneapivalidation/service/impl/payment/ValidateRefund.java +++ b/components/oneapi-validation/src/main/java/com/wso2telco/dep/oneapivalidation/service/impl/payment/ValidateRefund.java @@ -16,6 +16,9 @@ package com.wso2telco.dep.oneapivalidation.service.impl.payment; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONException; import org.json.JSONObject; import com.wso2telco.dep.oneapivalidation.exceptions.CustomException; @@ -31,6 +34,8 @@ */ public class ValidateRefund implements IServiceValidate { + private Log log = LogFactory.getLog(ValidateRefund.class); + /** The validation rules. */ private final String[] validationRules = {"*", "transactions", "amount"}; @@ -109,37 +114,46 @@ public void validate(String json) throws CustomException { code = nullOrTrimmed(objChargingInformation.get("code").toString()); } - JSONObject objChargingMetaData = (JSONObject) objPaymentAmount.get("chargingMetaData"); + if (objPaymentAmount.has("chargingMetaData")) { + JSONObject objChargingMetaData = (JSONObject) objPaymentAmount.get("chargingMetaData"); - if (!objChargingMetaData.isNull("onBehalfOf") ) { - onBehalfOf = nullOrTrimmed(objChargingMetaData.get("onBehalfOf").toString()); - } - if (!objChargingMetaData.isNull("purchaseCategoryCode")) { - purchaseCategoryCode = nullOrTrimmed(objChargingMetaData.get("purchaseCategoryCode").toString()); - } - if (!objChargingMetaData.isNull("channel") ) { - channel = nullOrTrimmed(objChargingMetaData.get("channel").toString()); - } - if (!objChargingMetaData.isNull("taxAmount")) { - if(!objChargingMetaData.get("taxAmount").toString().matches(doubleValidationRegex)){ - throw new CustomException("SVC0002", "Invalid input value for message part %1", - new String[]{"taxAmount should be a whole or two digit decimal positive number"}); + if (!objChargingMetaData.isNull("onBehalfOf")) { + onBehalfOf = nullOrTrimmed(objChargingMetaData.get("onBehalfOf").toString()); + } + if (!objChargingMetaData.isNull("purchaseCategoryCode")) { + purchaseCategoryCode = nullOrTrimmed(objChargingMetaData.get("purchaseCategoryCode").toString()); } - taxAmount = Double.parseDouble(nullOrTrimmed(String.valueOf(objChargingMetaData.get("taxAmount")))); + if (!objChargingMetaData.isNull("channel")) { + channel = nullOrTrimmed(objChargingMetaData.get("channel").toString()); + } + if (!objChargingMetaData.isNull("taxAmount")) { + if (!objChargingMetaData.get("taxAmount").toString().matches(doubleValidationRegex)) { + throw new CustomException("SVC0002", "Invalid input value for message part %1", + new String[]{"taxAmount should be a whole or two digit decimal positive number"}); + } + taxAmount = Double.parseDouble(nullOrTrimmed(String.valueOf(objChargingMetaData.get("taxAmount")))); + } + if (!objChargingMetaData.isNull("mandateId")) { + mandateId = nullOrTrimmed(objChargingMetaData.get("mandateId").toString()); + } + if (!objChargingMetaData.isNull("productId")) { + productId = nullOrTrimmed(objChargingMetaData.get("productId").toString()); + } + if (!objChargingMetaData.isNull("serviceId")) { + serviceId = nullOrTrimmed(objChargingMetaData.get("serviceId").toString()); + } } - if (!objChargingMetaData.isNull("mandateId") ) { - mandateId = nullOrTrimmed(objChargingMetaData.get("mandateId").toString()); - } - if (!objChargingMetaData.isNull("productId") ) { - productId = nullOrTrimmed(objChargingMetaData.get("productId").toString()); - } - if (!objChargingMetaData.isNull("serviceId")) { - serviceId = nullOrTrimmed(objChargingMetaData.get("serviceId").toString()); + + if (log.isDebugEnabled()) { + log.debug("Manipulated received JSON Object: " + json); } - + + } catch (JSONException e) { + log.error("Manipulating received JSON Object: " + e); + throw new CustomException("SVC0001", "", new String[]{"Incorrect JSON Object received"}); } catch (Exception e) { - System.out.println("Manipulating recived JSON Object: " + e); + log.error("Manipulating recived JSON Object: " + e); throw new CustomException("POL0299", "Unexpected Error", new String[]{""}); }