Refactoring the payment config/schema, loading and routes path
parent
5f68a5bc1f
commit
da34f3e63c
58
app.js
58
app.js
|
@ -38,40 +38,12 @@ if(baseConfig === false){
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the payment gateway config
|
// Validate the payment gateway config
|
||||||
switch(config.paymentGateway){
|
if(ajv.validate(
|
||||||
case 'paypal':
|
require(`./config/payment/schema/${config.paymentGateway}Schema`),
|
||||||
if(ajv.validate(require('./config/paypalSchema'), require('./config/paypal.json')) === false){
|
require(`./config/payment/config/${config.paymentGateway}.json`)) === false
|
||||||
console.log(colors.red(`PayPal config is incorrect: ${ajv.errorsText()}`));
|
){
|
||||||
|
console.log(colors.red(`${config.paymentGateway} config is incorrect: ${ajv.errorsText()}`));
|
||||||
process.exit(2);
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// require the routes
|
// require the routes
|
||||||
|
@ -81,12 +53,9 @@ const product = require('./routes/product');
|
||||||
const customer = require('./routes/customer');
|
const customer = require('./routes/customer');
|
||||||
const order = require('./routes/order');
|
const order = require('./routes/order');
|
||||||
const user = require('./routes/user');
|
const user = require('./routes/user');
|
||||||
const paypal = require('./routes/payments/paypal');
|
|
||||||
const stripe = require('./routes/payments/stripe');
|
// Add the payment route
|
||||||
const blockonomics = require('./routes/payments/blockonomics');
|
const paymentRoute = require(`./lib/payments/${config.paymentGateway}`);
|
||||||
const authorizenet = require('./routes/payments/authorizenet');
|
|
||||||
const adyen = require('./routes/payments/adyen');
|
|
||||||
const instore = require('./routes/payments/instore');
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
|
@ -410,19 +379,16 @@ app.use((req, res, next) => {
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
// setup the routes
|
// Setup the routes
|
||||||
app.use('/', index);
|
app.use('/', index);
|
||||||
app.use('/', customer);
|
app.use('/', customer);
|
||||||
app.use('/', product);
|
app.use('/', product);
|
||||||
app.use('/', order);
|
app.use('/', order);
|
||||||
app.use('/', user);
|
app.use('/', user);
|
||||||
app.use('/', admin);
|
app.use('/', admin);
|
||||||
app.use('/paypal', paypal);
|
|
||||||
app.use('/stripe', stripe);
|
// Payment route
|
||||||
app.use('/blockonomics', blockonomics);
|
app.use(`/${config.paymentGateway}`, paymentRoute);
|
||||||
app.use('/authorizenet', authorizenet);
|
|
||||||
app.use('/adyen', adyen);
|
|
||||||
app.use('/instore', instore);
|
|
||||||
|
|
||||||
// catch 404 and forward to error handler
|
// catch 404 and forward to error handler
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
|
|
|
@ -336,20 +336,13 @@ const getConfig = () => {
|
||||||
|
|
||||||
const getPaymentConfig = () => {
|
const getPaymentConfig = () => {
|
||||||
const siteConfig = getConfig();
|
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 = [];
|
let config = [];
|
||||||
if(fs.existsSync(gateConfigFile)){
|
if(fs.existsSync(gateConfigFile)){
|
||||||
config = JSON.parse(fs.readFileSync(gateConfigFile, 'utf8'));
|
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
|
// Override from env.yaml environment file
|
||||||
Object.keys(config).forEach((configKey) => {
|
Object.keys(config).forEach((configKey) => {
|
||||||
if(process.env[configKey]){
|
if(process.env[configKey]){
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const common = require('../../lib/common');
|
const common = require('../common');
|
||||||
const { indexOrders } = require('../../lib/indexing');
|
const { indexOrders } = require('../indexing');
|
||||||
const numeral = require('numeral');
|
const numeral = require('numeral');
|
||||||
const { Client, CheckoutAPI } = require('@adyen/api-library');
|
const { Client, CheckoutAPI } = require('@adyen/api-library');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
|
@ -1,8 +1,8 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const stripBom = require('strip-bom');
|
const stripBom = require('strip-bom');
|
||||||
const common = require('../../lib/common');
|
const common = require('../common');
|
||||||
const { indexOrders } = require('../../lib/indexing');
|
const { indexOrders } = require('../indexing');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
// The homepage of the site
|
// The homepage of the site
|
|
@ -1,6 +1,6 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const common = require('../../lib/common');
|
const common = require('../common');
|
||||||
const { indexOrders } = require('../../lib/indexing');
|
const { indexOrders } = require('../indexing');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const axios = require('axios').default;
|
const axios = require('axios').default;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const common = require('../../lib/common');
|
const common = require('../common');
|
||||||
const { indexOrders } = require('../../lib/indexing');
|
const { indexOrders } = require('../indexing');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
// The homepage of the site
|
// The homepage of the site
|
|
@ -1,6 +1,6 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const common = require('../../lib/common');
|
const common = require('../common');
|
||||||
const { indexOrders } = require('../../lib/indexing');
|
const { indexOrders } = require('../indexing');
|
||||||
const paypal = require('paypal-rest-sdk');
|
const paypal = require('paypal-rest-sdk');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const common = require('../../lib/common');
|
const common = require('../common');
|
||||||
const { indexOrders } = require('../../lib/indexing');
|
const { indexOrders } = require('../indexing');
|
||||||
const numeral = require('numeral');
|
const numeral = require('numeral');
|
||||||
const stripe = require('stripe')(common.getPaymentConfig().secretKey);
|
const stripe = require('stripe')(common.getPaymentConfig().secretKey);
|
||||||
const router = express.Router();
|
const router = express.Router();
|
Loading…
Reference in New Issue