begin stripe plugin

This commit is contained in:
2023-07-25 15:23:49 +02:00
parent ff277a41ae
commit 782a245c17
10 changed files with 122 additions and 10 deletions

View File

@@ -15,6 +15,8 @@ const bodyParser = require( 'body-parser' );
const cookieParser = require( 'cookie-parser' ); const cookieParser = require( 'cookie-parser' );
const http = require( 'http' ); const http = require( 'http' );
const fs = require( 'fs' ); const fs = require( 'fs' );
const pm = require( './backend/plugins/manager.js' );
const pluginManager = new pm();
console.log( ` console.log( `
@@ -86,8 +88,8 @@ if ( settings.init ) {
file = path.join( __dirname + '/../webapp/setup/dist/index.html' ); file = path.join( __dirname + '/../webapp/setup/dist/index.html' );
} }
// TODO: Create plugin loader and manager console.log( '[ Server ] loading plugins' );
pluginManager.load( app, settings );
app.use( ( request, response ) => { app.use( ( request, response ) => {
response.sendFile( file ); response.sendFile( file );

View File

@@ -0,0 +1,28 @@
/*
* libreevent - manager.js
*
* Created by Janis Hutz 07/25/2023, Licensed under the GPL V3 License
* https://janishutz.com, development@janishutz.com
*
*
*/
/*
This is the plugin manager. It is responsible for installing, updating and uninstalling plugins.
*/
class PluginManager {
constructor () {}
install ( plugin ) {
}
update () {}
uninstall ( plugin ) {
}
}
module.exports = PluginManager;

View File

@@ -7,8 +7,21 @@ You will also need to add documentation for the user to set up the payment gatew
In the routes.js file you should have at least the following code: In the routes.js file you should have at least the following code:
``` ```
module.exports = ( app ) => { module.exports = ( app, settings ) => {
app.post( '/payments/prepare', ( req, res ) => {
} );
app.get( '/payments/status', ( request, response ) => {
response.writeHead( 200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
} );
response.status( 200 );
response.flushHeaders();
response.write( 'data: connected\n\n' );
} );
} }
``` ```

View File

@@ -7,6 +7,49 @@
* *
*/ */
module.exports = ( app ) => { const fs = require( 'fs' );
app.post( '/payments/prepare' ) const path = require( 'path' );
const stripe = require( 'stripe' )( fs.readFileSync( path.join( __dirname + '/../../../../config/payments.config.secret.json' ) )[ 'stripe' ][ 'APIKey' ] );
module.exports = ( app, settings ) => {
app.post( '/payments/prepare', async ( req, res ) => {
let purchase = {
'line_items': [],
'mode': 'payment',
'success_url': settings.yourDomain + '/payments/success',
'cancel_url': settings.yourDomain + '/payments/canceled',
'submit_type': 'book',
'customer_email': req.body.customer.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 );
} );
app.get( '/payments/status', ( request, response ) => {
response.writeHead( 200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
} );
response.status( 200 );
response.flushHeaders();
response.write( 'data: connected\n\n' );
} );
app.post( '/payments/webhook', ( req, res ) => {
// The webhook stripe sends data to
} );
};

View File

@@ -0,0 +1,5 @@
{
"stripe": {
"APIKey": ""
}
}

View File

@@ -1,5 +1,7 @@
{ {
"init": true, "init": true,
"twoFA": "enhanced", "twoFA": "enhanced",
"db": "mysql" "db": "mysql",
"payments": "stripe",
"yourDomain": "http://localhost:8081"
} }

View File

@@ -20,7 +20,8 @@
"mysql": "^2.18.1", "mysql": "^2.18.1",
"nodemailer": "^6.9.3", "nodemailer": "^6.9.3",
"serve-favicon": "^2.5.0", "serve-favicon": "^2.5.0",
"serve-static": "^1.15.0" "serve-static": "^1.15.0",
"stripe": "^12.14.0"
}, },
"devDependencies": { "devDependencies": {
"acorn": "^8.8.2", "acorn": "^8.8.2",
@@ -236,6 +237,11 @@
"tslib": "^2.4.0" "tslib": "^2.4.0"
} }
}, },
"node_modules/@types/node": {
"version": "20.4.4",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.4.4.tgz",
"integrity": "sha512-CukZhumInROvLq3+b5gLev+vgpsIqC2D0deQr/yS1WnxvmYLlJXZpaQrQiseMY+6xusl79E04UjWoqyr+t1/Ew=="
},
"node_modules/abbrev": { "node_modules/abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@@ -2211,6 +2217,18 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/stripe": {
"version": "12.14.0",
"resolved": "https://registry.npmjs.org/stripe/-/stripe-12.14.0.tgz",
"integrity": "sha512-WrDlYH1p5jliY7uzSU5nLDY7OCIeRe6FkC0hhScpTGwMthP/Muk38WXGeggjDHKeXAGCs43jUheZ7Ud/NEAJdg==",
"dependencies": {
"@types/node": ">=8.1.0",
"qs": "^6.11.0"
},
"engines": {
"node": ">=12.*"
}
},
"node_modules/tar": { "node_modules/tar": {
"version": "6.1.13", "version": "6.1.13",
"resolved": "https://registry.npmjs.org/tar/-/tar-6.1.13.tgz", "resolved": "https://registry.npmjs.org/tar/-/tar-6.1.13.tgz",

View File

@@ -53,7 +53,8 @@
"mysql": "^2.18.1", "mysql": "^2.18.1",
"nodemailer": "^6.9.3", "nodemailer": "^6.9.3",
"serve-favicon": "^2.5.0", "serve-favicon": "^2.5.0",
"serve-static": "^1.15.0" "serve-static": "^1.15.0",
"stripe": "^12.14.0"
}, },
"scripts": { "scripts": {
"test": "test.js" "test": "test.js"