258 lines
7.9 KiB
JavaScript
258 lines
7.9 KiB
JavaScript
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 {decode} from 'html-entities';
|
|
//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 "www5" -- www5
|
|
// pm2 start server.js --name "www10" -- www10
|
|
// pm2 delete www15;pm2 start server.js --name www15 -- www15;pm2 logs www --lines 1000
|
|
// pm2 logs --lines 1000
|
|
// /home/cradle2careertxxyz/websites/www10/portal/dashboard/system/scripts
|
|
// /home/cradle2careertxxyz/websites/www10/portal/dashboard/services/paypal/node/advanced-integration
|
|
|
|
//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);
|
|
console.log(subdomain);
|
|
|
|
function createMySQLConnection(){
|
|
return mysql.createConnection({
|
|
host: process.env.MY_HOSTNAME,
|
|
user: process.env.MY_USER,
|
|
password: process.env.MY_PASSWORD,
|
|
database: process.env.MY_DATABASE,
|
|
insecureAuth : true
|
|
});
|
|
}
|
|
|
|
let con = createMySQLConnection();
|
|
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){
|
|
let managed_domain = {};
|
|
for(let i=0; i < results.length; i++){
|
|
//console.log(results[i].SubDomain + " : " + results[i].PropertyName + " : " + results[i].PropertyValue);
|
|
managed_domain[results[i].PropertyName] = results[i].PropertyValue;
|
|
}
|
|
|
|
con.query(`SELECT * FROM configurations WHERE category="processors"`, function (err1, results1) {
|
|
if (err1) throw err1;
|
|
con.end();
|
|
if(results1.length > 0){
|
|
let processors = [];
|
|
for(let n=0; n < results1.length; n++){
|
|
let processor = results1[n];
|
|
processor.json = JSON.parse(decode(processor.json));
|
|
if(processor.json.processor=="paypal"){
|
|
processors.push(processor);
|
|
}
|
|
}
|
|
run(managed_domain, processors);
|
|
}else{
|
|
console.log(`No active server for subdomain ${subdomain}`);
|
|
}
|
|
|
|
});
|
|
}else{
|
|
console.log(`Error starting server! No port number found for subdomain ${subdomain}`);
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
function run(managed_domain, processors){
|
|
// function run(opts,opts1){
|
|
// console.log(typeof opts1[0].json);
|
|
// console.log(opts1[0].json.port);
|
|
|
|
let active_paypal = null;
|
|
for(let n=0; n < processors.length; n++){
|
|
if(processors[n].json.active==true){
|
|
active_paypal = processors[n];
|
|
break;
|
|
}
|
|
}
|
|
|
|
console.log(active_paypal);
|
|
|
|
if(active_paypal==null){
|
|
console.log("No active paypal service");
|
|
return;
|
|
}
|
|
console.log("Starting Paypal Server on port: " + active_paypal.json.service_port);
|
|
|
|
var privateKey = fs.readFileSync(managed_domain.CertPrivateKeyLocation, 'utf8');
|
|
var certificate = fs.readFileSync(managed_domain.CertPublicKeyLocation, 'utf8');
|
|
|
|
var credentials = {key: privateKey, cert: certificate};
|
|
var app = express();
|
|
|
|
// var clientId = process.env.CLIENT_ID;
|
|
let clientId = active_paypal.json.client_id;
|
|
let clientSecret = active_paypal.json.client_secret;
|
|
let base = "";
|
|
let mode = active_paypal.json.mode;
|
|
if(mode=="sandbox"){
|
|
base = "https://api-m.sandbox.paypal.com";
|
|
}else{
|
|
base = "https://api-m.paypal.com";
|
|
}
|
|
|
|
// purchase service
|
|
app.get("/"+active_paypal.json.service_end_point, async (req, res) => {
|
|
|
|
console.log(base);
|
|
|
|
try {
|
|
//console.log("starting...");
|
|
if(req.query.type=="check"){
|
|
const clientToken = await paypal.generateClientToken(base, clientId, clientSecret);
|
|
res.jsonp({action:true, token:clientToken, base:base});
|
|
}else if(req.query.type=="generate_token"){
|
|
console.log("Generating token...");
|
|
const clientToken = await paypal.generateClientToken(base, clientId, clientSecret);
|
|
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(base, data, clientId, clientSecret);
|
|
res.jsonp(order);
|
|
}else if(req.query.type=="capture_order"){
|
|
const orderID = req.query.orderID;
|
|
const captureData = await paypal.capturePayment(base, orderID, clientId, clientSecret);
|
|
res.jsonp(captureData);
|
|
}else if(req.query.type=="refund"){
|
|
const orderID = req.query.orderID;
|
|
const captureData = await paypal.refundPayment(base, clientId, clientSecret, orderID);
|
|
res.jsonp(captureData);
|
|
}else if(req.query.type=="payout"){
|
|
// var data = JSON.parse( req.query.data );
|
|
const captureData = await paypal.payout(base, clientId, clientSecret, data);
|
|
res.jsonp(captureData);
|
|
}else if(req.query.type=="get_product_plan"){
|
|
var data = JSON.parse( req.query.data );
|
|
const captureData = await get_product_plan(base,clientId,clientSecret,data);
|
|
res.jsonp(captureData);
|
|
}
|
|
} catch (err) {
|
|
|
|
|
|
console.log(err.message)
|
|
res.jsonp(JSON.stringify({
|
|
err: err.message,
|
|
clientId: clientId,
|
|
clientSecret: clientSecret,
|
|
active_paypal: active_paypal
|
|
}));
|
|
}
|
|
});
|
|
|
|
app.get("/check", async (req, res) => {
|
|
try {
|
|
if(req.query.type=="generate_token"){
|
|
console.log("Generating token...");
|
|
const clientToken = await paypal.generateClientToken(base, clientId, clientSecret);
|
|
res.jsonp(JSON.stringify({clientToken:clientToken,clientId:clientId}));
|
|
}
|
|
} catch (err) {
|
|
console.log(err.message)
|
|
res.jsonp(err.message);
|
|
}
|
|
});
|
|
|
|
// webhooks
|
|
app.get("/webhook_events_listener", async (req, res) => {
|
|
|
|
const accessToken = await paypal.generateAccessToken(base, clientId,clientSecret);
|
|
|
|
/*
|
|
"transmission_id": "69cd13f0-d67a-11e5-baa3-778b53f4ae55",
|
|
"transmission_time": "2016-02-18T20:01:35Z",
|
|
"cert_url": "cert_url", // PAYPAL-CERT-URL
|
|
"auth_algo": "SHA256withRSA", // PAYPAL-AUTH-ALGO
|
|
*/
|
|
|
|
console.log("=========================")
|
|
|
|
let data = {
|
|
webhook_id: "",// The ID of the webhook as configured in your Developer Portal account.
|
|
transmission_id: req.get('PAYPAL-TRANSMISSION-ID'),
|
|
transmission_time: req.get('PAYPAL-TRANSMISSION-TIME'),
|
|
transmission_sig: req.get('PAYPAL-TRANSMISSION-SIG'),
|
|
|
|
cert_url: req.get('PAYPAL-CERT-URL'),
|
|
auth_algo: req.get('PAYPAL-AUTH-ALGO'),
|
|
}
|
|
|
|
// let fileloc = "/home/cradle2careertxorg/websites/www15/portal/admin/services/paypal/node/advanced-integration";
|
|
// fs.writeFile(fileloc + "/test", JSON.stringify(data, null, 4), function(err) {
|
|
// if(err) {
|
|
// return JSON.stringify(err, null, 4);
|
|
// }
|
|
|
|
// });
|
|
|
|
// Aa-hUI-QfmPnt7vNwhqkoSdjE0UuK0wFe-neDJiuIfnOEXhGl-Fx8Qf1hxaDqxsw_JS5ta6ew2boAoTR
|
|
// EBaL2WenKH5x028AK_WukCFOzvwRaOtIgTgVSlN44xBRwKcvCYYr0cT4JOTDQK1CVlaJ-QM_b-d_bMYu
|
|
|
|
// console.log(accessToken);
|
|
|
|
|
|
res.send(accessToken);
|
|
|
|
|
|
// let response = await paypal.verifyWebHook(base);
|
|
|
|
|
|
|
|
});
|
|
|
|
// var httpServer = http.createServer(app);
|
|
var httpsServer = https.createServer(credentials, app);
|
|
|
|
//httpServer.listen(9888);
|
|
httpsServer.listen(active_paypal.json.service_port);
|
|
}
|
|
|
|
// 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"
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|