import { createRequire } from 'module'; import path from 'path'; import { fileURLToPath } from 'url'; const require = createRequire(import.meta.url); const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const fs = require('fs'); //import "dotenv/config"; import express from "express"; import http from "http"; import https from "https"; import * as paypal from "./paypal-api.js"; var mysql = require('mysql2'); // pm2 start server.js --name "www15" -- www15 // pm2 start server.js --name "www10" -- www10 // pm2 delete www15;pm2 start server.js --name "www15" -- www15;pm2 logs www15 --lines 1000 // /home/cradle2careertxxyz/websites/www15/portal/dashboard/system/scripts //process.argv.forEach(function (val, index, array) {console.log(index + ': ' + val);}); const subdomain = process.argv[2]; const mypath = path.join(__dirname, '../../../../system/db/.env'); require('dotenv').config({ path: mypath }) console.log(process.env.MY_DATABASE); console.log(mypath); var con = mysql.createConnection({ host: process.env.MY_HOSTNAME, user: process.env.MY_USER, password: process.env.MY_PASSWORD, database: process.env.MY_DATABASE, insecureAuth : true }); con.connect(function(err) { if (err) throw err; con.query(`SELECT * FROM subdomain_properties WHERE SubDomain = '${subdomain}'`, function (err, results) { if (err) throw err; con.end(); if(results.length > 0){ var obj = {}; for(var i=0; i < results.length; i++){ //console.log(results[i].SubDomain + " : " + results[i].PropertyName + " : " + results[i].PropertyValue); obj[results[i].PropertyName] = results[i].PropertyValue; } run(obj); }else{ console.log(`Error starting server! No port number found for subdomain ${subdomain}`); } }); }); function run(opts){ console.log("Starting Paypal Server on port: " + opts.PayPalPort); console.log(opts); var privateKey = fs.readFileSync(opts.CertPrivateKeyLocation, 'utf8'); var certificate = fs.readFileSync(opts.CertPublicKeyLocation, 'utf8'); var credentials = {key: privateKey, cert: certificate}; var app = express(); // render checkout page with client id & unique client token app.get("/"+opts.PayPalEndPoint, async (req, res) => { var clientId = process.env.CLIENT_ID; if(opts.PaypalClientId!=undefined){ clientId = opts.PaypalClientId; } try { //console.log("starting..."); if(req.query.type=="generate_token"){ console.log("Generating token..."); const clientToken = await paypal.generateClientToken(opts.PaypalClientId, opts.PaypalAppSecret); res.jsonp(JSON.stringify({clientToken:clientToken,clientId:clientId})); }else if(req.query.type=="create_order"){ console.log(req.query.data) var data = JSON.parse( req.query.data ); const order = await paypal.createOrder(data,opts.PaypalClientId, opts.PaypalAppSecret); res.jsonp(order); }else if(req.query.type=="capture_order"){ const orderID = req.query.orderID; const captureData = await paypal.capturePayment(orderID,opts.PaypalClientId, opts.PaypalAppSecret); res.jsonp(captureData); }else if(req.query.type=="refund"){ const orderID = req.query.orderID; const captureData = await paypal.refundPayment(orderID); res.jsonp(captureData); } } catch (err) { console.log(err.message) res.jsonp(err.message); } }); var httpServer = http.createServer(app); var httpsServer = https.createServer(credentials, app); //httpServer.listen(9888); httpsServer.listen(opts.PayPalPort); } // pm2 delete www15;pm2 start server.js --name "www15" -- www15;pm2 log --lines 200 www15 /* curl -v POST https://api.sandbox.paypal.com/v1/oauth2/token \ -H "Accept: application/json" \ -H "Accept-Language: en_US" \ -u "AbehsBM-Cg5UwIx6pAWObkiltltLPwN82QlhN-u4qwYMD7Fzly_mDmUGYnSJqHuMLIWjhBk9I5iKsvIN:EIMOBpKnMGNlgaofARDgpuigkvpoSilNUJQSXSmXhNRwxrVosOVp1DCpb7Ky2OfjX7rR0SqszgRMyg1-" \ -d "grant_type=client_credentials" */