connector-openpay-mx
Version:
Package that allows us to connect to OpenPay Mexico and make use of the following resources.
77 lines (63 loc) • 1.92 kB
JavaScript
const Joi = require('joi');
const CONTEXT_DEFAULT = 'sandbox';
const { defaultConfig } = require('../configuration');
const validation = {};
validation.isValidMerchant = account => isMerchant(normalizedMerchant(account));
const normalizedMerchant = account => {
let { ctx, merchant } = account;
const { private_key, public_key } = merchant;
ctx = ctx ? ctx : CONTEXT_DEFAULT;
return {
...defaultConfig[ctx],
openpay_id: account.merchant.id,
openpay_private_key: private_key,
openpay_public_key: public_key
}
};
const isMerchant = account => {
const scheme = Joi.object({
openpay_id: Joi.string()
.alphanum()
.min(20)
.max(45)
.required(),
openpay_url: Joi.string()
.uri({
scheme: [
/https/
]
})
.required(),
openpay_url_pdf: Joi.string()
.uri({
scheme: [
/https/
]
})
.required(),
openpay_private_key: Joi.string()
.min(35)
.max(35)
.required(),
openpay_public_key: Joi.string()
.min(35)
.max(35)
.required(),
openpay_set_production: Joi.boolean()
.truthy('TRUE')
.truthy('true')
.falsy('FALSE')
.falsy('false')
.sensitive()
.default(false),
});
try {
const { error = null, value } = scheme.validate(account);
const response = error === null ? value : null;
return { error, response };
}
catch (err) {
throw new Error('verify that the merchant details are correct. ' + err);
}
}
module.exports = validation;