stripe integration done, handling missing still

This commit is contained in:
2023-08-03 15:37:50 +02:00
parent 19874cd007
commit 8784f79014
7 changed files with 39 additions and 12 deletions

View File

@@ -12,6 +12,8 @@ const geth = require( './api/getHandler.js' );
const postHandler = new posth(); const postHandler = new posth();
const getHandler = new geth(); const getHandler = new geth();
const path = require( 'path' ); const path = require( 'path' );
const bodyParser = require( 'body-parser' );
// settings is missing in arguments which shouldn't pose any problem // settings is missing in arguments which shouldn't pose any problem
module.exports = ( app ) => { module.exports = ( app ) => {
@@ -29,7 +31,7 @@ module.exports = ( app ) => {
} }
} ); } );
app.post( '/admin/API/:call', ( req, res ) => { app.post( '/admin/API/:call', bodyParser.json(), ( req, res ) => {
if ( req.session.loggedInAdmin ) { if ( req.session.loggedInAdmin ) {
postHandler.handleCall( req.params.call, req.body, req.query.lang ).then( data => { postHandler.handleCall( req.params.call, req.body, req.query.lang ).then( data => {
res.send( data ); res.send( data );

View File

@@ -14,6 +14,7 @@ const twoFA = new auth();
const path = require( 'path' ); const path = require( 'path' );
const mail = require( '../backend/mail/mailSender.js' ); const mail = require( '../backend/mail/mailSender.js' );
const mailManager = new mail(); const mailManager = new mail();
const bodyParser = require( 'body-parser' );
let responseObjects = {}; let responseObjects = {};
let authOk = {}; let authOk = {};
@@ -23,7 +24,7 @@ module.exports = ( app, settings ) => {
Admin login route that checks the password Admin login route that checks the password
*/ */
app.post( '/admin/auth', ( request, response ) => { app.post( '/admin/auth', bodyParser.json(), ( request, response ) => {
if ( request.body.mail && request.body.password ) { if ( request.body.mail && request.body.password ) {
pwdmanager.checkpassword( request.body.mail, request.body.password ).then( data => { pwdmanager.checkpassword( request.body.mail, request.body.password ).then( data => {
request.session.username = request.body.mail; request.session.username = request.body.mail;
@@ -75,7 +76,7 @@ module.exports = ( app, settings ) => {
} }
} ); } );
app.post( '/admin/2fa/verify', ( request, response ) => { app.post( '/admin/2fa/verify', bodyParser.json(), ( request, response ) => {
let verified = twoFA.verifyEnhanced( request.body.token, request.body.code ); let verified = twoFA.verifyEnhanced( request.body.token, request.body.code );
if ( verified ) { if ( verified ) {
request.session.loggedInAdmin = true; request.session.loggedInAdmin = true;

View File

@@ -73,8 +73,8 @@ app.use( expressSession( {
} }
} ) ); } ) );
app.use( bodyParser.urlencoded( { extended: false } ) ); // app.use( bodyParser.urlencoded( { extended: false } ) );
app.use( bodyParser.json() ); // app.use( bodyParser.json() );
app.use( cookieParser() ); app.use( cookieParser() );
let file = path.join( __dirname + '/../webapp/main/dist/index.html' ); let file = path.join( __dirname + '/../webapp/main/dist/index.html' );

View File

@@ -8,7 +8,17 @@
*/ */
class PaymentHandler { class PaymentHandler {
constructor () {} constructor () {
this.canceledTransactions = {};
}
async handleSuccess ( token ) {
console.log( token );
}
async handleError ( token ) {
}
} }
module.exports = PaymentHandler; module.exports = PaymentHandler;

View File

@@ -12,13 +12,18 @@ const path = require( 'path' );
const db = require( '../../../db/db.js' ); const db = require( '../../../db/db.js' );
const stripConfig = JSON.parse( fs.readFileSync( path.join( __dirname + '/../../../../config/payments.config.secret.json' ) ) )[ 'stripe' ]; const stripConfig = JSON.parse( fs.readFileSync( path.join( __dirname + '/../../../../config/payments.config.secret.json' ) ) )[ 'stripe' ];
const stripe = require( 'stripe' )( stripConfig[ 'APIKey' ] ); const stripe = require( 'stripe' )( stripConfig[ 'APIKey' ] );
const bodyParser = require( 'body-parser' );
const ph = require( '../../../payments/paymentHandler.js' );
const paymentHandler = new ph();
const endpointSecret = stripConfig[ 'endpointSecret' ]; const endpointSecret = stripConfig[ 'endpointSecret' ];
let sessionReference = {};
// TODO: Remove all selected tickets if timestamp more than user defined amount ago // TODO: Remove all selected tickets if timestamp more than user defined amount ago
module.exports = ( app, settings ) => { module.exports = ( app, settings ) => {
app.post( '/payments/prepare', ( req, res ) => { app.post( '/payments/prepare', bodyParser.json(), ( req, res ) => {
let purchase = { let purchase = {
'line_items': [], 'line_items': [],
'mode': 'payment', 'mode': 'payment',
@@ -48,6 +53,7 @@ module.exports = ( app, settings ) => {
} }
} }
const session = await stripe.checkout.sessions.create( purchase ); const session = await stripe.checkout.sessions.create( purchase );
sessionReference[ session.id ] = req.session.id;
res.send( session.url ); res.send( session.url );
} )(); } )();
} ); } );
@@ -69,9 +75,10 @@ module.exports = ( app, settings ) => {
response.status( 200 ); response.status( 200 );
response.flushHeaders(); response.flushHeaders();
response.write( 'data: connected\n\n' ); response.write( 'data: connected\n\n' );
// TODO: Finish up
} ); } );
app.post( '/payments/webhook', ( req, res ) => { app.post( '/payments/webhook', bodyParser.raw( { type: 'application/json' } ), ( req, res ) => {
const payload = req.body; const payload = req.body;
const sig = req.headers[ 'stripe-signature' ]; const sig = req.headers[ 'stripe-signature' ];
@@ -80,9 +87,14 @@ module.exports = ( app, settings ) => {
try { try {
event = stripe.webhooks.constructEvent( payload, sig, endpointSecret ); event = stripe.webhooks.constructEvent( payload, sig, endpointSecret );
} catch ( err ) { } catch ( err ) {
console.error( err );
return res.status( 400 ).send( 'Webhook Error' ); return res.status( 400 ).send( 'Webhook Error' );
} }
if ( event.type === 'checkout.session.completed' ) {
paymentHandler.handleSuccess( sessionReference[ event.data.object.id ] );
}
res.status( 200 ).end(); res.status( 200 ).end();
} ); } );
}; };

View File

@@ -12,6 +12,7 @@ const geth = require( './api/getHandler.js' );
const postHandler = new posth(); const postHandler = new posth();
const getHandler = new geth(); const getHandler = new geth();
const path = require( 'path' ); const path = require( 'path' );
const bodyParser = require( 'body-parser' );
// settings is missing in arguments which shouldn't pose any problem // settings is missing in arguments which shouldn't pose any problem
module.exports = ( app, settings ) => { module.exports = ( app, settings ) => {
@@ -31,7 +32,7 @@ module.exports = ( app, settings ) => {
} ); } );
} ); } );
app.post( '/API/:call', ( req, res ) => { app.post( '/API/:call', bodyParser.json(), ( req, res ) => {
// add lang in the future // add lang in the future
postHandler.handleCall( req.params.call, req.body, req.session ).then( data => { postHandler.handleCall( req.params.call, req.body, req.session ).then( data => {
res.send( data ); res.send( data );

View File

@@ -14,6 +14,7 @@ const twoFA = new auth();
const path = require( 'path' ); const path = require( 'path' );
const mail = require( './mail/mailSender.js' ); const mail = require( './mail/mailSender.js' );
const mailManager = new mail(); const mailManager = new mail();
const bodyParser = require( 'body-parser' );
let responseObjects = {}; let responseObjects = {};
let authOk = {}; let authOk = {};
@@ -43,7 +44,7 @@ module.exports = ( app, settings ) => {
res.send( 'ok' ); res.send( 'ok' );
} ); } );
app.post( '/user/login', ( request, response ) => { app.post( '/user/login', bodyParser.json(), ( request, response ) => {
if ( request.body.mail && request.body.password ) { if ( request.body.mail && request.body.password ) {
pwdmanager.checkpassword( request.body.mail, request.body.password ).then( data => { pwdmanager.checkpassword( request.body.mail, request.body.password ).then( data => {
request.session.username = request.body.mail; request.session.username = request.body.mail;
@@ -98,7 +99,7 @@ module.exports = ( app, settings ) => {
} }
} ); } );
app.post( '/user/2fa/verify', ( request, response ) => { app.post( '/user/2fa/verify', bodyParser.json(), ( request, response ) => {
let verified = twoFA.verifyEnhanced( request.body.token, request.body.code ); let verified = twoFA.verifyEnhanced( request.body.token, request.body.code );
if ( verified ) { if ( verified ) {
request.session.loggedInUser = true; request.session.loggedInUser = true;
@@ -136,7 +137,7 @@ module.exports = ( app, settings ) => {
response.send( 'logoutOk' ); response.send( 'logoutOk' );
} ); } );
app.post( '/user/signup', ( request, response ) => { app.post( '/user/signup', bodyParser.json(), ( request, response ) => {
// TODO: Make sure that user does not exist yet first and if user // TODO: Make sure that user does not exist yet first and if user
// exists, send back info that it is that way // exists, send back info that it is that way
response.send( 'ok' ); response.send( 'ok' );