Skip to content

Commit

Permalink
Add javascript checkout parameter validation (#90)
Browse files Browse the repository at this point in the history
* Add javascript checkout parameter validation
  • Loading branch information
okenshields committed Sep 27, 2019
1 parent 934094b commit 9fba2c0
Show file tree
Hide file tree
Showing 7 changed files with 5,573 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module.exports = {
"react-native"
],
"env": {
"react-native/react-native": true
"react-native/react-native": true,
"jest": true
},
"rules": {
"react-native/no-unused-styles": 2,
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ matrix:
- language: node_js
node_js: 8
script:
- yarn && yarn lint && cd reader-sdk-react-native-quickstart && yarn && yarn lint && cd ..
- yarn && yarn lint && yarn test && cd reader-sdk-react-native-quickstart && yarn && yarn lint && cd ..

- language: android
sudo: false
Expand Down
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
/*
Copyright 2019 Square Inc.
Licensed 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.
*/

import { NativeModules } from 'react-native'; // eslint-disable-line import/no-unresolved
import ValidateCheckoutParameters from './src/utils';

const {
RNReaderSDKAuthorization,
Expand Down Expand Up @@ -57,6 +74,7 @@ export async function getAuthorizedLocationAsync() {

export async function startCheckoutAsync(checkoutParams) {
try {
ValidateCheckoutParameters(checkoutParams);
return await RNReaderSDKCheckout.startCheckout(checkoutParams);
} catch (ex) {
throw createReaderSDKError(ex);
Expand Down
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"main": "index.js",
"scripts": {
"lint": "eslint ./ --ext .js"
"lint": "eslint ./ --ext .js",
"test": "jest"
},
"keywords": [
"react-native",
Expand All @@ -30,6 +31,18 @@
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-react": "^7.11.1",
"eslint-plugin-react-native": "^3.2.1"
"eslint-plugin-react-native": "^3.2.1",
"jest": "^24.9.0",
"react": "^16.9.0",
"react-native": "^0.60.5"
},
"jest": {
"preset": "react-native",
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
},
"modulePathIgnorePatterns": [
"reader-sdk-react-native-quickstart"
]
}
}
143 changes: 143 additions & 0 deletions src/__tests__/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
Copyright 2019 Square Inc.
Licensed 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.
*/
import ValidateCheckoutParameters from '../utils';

let checkoutParams = {};

describe('Test ValidateCheckoutParameters', () => {
beforeEach(() => {
checkoutParams = {
amountMoney: {
amount: 100,
currencyCode: 'USD', // optional, use authorized location's currency code by default
},
// Optional for all following configuration
skipReceipt: false,
collectSignature: true,
allowSplitTender: false,
delayCapture: false,
note: 'Hello 💳 💰 World!',
tipSettings: {
showCustomTipField: true,
showSeparateTipScreen: false,
tipPercentages: [15, 20, 30],
},
additionalPaymentTypes: ['cash', 'manual_card_entry', 'other'],
};
});

it('returns when checkoutParams pass validation', () => {
expect(ValidateCheckoutParameters(checkoutParams)).toBeUndefined();
});

it('throws if checkoutParams is null', () => {
checkoutParams = null;
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'checkoutParams' is undefined or null");
});

it('throws if amountMoney is null', () => {
checkoutParams.amountMoney = null;
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'amountMoney' is missing or not an object");
});

it('throws if skipReceipt is not a boolean', () => {
checkoutParams.skipReceipt = 1;
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'skipReceipt' is not a boolean");
});

it('throws if collectSignature is not a boolean', () => {
checkoutParams.collectSignature = 1;
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'collectSignature' is not a boolean");
});

it('throws if allowSplitTender is not a boolean', () => {
checkoutParams.allowSplitTender = 1;
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'allowSplitTender' is not a boolean");
});

it('throws if delayCapture is not a boolean', () => {
checkoutParams.delayCapture = 1;
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'delayCapture' is not a boolean");
});

it('throws if note is not a string', () => {
checkoutParams.note = 1;
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'note' is not a string");
});

it('throws if tipSettings is not an object', () => {
checkoutParams.tipSettings = 1;
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'tipSettings' is not an object");
});

it('throws if additionalPaymentTypes is not an array', () => {
checkoutParams.additionalPaymentTypes = {};
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'additionalPaymentTypes' is not an array");
});

it('throws if amountMoney.amount is not a number', () => {
checkoutParams.amountMoney.amount = false;
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'amount' is not an integer");
});

it('throws if amountMoney.currencyCode is not a string', () => {
checkoutParams.amountMoney.currencyCode = false;
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'currencyCode' is not a String");
});

it('throws if tipSettings.showCustomTipField is not a boolean', () => {
checkoutParams.tipSettings.showCustomTipField = 2;
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'showCustomTipField' is not a boolean");
});

it('throws if tipSettings.showSeparateTipScreen is not a boolean', () => {
checkoutParams.tipSettings.showSeparateTipScreen = 1;
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'showSeparateTipScreen' is not a boolean");
});

it('throws if tipSettings.tipPercentages is not an array', () => {
checkoutParams.tipSettings.tipPercentages = {};
expect(() => {
ValidateCheckoutParameters(checkoutParams);
}).toThrow("'tipPercentages' is not an array");
});
});
84 changes: 84 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2019 Square Inc.
Licensed 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.
*/

function hasNonNullProperty(obj, property) {
if (Object.prototype.hasOwnProperty.call(obj, property) && typeof obj[property] !== 'undefined' && obj[property] !== null) {
return true;
}
return false;
}

export default function ValidateCheckoutParameters(checkoutParams) {
const paramError = {};
paramError.debugCode = 'rn_checkout_invalid_parameter';
paramError.message = 'Something went wrong. Please contact the developer of this application and provide them with this error code: rn_checkout_invalid_parameter';
paramError.debugMessage = 'Invalid parameter found in checkout parameters. ';
if (!checkoutParams) {
paramError.debugMessage += "'checkoutParams' is undefined or null";
throw new Error(JSON.stringify(paramError));
}
if (!hasNonNullProperty(checkoutParams, 'amountMoney') || typeof checkoutParams.amountMoney !== 'object') {
paramError.debugMessage += "'amountMoney' is missing or not an object";
throw new Error(JSON.stringify(paramError));
} else if (hasNonNullProperty(checkoutParams, 'skipReceipt') && typeof checkoutParams.skipReceipt !== 'boolean') {
paramError.debugMessage += "'skipReceipt' is not a boolean";
throw new Error(JSON.stringify(paramError));
} else if (hasNonNullProperty(checkoutParams, 'collectSignature') && typeof checkoutParams.collectSignature !== 'boolean') {
paramError.debugMessage += "'collectSignature' is not a boolean";
throw new Error(JSON.stringify(paramError));
} else if (hasNonNullProperty(checkoutParams, 'allowSplitTender') && typeof checkoutParams.allowSplitTender !== 'boolean') {
paramError.debugMessage += "'allowSplitTender' is not a boolean";
throw new Error(JSON.stringify(paramError));
} else if (hasNonNullProperty(checkoutParams, 'delayCapture') && typeof checkoutParams.delayCapture !== 'boolean') {
paramError.debugMessage += "'delayCapture' is not a boolean";
throw new Error(JSON.stringify(paramError));
} else if (hasNonNullProperty(checkoutParams, 'note') && typeof checkoutParams.note !== 'string') {
paramError.debugMessage += "'note' is not a string";
throw new Error(JSON.stringify(paramError));
} else if (hasNonNullProperty(checkoutParams, 'tipSettings') && typeof checkoutParams.tipSettings !== 'object') {
paramError.debugMessage += "'tipSettings' is not an object";
throw new Error(JSON.stringify(paramError));
} else if (hasNonNullProperty(checkoutParams, 'additionalPaymentTypes') && !Array.isArray(checkoutParams.additionalPaymentTypes)) {
paramError.debugMessage += "'additionalPaymentTypes' is not an array";
throw new Error(JSON.stringify(paramError));
}

// check amountMoney
const { amountMoney } = checkoutParams;
if (!hasNonNullProperty(amountMoney, 'amount') || typeof amountMoney.amount !== 'number') {
paramError.debugMessage += "'amount' is not an integer";
throw new Error(JSON.stringify(paramError));
}
if (hasNonNullProperty(amountMoney, 'currencyCode') && typeof amountMoney.currencyCode !== 'string') {
paramError.debugMessage += "'currencyCode' is not a String";
throw new Error(JSON.stringify(paramError));
}

if (hasNonNullProperty(checkoutParams, 'tipSettings')) {
// check tipSettings
const { tipSettings } = checkoutParams;
if (hasNonNullProperty(tipSettings, 'showCustomTipField') && typeof tipSettings.showCustomTipField !== 'boolean') {
paramError.debugMessage += "'showCustomTipField' is not a boolean";
throw new Error(JSON.stringify(paramError));
} else if (hasNonNullProperty(tipSettings, 'showSeparateTipScreen') && typeof tipSettings.showSeparateTipScreen !== 'boolean') {
paramError.debugMessage += "'showSeparateTipScreen' is not a boolean";
throw new Error(JSON.stringify(paramError));
} else if (hasNonNullProperty(tipSettings, 'tipPercentages') && !Array.isArray(tipSettings.tipPercentages)) {
paramError.debugMessage += "'tipPercentages' is not an array";
throw new Error(JSON.stringify(paramError));
}
}
}
Loading

0 comments on commit 9fba2c0

Please sign in to comment.