plxdcma_eshop
Version:
Instala todo lo necesario para una tienda virtual
151 lines (137 loc) • 4.89 kB
JavaScript
//https://www.geeksforgeeks.org/how-to-integrate-paypal-in-node/
const paypal = require('paypal-rest-sdk');
var pool;
var _appurl;
function installDefault(app,appurl,client_id,client_secret,successf,cancelf,mode,dbconection){
//http://localhost:3000
pool=dbconection
_appurl=appurl
paypal.configure({
'mode': mode, // live
'client_id': client_id,
'client_secret': client_secret
});
app.get('/ppl/cc', (req, res) => {
cancelf(req, res)
});
app.get('/ppl/sc', (req, res) => {
const payerId = req.query.PayerID;
const paymentId = req.query.paymentId;
pool.query(`select * from paypaltransaction where ?`,{transactionid:paymentId},function(er2,result){
console.log(`payerId ${payerId} paymentId ${paymentId}`)
if(result.length == 0){
cancelf()
return
}
var rx=result[0]
var fresultId=rx.id
var total=rx.total
var currency=rx.currency
const execute_payment_json = {
"payer_id": payerId,
"transactions": [{
"amount": {
"currency": currency,
"total": total
}
}]
};
paypal.payment.execute(paymentId,
execute_payment_json,
function (error, payment) {
if (error) {
console.log(error.response);
throw error;
} else {
pool.query(`update paypaltransaction set dateatsuccs = now() where id = ${fresultId}`)
successf(req, res,fresultId,rx)
}
});
})
});
}
// app
// http://localhost:3000
//
module.exports.asSandBox=function(app,appurl,yourclientid,yoursecretkey,successFunction,cancelFunction,dbconection){
installDefault(app,appurl,yourclientid,yoursecretkey,successFunction,cancelFunction,"sandbox",dbconection)
}
module.exports.asProduction=function(app,appurl,yourclientid,yoursecretkey,successFunction,cancelFunction,dbconection){
installDefault(app,appurl,yourclientid,yoursecretkey,successFunction,cancelFunction,"live",dbconection)
//app,appurl,scF,cnF,mode,client_id,client_secret,successf,cancelf
}
module.exports.pay=function(items,amount,descripcion,emisorid,conclusion){
/*
{
"name": "Red Sox Hat",
"sku": "001",
"price": "25.00",
"currency": "USD",
"quantity": 1
}
//--
//
amount:{
"currency": "USD",
"total": "25.00"
}
*/
var internalsids=[]
for(var each in items){
internalsids.push(items[each].internalid)
delete items[each].internalid;
}
console.log(`internalsids internalsids`)
console.log(internalsids)
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": `${_appurl}/ppl/sc`,
"cancel_url": `${_appurl}/ppl/cc`
},
"transactions": [{
"item_list": {
"items": items
},
"amount": amount,
"description": descripcion
}]
};
console.log(`create_payment_json`)
console.log(create_payment_json)
console.log(`transactions`)
console.log(create_payment_json.transactions)
paypal.payment.create(
create_payment_json,
function (error, payment) {
if (error) {
throw error;
} else {
for (let i = 0; i < payment.links.length; i++) {
if (payment.links[i].rel === 'approval_url') {
//res.redirect(payment.links[i].href);
console.log(`payment at post`)
console.log(payment)
pool.query(`insert into paypaltransaction(transactionid,dateatpost,total,currency,descripcion,emisor)value('${payment.id}',now(),'${amount.total}','${amount.currency}','${descripcion}',${emisorid})`,function(er,r){
console.log(`er - er`)
console.log(er)
var ars=[]
for(var each in items){
var itm=items[each]
console.log(`internalsids[each] `)
console.log(internalsids[each])
ars.push([r.insertId,internalsids[each],itm.name,itm.sku,itm.price,itm.currency,itm.quantity])
}
pool.query(`insert into paypaltransaction_items(transactionid,itemid,itemname,sku,precio,currency,cuantity) values ? `,[ars],function(e,r2){
console.log(e)
conclusion(payment.links[i].href,r.insertId,r2)
})
})
}
}
}
});
}