diff --git a/api/controller/cardsController.js b/api/controller/cardsController.js index da62d95..d6e4112 100644 --- a/api/controller/cardsController.js +++ b/api/controller/cardsController.js @@ -2,10 +2,37 @@ //equire('../../package.json') const cards = require('../../service/cards.js'); - +const CARD_REQUESTED =0; +const CARD_HOLDER_CREATED = 10; +const CARD_OWNER_ACTIVE = 30; +const CARD_ASSIGNED = 40; //const logger = require('../../app/logger'); const cardsControllers = { + createApplications: function(req, res){ + cards.createCardHolder(req, res, function (err, result) { + res.status(200).json({ + 'status': 'OK', + 'internal_return': 0, + 'result_list': result.result, + 'total_record': result.total_record + }) + }).then(r =>{ + console.log(r); + }); + }, + createCardHolder: function(req, res){ + cards.createCardHolder(req, res, function (err, result) { + res.status(200).json({ + 'status': 'OK', + 'internal_return': 0, + 'result_list': result.result, + 'total_record': result.total_record + }) + }).then(r =>{ + console.log(r); + }); + }, createCard: function (req, res) { cards.createCard(req, res, function (err, result) { res.status(200).json({ @@ -18,7 +45,9 @@ const cardsControllers = { console.log(r); }); }, + acceptCardTerms: function (req, resp){ + }, activateCard: function (req, res) { cards.activateCard(req, res, function (err, dist) { if (err) { diff --git a/api/routes/route.js b/api/routes/route.js index ed7737d..48bac13 100644 --- a/api/routes/route.js +++ b/api/routes/route.js @@ -4,8 +4,14 @@ const cardsControllers = require('../controller/cardsController.js'); module.exports = function(app) { + app.route('/cardapplications') + .post(cardsControllers.createApplications); + app.route('/createCardHolder') + .post(cardsControllers.createCardHolder); app.route('/create') .post(cardsControllers.createCard); + app.route('/create') + .post(cardsControllers.acceptCardTerms); app.route('/activate') .post(cardsControllers.activateCard); // app.route('/eventSendMoney') diff --git a/service/cards.js b/service/cards.js index 28c19a1..cb729e1 100644 --- a/service/cards.js +++ b/service/cards.js @@ -6,6 +6,131 @@ const logger = require('../app/logger'); //const User = require("../app/model/cardModel.js"); const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); var cards = { + createApplications: async function(req, res, next){ + try { + logger.info(req.body); + var data = { + "status": req.body.status, + "limit": req.body.limit, + }; + let Qstring = " SELECT id AS request_id, uid AS request_uid, * FROM members_card_request WHERE status = "+data.status+" LIMIT "+data.limit; + logger.info(Qstring); + db.query(Qstring, async function (err, result) { + + if (err) throw err; + logger.info(result.rows); + let resultItem ={ + "result": result.rows, + "total_record": result.rows.length + } + next(null, resultItem); // pass control to the next handler + }); + } catch (error) { + await res.status(500).json({error: `Internal Server error 002 ${error} `}); + } + }, + createCardHolder: async function (req, res, next) { + try { + logger.info(req.body); + var data = { + "request_uid": req.body.request_uid, + "request_id": req.body.request_id, + }; + + let Qstring = " SELECT r.*,m.firstname,m.lastname,m.email FROM members_card_request r " + + " LEFT JOIN members m ON m.id=r.member_id " + + " WHERE r.uid::text = '" + data.request_uid +"'"; + logger.info(Qstring); + db.query(Qstring, async function (err, result) { + try { + if (err) throw err; + + const req_data = { + "name": result.rows[0].firstname.toString() + ' ' + result.rows[0].firstname.toString(), + "firstname": result.rows[0].firstname, + "lastname": result.rows[0].lastname, + "phone_number": result.rows[0].phone_number, + "email": result.rows[0].email, + "address": result.rows[0].address, + "city": result.rows[0].city, + "state": result.rows[0].state, + "postal_code": result.rows[0].postal_code, + "country": result.rows[0].country, + "dob_day": result.rows[0].dob_day, + "dob_month": result.rows[0].dob_month, + "dob_year": result.rows[0].dob_year, + "member_uid" : result.rows[0].member_uid, + "member_id" : result.rows[0].member_id, + }; + + logger.info("READY FOR STRIPE CALL ",req_data); + logger.info("READY FOR STRIPE CALL ************************************************************************ AFTER "); + + const ReqParam = + { + name: req_data.name, + email: req_data.email, + phone_number: req_data.phone_number, + status: 'active', + type: 'individual', + individual: { + first_name: req_data.firstname, + last_name: req_data.lastname, + dob: { + day: req_data.dob_day, + month: req_data.dob_month, + year: req_data.dob_year + }, + }, + billing: { + address: { + line1: req_data.address, + city: req_data.city, + state: req_data.state, + postal_code: req_data.postal_code, + country: req_data.country, + }, + }, + }; + + logger.info("REQ=>", ReqParam); + stripe.issuing.cardholders.create(ReqParam).then((cardholder) => { + console.log(cardholder); + Qstring = " UPDATE members_card_request " + + " SET card_issue_id='"+cardholder.id+"' , status = 10, updated = now() " + + " WHERE uid::text = '" + data.request_uid +"'"; + db.query(Qstring, async function (err, result) { + console.log(cardholder); + Qstring = "UPDATE members SET " + + " stripe_cardholder_id = '"+cardholder.id+"' " + + " WHERE id ="+data.member_id+" AND uid::text = '"+data.member_uid+"'"; + db.query(Qstring, async function (err, result) { + + let resultItem = { + "result": cardholder, + "stripe_cardholder_id": cardholder.id, + "total_record": 0 + } + next(null, resultItem); // pass control to the next handler + }); + }); + + }).catch((error) => { + console.error(error); + let resultItem = { + "result": error, + "total_record": 0 + } + next(null, resultItem); + }); + } catch (e) { + next(e.message, null); // pass control to the next handler + } + }); + } catch (error) { + await res.status(500).json({error: `Internal Server error 002 ${error} `}); + } + }, createCard: async function (req, res, next) { try { logger.info(req.body); @@ -133,16 +258,56 @@ var cards = { await res.status(500).json({error: `Internal Server error 002 ${error} `}); } + }, + acceptCardTerms: function (req, res, next) { - // let resultItem ={ - // "result": bannerArray, - // "total_record": 4 - // } - // next(null, resultItem ); // pass control to the next handler + const aggrement_ip = '91.121.146.224'; + const aggrement_date = Date.now(); + //const cardholder = await + stripe.issuing.cardholders.update( + cardholder.id, + { + individual: { + card_issuing: { + user_terms_acceptance: { + date: aggrement_date, + ip: aggrement_ip, + }, + }, + }, + } + ).then((cardConfirm)=>{ + + const card = stripe.issuing.cards.create({ + cardholder: cardholder.id, + currency: 'usd', + type: 'virtual', + }); + + let resultItem = { + "result_confirm": cardConfirm, + "total_record": 0 + } + next(null, resultItem); // pass control to the next handler + console.log(cardConfirm); + }); + + + let resultItem ={ + "result": [], + "total_record": 0 + } + next(null, resultItem ); // pass control to the next handler }, activateCard: function (req, res, next) { + const card = stripe.issuing.cards.create({ + cardholder: cardholder.id, + currency: 'usd', + type: 'virtual', + status: 'active', + }); let resultItem ={ "result": [],