Skip to content

Commit

Permalink
[OB3] [Accelerator] Saving Multiple Control Parameters in Variable Re…
Browse files Browse the repository at this point in the history
…curring Payment Consent Initiation Payload (#74)

Adds Fixes in VRP Submission Flow
  • Loading branch information
Sandaru-Vithanage authored Jun 28, 2024
1 parent 5e1fe37 commit 004a5ca
Show file tree
Hide file tree
Showing 13 changed files with 642 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ CREATE TABLE OB_CONSENT_FILE (
CREATE TABLE OB_CONSENT_ATTRIBUTE (
CONSENT_ID VARCHAR(255) NOT NULL,
ATT_KEY VARCHAR(255) NOT NULL,
ATT_VALUE VARCHAR(255) NOT NULL,
ATT_VALUE VARCHAR(1023) NOT NULL,
PRIMARY KEY(CONSENT_ID, ATT_KEY),
CONSTRAINT FK_OB_CONSENT_ATTRIBUTE FOREIGN KEY (CONSENT_ID) REFERENCES OB_CONSENT (CONSENT_ID) ON DELETE CASCADE
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ ENGINE INNODB;
CREATE TABLE IF NOT EXISTS OB_CONSENT_ATTRIBUTE (
CONSENT_ID VARCHAR(255) NOT NULL,
ATT_KEY VARCHAR(255) NOT NULL,
ATT_VALUE VARCHAR(255) NOT NULL,
ATT_VALUE VARCHAR(1023) NOT NULL,
PRIMARY KEY(CONSENT_ID, ATT_KEY),
CONSTRAINT FK_OB_CONSENT_ATTRIBUTE FOREIGN KEY (CONSENT_ID) REFERENCES OB_CONSENT (CONSENT_ID) ON DELETE CASCADE
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ CREATE TABLE OB_CONSENT_FILE (
CREATE TABLE OB_CONSENT_ATTRIBUTE (
CONSENT_ID VARCHAR(255) NOT NULL,
ATT_KEY VARCHAR(255) NOT NULL,
ATT_VALUE VARCHAR(255) NOT NULL,
ATT_VALUE VARCHAR(1023) NOT NULL,
PRIMARY KEY(CONSENT_ID, ATT_KEY),
CONSTRAINT FK_OB_CONSENT_ATTRIBUTE FOREIGN KEY (CONSENT_ID) REFERENCES OB_CONSENT (CONSENT_ID) ON DELETE CASCADE
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ CREATE TABLE IF NOT EXISTS OB_CONSENT_FILE (
CREATE TABLE IF NOT EXISTS OB_CONSENT_ATTRIBUTE (
CONSENT_ID VARCHAR(255) NOT NULL,
ATT_KEY VARCHAR(255) NOT NULL,
ATT_VALUE VARCHAR(255) NOT NULL,
ATT_VALUE VARCHAR(1023) NOT NULL,
PRIMARY KEY(CONSENT_ID, ATT_KEY),
CONSTRAINT FK_OB_CONSENT_ATTRIBUTE FOREIGN KEY (CONSENT_ID) REFERENCES OB_CONSENT (CONSENT_ID) ON DELETE CASCADE
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ public class ErrorConstants {
public static final String CONSENT_ID_NOT_FOUND = "Consent ID not available in consent data";
public static final String FIELD_INVALID_DATE = "OB.Field.InvalidDate";
public static final String EXPIRED_DATE_ERROR = "The ExpirationDateTime value has to be a future date.";
public static final String CONSENT_ATTRIBUTE_RETRIEVAL_ERROR = "Error occurred while retrieving the consent " +
"attributes";

// VRP error constants

Expand Down Expand Up @@ -263,6 +265,14 @@ public class ErrorConstants {
public static final String INVALID_PERIOD_ALIGNMENT = "Invalid value for period alignment in PeriodicLimits";
public static final String INVALID_PARAMETER_MESSAGE = "Parameter '%s' passed in is null, empty, or not a %s";
public static final String DATE_INVALID_PARAMETER_MESSAGE = "Invalid date-time range for ValidToDateTime ";
public static final String INVALID_PERIODIC_LIMIT_SIZE = "Periodic limits exceed the allowed limits";
public static final String DUPLICATE_PERIOD_TYPE = "Duplicate Period Types found in the request";
public static final String CURRENCY_MISMATCH = "Currency does not match with the currency of the periodic limits";
public static final int MAXIMUM_PERIODIC_LIMITS = 6;
public static final String INVALID_MAXIMUM_INDIVIDUAL_CURRENCY = "Invalid value for Currency in " +
"MaximumIndividualAmount";
public static final String INVALID_PERIODIC_LIMIT_AMOUNT = "Invalid value for in Amount in Periodic Limits";
public static final String INVALID_PERIODIC_LIMIT_CURRENCY = "Invalid value for Currency in Periodic Limits";


// vrp path parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,11 @@ public class ConsentExtensionConstants {
public static final String MAXIMUM_INDIVIDUAL_AMOUNT = "MaximumIndividualAmount";
public static final String MAXIMUM_INDIVIDUAL_AMOUNT_CURRENCY = "MaximumIndividualAmount.Amount.Currency";
public static final String PERIODIC_LIMITS = "PeriodicLimits";
public static final String PERIODIC_TYPES = "PeriodicTypes";
public static final String PERIOD_AMOUNT_LIMIT = "Amount";
public static final String PERIOD_LIMIT_CURRENCY = "PeriodicLimits.Currency";
public static final String CYCLIC_EXPIRY_TIME = "cyclicExpiryTime";
public static final String CYCLIC_REMAINING_AMOUNT = "cyclicRemainingAmount";

//vrp period alignment
public static final String PERIOD_ALIGNMENT = "PeriodAlignment";
Expand Down Expand Up @@ -219,4 +222,6 @@ public class ConsentExtensionConstants {
public static final String VRP_PATH = "/domestic-vrps";
public static final String PREVIOUS_PAID_AMOUNT = "prevPaidAmount";
public static final String PREVIOUS_LAST_PAYMENT_DATE = "prevLastPaymentDate";


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses 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. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.wso2.openbanking.accelerator.consent.extensions.event.executors;

import com.wso2.openbanking.accelerator.common.event.executor.OBEventExecutor;
import com.wso2.openbanking.accelerator.common.event.executor.model.OBEvent;
import com.wso2.openbanking.accelerator.consent.extensions.common.ConsentExtensionConstants;
import com.wso2.openbanking.accelerator.consent.extensions.manage.model.PeriodicLimit;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
* This class is responsible for executing Variable Recurring Payments (VRP) events.
* It implements the OBEventExecutor interface and overrides its methods to provide
* specific implementations for VRP events.
*/
public class VRPEventExecutor implements OBEventExecutor {

public static List<PeriodicLimit> validateInstructedAmountWithControlParameters(BigDecimal instructedAmount,
JSONObject controlParameters) {

/**
* Validates the instructed amount with control parameters and returns a list of PeriodicLimit objects.
* If the instructed amount is greater than the maximum individual amount or the cyclic remaining amount,
* an empty list is returned. If the JSON parsing fails, an empty list is also returned.
*
* @param instructedAmount The instructed amount to be validated
* @param controlParameters The control parameters to be used for validation
* @return A list of PeriodicLimit objects
*/
List<PeriodicLimit> periodicLimitsList = new ArrayList<>();

BigDecimal maxIndividualAmount = BigDecimal.valueOf(Double.parseDouble(controlParameters.
getAsString(ConsentExtensionConstants.MAXIMUM_INDIVIDUAL_AMOUNT)));

if (instructedAmount.compareTo(maxIndividualAmount) > 0) {
return periodicLimitsList;
}

JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
JSONArray periodicLimits;

try {
periodicLimits = (JSONArray) parser.parse(controlParameters.
getAsString(ConsentExtensionConstants.PERIODIC_LIMITS));
} catch (ParseException e) {
// Log the error or handle it as needed
return periodicLimitsList;
}

long currentMoment = System.currentTimeMillis() / 1000;

for (Object obj : periodicLimits) {
JSONObject limit = (JSONObject) obj;
BigDecimal amount = BigDecimal.
valueOf(Double.parseDouble(limit.getAsString(ConsentExtensionConstants.AMOUNT)));
long cyclicExpiryTime = Long.parseLong(limit.getAsString(ConsentExtensionConstants.CYCLIC_EXPIRY_TIME));
BigDecimal cyclicRemainingAmount = BigDecimal.
valueOf(Double.parseDouble(limit.getAsString(ConsentExtensionConstants.CYCLIC_REMAINING_AMOUNT)));

String periodType = limit.getAsString(ConsentExtensionConstants.PERIOD_TYPE);
String periodAlignment = limit.getAsString(ConsentExtensionConstants.PERIOD_ALIGNMENT);

PeriodicLimit periodicLimit = new PeriodicLimit(periodType, amount, periodAlignment);

if (currentMoment <= cyclicExpiryTime) {
if (instructedAmount.compareTo(cyclicRemainingAmount) > 0) {
return periodicLimitsList;
} else {
cyclicRemainingAmount = cyclicRemainingAmount.subtract(instructedAmount);
}
} else {
while (currentMoment > periodicLimit.getCyclicExpiryTime()) {
periodicLimit.setCyclicExpiryTime();
}
cyclicRemainingAmount = amount;
if (instructedAmount.compareTo(cyclicRemainingAmount) > 0) {
return periodicLimitsList;
} else {
cyclicRemainingAmount = cyclicRemainingAmount.subtract(instructedAmount);
}
}
periodicLimitsList.add(periodicLimit);
}

return periodicLimitsList;
}

/**
* Processes the given OBEvent. This method is part of the OBEventExecutor interface and needs to be implemented.
*
* @param obEvent The OBEvent to be processed
*/
@Override
public void processEvent(OBEvent obEvent) {

}
}
Loading

0 comments on commit 004a5ca

Please sign in to comment.