NodeJS Express Session gets empty after successful payment redirection

Viewed 115

I have integrated a payment method in one of my eCommerce site

Now when i do a payment after payment successfully done it redirects on mentioned URL in request but when it come to my URL all the sessions stored on the platform gets cleared

Here is the session configuration for the session In node express with SQL session table

let session = require('express-session');
let MySQLStore = require('express-mysql-session')(session);

let db = require('./knexfile.js');
let options = db[environment]['connection'];
let sessionStore = new MySQLStore(options);

app.use(session({
    secret: 'ecommerceSiteSecrets',
    cookie: {
        maxAge: 365 * 24 * 60 * 60 * 1000,
        expires: false
    },
    store: sessionStore,
    saveUninitialized: true,
    resave: true,
}));

Now Before i do a payment My session is as below

Session {
  cookie:
   { path: '/',
     _expires: false,
     originalMaxAge: false,
     httpOnly: true },
  flash: {},
  customer_id: 2,
  email: 'tt1@gmail.com',
  name: 'Alex Stuart',
  type: 'customer',
  user:
   { customer_id: 2,
     customer_first_name: 'Alex',
     customer_last_name: 'Stuart',
     customer_email: 'tt1@gmail.com',
     customer_status: 1 
   },
  orderSession:
   { cart_id: '348,',
     customer_id: '2',
     amount: '225',
     pay_method: '1' 
   } 
}

And after successfull payment done payment gateway redirect me to the success page where i am suppose to do the code for capturing payment information and storing it in the DB

Session {
  cookie:
   { path: '/',
     _expires: false,
     originalMaxAge: false,
     httpOnly: true 
   } 
}

Can please guide me where am i doing a mistake !

1 Answers

Try to use node-sessionstorage method. Where you can set your required session data before making payment request and after payment done you need get session storage for you session and re-update your node session.

For e.g. take for session and save it in session storage

  customer_id: 2,
  email: 'tt1@gmail.com',
  name: 'Alex Stuart',
  type: 'customer',
  user:
   { customer_id: 2,
     customer_first_name: 'Alex',
     customer_last_name: 'Stuart',
     customer_email: 'tt1@gmail.com',
     customer_status: 1 
   },
  orderSession:
   { cart_id: '348,',
     customer_id: '2',
     amount: '225',
     pay_method: '1' 
   } 

You can use below method for reference -

const storage = require('node-sessionstorage');
storage.setItem('userSession', 'YourSessionArray');
console.log('item set:', storage.getItem('userSession'));
Related