diff --git a/app.js b/app.js index 8d03546..bcd3a9c 100644 --- a/app.js +++ b/app.js @@ -38,40 +38,12 @@ if(baseConfig === false){ } // Validate the payment gateway config -switch(config.paymentGateway){ - case 'paypal': - if(ajv.validate(require('./config/paypalSchema'), require('./config/paypal.json')) === false){ - console.log(colors.red(`PayPal config is incorrect: ${ajv.errorsText()}`)); - process.exit(2); - } - break; - - case 'stripe': - if(ajv.validate(require('./config/stripeSchema'), require('./config/stripe.json')) === false){ - console.log(colors.red(`Stripe config is incorrect: ${ajv.errorsText()}`)); - process.exit(2); - } - break; - - case 'authorizenet': - if(ajv.validate(require('./config/authorizenetSchema'), require('./config/authorizenet.json')) === false){ - console.log(colors.red(`Authorizenet config is incorrect: ${ajv.errorsText()}`)); - process.exit(2); - } - break; - - case 'adyen': - if(ajv.validate(require('./config/adyenSchema'), require('./config/adyen.json')) === false){ - console.log(colors.red(`adyen config is incorrect: ${ajv.errorsText()}`)); - process.exit(2); - } - break; - case 'instore': - if(ajv.validate(require('./config/instoreSchema'), require('./config/instore.json')) === false){ - console.log(colors.red(`instore config is incorrect: ${ajv.errorsText()}`)); - process.exit(2); - } - break; +if(ajv.validate( + require(`./config/payment/schema/${config.paymentGateway}Schema`), + require(`./config/payment/config/${config.paymentGateway}.json`)) === false + ){ + console.log(colors.red(`${config.paymentGateway} config is incorrect: ${ajv.errorsText()}`)); + process.exit(2); } // require the routes @@ -81,12 +53,9 @@ const product = require('./routes/product'); const customer = require('./routes/customer'); const order = require('./routes/order'); const user = require('./routes/user'); -const paypal = require('./routes/payments/paypal'); -const stripe = require('./routes/payments/stripe'); -const blockonomics = require('./routes/payments/blockonomics'); -const authorizenet = require('./routes/payments/authorizenet'); -const adyen = require('./routes/payments/adyen'); -const instore = require('./routes/payments/instore'); + +// Add the payment route +const paymentRoute = require(`./lib/payments/${config.paymentGateway}`); const app = express(); @@ -410,19 +379,16 @@ app.use((req, res, next) => { next(); }); -// setup the routes +// Setup the routes app.use('/', index); app.use('/', customer); app.use('/', product); app.use('/', order); app.use('/', user); app.use('/', admin); -app.use('/paypal', paypal); -app.use('/stripe', stripe); -app.use('/blockonomics', blockonomics); -app.use('/authorizenet', authorizenet); -app.use('/adyen', adyen); -app.use('/instore', instore); + +// Payment route +app.use(`/${config.paymentGateway}`, paymentRoute); // catch 404 and forward to error handler app.use((req, res, next) => { diff --git a/config/adyen.json b/config/payment/config/adyen.json similarity index 100% rename from config/adyen.json rename to config/payment/config/adyen.json diff --git a/config/authorizenet.json b/config/payment/config/authorizenet.json similarity index 100% rename from config/authorizenet.json rename to config/payment/config/authorizenet.json diff --git a/config/blockonomics.json b/config/payment/config/blockonomics.json similarity index 100% rename from config/blockonomics.json rename to config/payment/config/blockonomics.json diff --git a/config/instore.json b/config/payment/config/instore.json similarity index 100% rename from config/instore.json rename to config/payment/config/instore.json diff --git a/config/paypal.json b/config/payment/config/paypal.json similarity index 100% rename from config/paypal.json rename to config/payment/config/paypal.json diff --git a/config/stripe.json b/config/payment/config/stripe.json similarity index 100% rename from config/stripe.json rename to config/payment/config/stripe.json diff --git a/config/adyenSchema.json b/config/payment/schema/adyenSchema.json similarity index 100% rename from config/adyenSchema.json rename to config/payment/schema/adyenSchema.json diff --git a/config/authorizenetSchema.json b/config/payment/schema/authorizenetSchema.json similarity index 100% rename from config/authorizenetSchema.json rename to config/payment/schema/authorizenetSchema.json diff --git a/config/blockonomicsSchema.json b/config/payment/schema/blockonomicsSchema.json similarity index 100% rename from config/blockonomicsSchema.json rename to config/payment/schema/blockonomicsSchema.json diff --git a/config/instoreSchema.json b/config/payment/schema/instoreSchema.json similarity index 100% rename from config/instoreSchema.json rename to config/payment/schema/instoreSchema.json diff --git a/config/paypalSchema.json b/config/payment/schema/paypalSchema.json similarity index 100% rename from config/paypalSchema.json rename to config/payment/schema/paypalSchema.json diff --git a/config/stripeSchema.json b/config/payment/schema/stripeSchema.json similarity index 100% rename from config/stripeSchema.json rename to config/payment/schema/stripeSchema.json diff --git a/lib/common.js b/lib/common.js index 20c0be0..8cf2d1e 100755 --- a/lib/common.js +++ b/lib/common.js @@ -336,20 +336,13 @@ const getConfig = () => { const getPaymentConfig = () => { const siteConfig = getConfig(); - const gateConfigFile = path.join(__dirname, '../config', `${siteConfig.paymentGateway}.json`); + const gateConfigFile = path.join(__dirname, '../config', 'payment', 'config', `${siteConfig.paymentGateway}.json`); let config = []; if(fs.existsSync(gateConfigFile)){ config = JSON.parse(fs.readFileSync(gateConfigFile, 'utf8')); } - // If a local config we combine the objects. Local configs are .gitignored - const localConfig = path.join(__dirname, '../config', `${siteConfig.paymentGateway}-local.json`); - if(fs.existsSync(localConfig)){ - const localConfigObj = JSON.parse(fs.readFileSync(localConfig, 'utf8')); - config = Object.assign(config, localConfigObj); - } - // Override from env.yaml environment file Object.keys(config).forEach((configKey) => { if(process.env[configKey]){ diff --git a/routes/payments/adyen.js b/lib/payments/adyen.js similarity index 98% rename from routes/payments/adyen.js rename to lib/payments/adyen.js index 4b385fd..42fb844 100644 --- a/routes/payments/adyen.js +++ b/lib/payments/adyen.js @@ -1,6 +1,6 @@ const express = require('express'); -const common = require('../../lib/common'); -const { indexOrders } = require('../../lib/indexing'); +const common = require('../common'); +const { indexOrders } = require('../indexing'); const numeral = require('numeral'); const { Client, CheckoutAPI } = require('@adyen/api-library'); const router = express.Router(); diff --git a/routes/payments/authorizenet.js b/lib/payments/authorizenet.js similarity index 98% rename from routes/payments/authorizenet.js rename to lib/payments/authorizenet.js index 9c150ee..056f9d7 100644 --- a/routes/payments/authorizenet.js +++ b/lib/payments/authorizenet.js @@ -1,8 +1,8 @@ const express = require('express'); const axios = require('axios'); const stripBom = require('strip-bom'); -const common = require('../../lib/common'); -const { indexOrders } = require('../../lib/indexing'); +const common = require('../common'); +const { indexOrders } = require('../indexing'); const router = express.Router(); // The homepage of the site diff --git a/routes/payments/blockonomics.js b/lib/payments/blockonomics.js similarity index 96% rename from routes/payments/blockonomics.js rename to lib/payments/blockonomics.js index 59b0633..41f7af4 100644 --- a/routes/payments/blockonomics.js +++ b/lib/payments/blockonomics.js @@ -1,6 +1,6 @@ const express = require('express'); -const common = require('../../lib/common'); -const { indexOrders } = require('../../lib/indexing'); +const common = require('../common'); +const { indexOrders } = require('../indexing'); const router = express.Router(); const axios = require('axios').default; diff --git a/routes/payments/instore.js b/lib/payments/instore.js similarity index 96% rename from routes/payments/instore.js rename to lib/payments/instore.js index b29feae..161ade5 100644 --- a/routes/payments/instore.js +++ b/lib/payments/instore.js @@ -1,6 +1,6 @@ const express = require('express'); -const common = require('../../lib/common'); -const { indexOrders } = require('../../lib/indexing'); +const common = require('../common'); +const { indexOrders } = require('../indexing'); const router = express.Router(); // The homepage of the site diff --git a/routes/payments/paypal.js b/lib/payments/paypal.js similarity index 98% rename from routes/payments/paypal.js rename to lib/payments/paypal.js index 7f7d99c..3dfdb23 100644 --- a/routes/payments/paypal.js +++ b/lib/payments/paypal.js @@ -1,6 +1,6 @@ const express = require('express'); -const common = require('../../lib/common'); -const { indexOrders } = require('../../lib/indexing'); +const common = require('../common'); +const { indexOrders } = require('../indexing'); const paypal = require('paypal-rest-sdk'); const router = express.Router(); diff --git a/routes/payments/stripe.js b/lib/payments/stripe.js similarity index 99% rename from routes/payments/stripe.js rename to lib/payments/stripe.js index c47d16d..9224aea 100644 --- a/routes/payments/stripe.js +++ b/lib/payments/stripe.js @@ -1,6 +1,6 @@ const express = require('express'); -const common = require('../../lib/common'); -const { indexOrders } = require('../../lib/indexing'); +const common = require('../common'); +const { indexOrders } = require('../indexing'); const numeral = require('numeral'); const stripe = require('stripe')(common.getPaymentConfig().secretKey); const router = express.Router();