I have a url that redirects to another url after payment success. How can I get the parameters from the redirected url to update the database?
Example of redirected url:
http://localhost:8888/success.html?id=3LS234170M9827257
Insert “3LS234170M9827257” into the database.
Currently I assume it is here: but it is not working.
app.get("/success", function (req, res) {
const user_id = req.query.id;
res.send(req.query.id);
var sql = "INSERT INTO records (transid) VALUES (id)";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
I need help in getting the id from the redirected url parameters and inserting into the database.
Server.js
import express from "express";
import * as paypal from "./paypal-api.js";
import mysql from "mysql";
const {PORT = 8888} = process.env;
const app = express();
app.use(express.static("public"));
// parse post params sent in body in json format
app.use(express.json());
var mysqlConnection = mysql.createConnection({
host: "localhost",
user: "xxx",
password: "xxx",
database:"xxx"
});
mysqlConnection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
app.post("/my-server/create-paypal-order", async (req, res) => {
try {
const order = await paypal.createOrder();
res.json(order);
} catch (err) {
res.status(500).send(err.message);
}
});
app.post("/my-server/capture-paypal-order", async (req, res) => {
const { orderID } = req.body;
try {
const captureData = await paypal.capturePayment(orderID);
res.json(captureData);
} catch (err) {
res.status(500).send(err.message);
}
});
app.get("/success", function (req, res) {
const user_id = req.query.id;
res.send(req.query.id);
});
app.listen(PORT, () => {
console.log(`Server listening at http://localhost:${PORT}/`);
});