-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add javascript checkout parameter validation (#90)
* Add javascript checkout parameter validation
- Loading branch information
1 parent
934094b
commit 9fba2c0
Showing
7 changed files
with
5,573 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.