mirror of
https://github.com/janishutz/libreevent.git
synced 2025-11-25 13:24:24 +00:00
stripe almost working
This commit is contained in:
@@ -91,7 +91,8 @@ if ( settings.init ) {
|
||||
}
|
||||
|
||||
console.log( '[ Server ] loading plugins' );
|
||||
// pluginManager.load( app, settings );
|
||||
// TODO: load dynamically
|
||||
require( './backend/plugins/payments/stripe/stripeRoutes.js' )( app, settings ); // setup routes
|
||||
|
||||
app.use( ( request, response ) => {
|
||||
response.sendFile( file );
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{ "test2": { "name": "TestEvent2", "location": "TestLocation2", "eventID": "test2", "date": "2023-07-15", "currency": "CHF", "categories": { "1": { "price": { "1":25, "2":35 }, "bg": "black", "fg": "white", "name": "Category 1" }, "2": { "price": { "1":15, "2":20 }, "bg": "green", "fg": "white", "name": "Category 2" } }, "ageGroups": { "1":{ "id": 1, "name":"Child", "age":"0 - 15.99" }, "2":{ "id": 2, "name": "Adult" } }, "maxTickets": 2 } }
|
||||
@@ -55,7 +55,7 @@ class SQLDB {
|
||||
if ( error ) throw error;
|
||||
if ( results[ 0 ][ '@@default_storage_engine' ] !== 'InnoDB' ) return 'DB HAS TO USE InnoDB!';
|
||||
} );
|
||||
this.sqlConnection.query( 'CREATE TABLE libreevent_users ( account_id INT ( 10 ) NOT NULL AUTO_INCREMENT, email TINYTEXT NOT NULL, pass TEXT, street TEXT, house_number TINYTEXT, country TEXT, phone TEXT, name TEXT, first_name TEXT, two_fa TINYTEXT, PRIMARY KEY ( account_id ) ) ENGINE=INNODB;', ( error ) => {
|
||||
this.sqlConnection.query( 'CREATE TABLE libreevent_users ( account_id INT ( 10 ) NOT NULL AUTO_INCREMENT, email TINYTEXT NOT NULL, pass TEXT, name TEXT, first_name TEXT, two_fa TINYTEXT, user_data VARCHAR( 60000 ), PRIMARY KEY ( account_id ) ) ENGINE=INNODB;', ( error ) => {
|
||||
if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error;
|
||||
this.sqlConnection.query( 'CREATE TABLE libreevent_orders ( order_id INT ( 10 ) NOT NULL AUTO_INCREMENT, account_id INT ( 10 ) NOT NULL, seats VARCHAR( 60000 ), PRIMARY KEY ( order_id ), FOREIGN KEY ( account_id ) REFERENCES libreevent_users( account_id ) ) ENGINE=INNODB;', ( error ) => {
|
||||
if ( error ) if ( error.code !== 'ER_TABLE_EXISTS_ERROR' ) throw error;
|
||||
|
||||
14
src/server/backend/payments/paymentHandler.js
Normal file
14
src/server/backend/payments/paymentHandler.js
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* libreevent - successHandler.js
|
||||
*
|
||||
* Created by Janis Hutz 08/02/2023, Licensed under the GPL V3 License
|
||||
* https://janishutz.com, development@janishutz.com
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
class PaymentHandler {
|
||||
constructor () {}
|
||||
}
|
||||
|
||||
module.exports = PaymentHandler;
|
||||
8
src/server/backend/payments/paymentRoutes.js
Normal file
8
src/server/backend/payments/paymentRoutes.js
Normal file
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
* libreevent - paymentRoutes.js
|
||||
*
|
||||
* Created by Janis Hutz 08/02/2023, Licensed under the GPL V3 License
|
||||
* https://janishutz.com, development@janishutz.com
|
||||
*
|
||||
*
|
||||
*/
|
||||
@@ -9,10 +9,16 @@
|
||||
|
||||
const fs = require( 'fs' );
|
||||
const path = require( 'path' );
|
||||
const stripe = require( 'stripe' )( fs.readFileSync( path.join( __dirname + '/../../../../config/payments.config.secret.json' ) )[ 'stripe' ][ 'APIKey' ] );
|
||||
const db = require( '../../../db/db.js' );
|
||||
const stripConfig = JSON.parse( fs.readFileSync( path.join( __dirname + '/../../../../config/payments.config.secret.json' ) ) )[ 'stripe' ];
|
||||
const stripe = require( 'stripe' )( stripConfig[ 'APIKey' ] );
|
||||
|
||||
const endpointSecret = stripConfig[ 'endpointSecret' ];
|
||||
|
||||
// TODO: Remove all selected tickets if timestamp more than user defined amount ago
|
||||
|
||||
module.exports = ( app, settings ) => {
|
||||
app.post( '/payments/prepare', async ( req, res ) => {
|
||||
app.post( '/payments/prepare', ( req, res ) => {
|
||||
let purchase = {
|
||||
'line_items': [],
|
||||
'mode': 'payment',
|
||||
@@ -22,20 +28,36 @@ module.exports = ( app, settings ) => {
|
||||
'customer_email': req.body.mail
|
||||
};
|
||||
|
||||
for ( let item in req.body.products ) {
|
||||
purchase[ 'line_items' ].push( {
|
||||
'price_data': {
|
||||
'currency': req.body.currency,
|
||||
'product_data': {
|
||||
'name': req.body.products[ item ].name,
|
||||
},
|
||||
'unit_amount': req.body.products[ item ].price
|
||||
},
|
||||
'quantity': req.body.products[ item ].count ?? 1,
|
||||
} );
|
||||
}
|
||||
const session = await stripe.checkout.sessions.create( purchase );
|
||||
res.send( session.url );
|
||||
db.getDataSimple( 'temp', 'user_id', req.session.id ).then( dat => {
|
||||
if ( dat[ 0 ] ) {
|
||||
db.getJSONData( 'events' ).then( events => {
|
||||
let data = JSON.parse( dat[ 0 ].data );
|
||||
( async () => {
|
||||
for ( let event in data ) {
|
||||
for ( let item in data[ event ] ) {
|
||||
purchase[ 'line_items' ].push( {
|
||||
'price_data': {
|
||||
'product_data': {
|
||||
'name': data[ event ][ item ].name,
|
||||
},
|
||||
'currency': events[ event ].currency,
|
||||
'unit_amount': Math.round( parseFloat( events[ event ][ 'categories' ][ data[ event ][ item ].category ].price[ data[ event ][ item ][ 'ticketOption' ] ] ) * 100 ),
|
||||
},
|
||||
'quantity': data[ event ][ item ].count ?? 1,
|
||||
} );
|
||||
}
|
||||
}
|
||||
const session = await stripe.checkout.sessions.create( purchase );
|
||||
res.send( session.url );
|
||||
} )();
|
||||
} );
|
||||
} else {
|
||||
res.status( 400 ).send( 'ERR_UID_NOT_FOUND' );
|
||||
}
|
||||
} ).catch( error => {
|
||||
console.error( '[ STRIPE ] DB ERROR: ' + error );
|
||||
res.status( 500 ).send( 'ERR_DB' );
|
||||
} );
|
||||
} );
|
||||
|
||||
app.get( '/payments/status', ( request, response ) => {
|
||||
@@ -50,6 +72,17 @@ module.exports = ( app, settings ) => {
|
||||
} );
|
||||
|
||||
app.post( '/payments/webhook', ( req, res ) => {
|
||||
// The webhook stripe sends data to
|
||||
const payload = req.body;
|
||||
const sig = req.headers[ 'stripe-signature' ];
|
||||
|
||||
let event;
|
||||
|
||||
try {
|
||||
event = stripe.webhooks.constructEvent( payload, sig, endpointSecret );
|
||||
} catch ( err ) {
|
||||
return res.status( 400 ).send( 'Webhook Error' );
|
||||
}
|
||||
|
||||
res.status( 200 ).end();
|
||||
} );
|
||||
};
|
||||
Reference in New Issue
Block a user