Skip to content

Commit

Permalink
Merge pull request #483 from MilindaLaknath/2.4.4-release
Browse files Browse the repository at this point in the history
[HUBDEV-2159] fixed the bug related when request send without chargingMetaData
  • Loading branch information
jaadds authored Nov 6, 2018
2 parents 10de4eb + 45248a4 commit eb4d094
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,24 +21,29 @@
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;



// 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;
Expand Down Expand Up @@ -91,32 +96,40 @@ 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"));
}
taxAmount = Double.parseDouble(nullOrTrimmed(String.valueOf(chargingMetaData.get("taxAmount"))));
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"});
}
taxAmount = Double.parseDouble(nullOrTrimmed(String.valueOf(chargingMetaData.get("taxAmount"))));

}
}

System.out.println("Manipulated recived 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) {
System.out.println("Manipulating recived JSON Object: " + e);
log.error("Manipulating received JSON Object: " + e);
throw new CustomException("POL0299", "Unexpected Error", new String[]{""});
}

Expand All @@ -135,11 +148,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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"};

Expand Down Expand Up @@ -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[]{""});
}

Expand Down

0 comments on commit eb4d094

Please sign in to comment.