UNPKG

sdk-payway-ventasonline

Version:

Payway SDK NODEJS ===============

734 lines (660 loc) 28 kB
var sdkModulo = require('../lib/sdk'); var healthcheckMod = require('../lib/healthcheck'); var paymentMod = require('../lib/payment'); var partialRefundMod = require('../lib/partial_refund'); var refundMod = require('../lib/refund'); var paymentInfoMod = require('../lib/payment_info'); var validateMod = require('../lib/validate'); var tokensMod = require('../lib/tokens'); var getAllPaymentsMod = require('../lib/all_payments'); var cardTokensMod = require('../lib/card_token'); var deleteCardTokenMod = require('../lib/delete_cardToken'); var deletePartialRefMod = require('../lib/delete_partialRefund'); var deleteRefMod = require('../lib/delete_refund'); var Client = require('node-rest-client').Client; var Promise = require('promise'); var TokenDataModulo = require('../lib/token_data.js'); var PaymentDataModulo = require('../lib/payment_data.js'); var retailModulo = require('../lib/fraud_detection/cs_retail.js'); const { PRIVATE_API_KEY, PUBLIC_API_KEY, ENDPOINT_DEVELOPER } = require('./constants'); // SE INSTANCIA SDK DECIDIR 2 var sdk = new sdkModulo.sdk('developer', PUBLIC_API_KEY, PRIVATE_API_KEY); ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //EJEMPLO DE FUNCIONALIDADES //exampleHealthCheck(sdk); //examplePayment(sdk); //examplePartialRefund(sdk); //exampleRefund(sdk); //examplePaymentInfo(sdk); //exampleGetAllPayments(sdk); //exampleCardTokens(sdk); //examplePayment_Tokenized(sdk); //exampleDeleteTokenizedCard(sdk); //exampleDeletePartialRefund(sdk); //exampleDeleteRefund(sdk); //exampleValidate(sdk); //examplePaymentBsa(sdk) //exampleTokens(sdk); // EJEMPLO ESTADO DEL SERVICIO function exampleHealthCheck(sdk) { args = {} var instHealthCheck = new healthcheckMod.healthcheck(sdk, args).then(function(resp) { console.log(resp) }) }; //EJEMPLO VALIDATE function exampleValidate(sdk) { //************************* return new Promise(function(resolve, reject) { //SE OBTIENE UN TOKEN DE PAGO var date = new Date().getTime(); args = { site_transaction_id: ""+date, user_id: 'juanpepito', email: "mauricio.ghiorzi@softtek.com", payment_method_id: 1, bin: "450799", amount: 25.50, currency: "ARS", installments: 1, description: "Description of product", payment_type: "single", sub_payments: [], apiKey: "aaaaaa1e430db94d436543523744", formSite: '00020555', 'Content-Type': "application/json", success_url: "https://shop.swatch.com/es_ar/", //si no se informa el "redirect_url" es requerido cancel_url: "https://swatch.com/api/result", redirect_url: "", //si no se informa el success_url es requerido fraud_detection: [], //si no esta activado cybersource no enviar este atributo }; var validateData = new validateMod.validate(args); // send_to_cs = TRUE O FALSE PARA ENVIAR PARAMETROS CS //Se envian sdk y parametros al modulos de validate que realizará el pago var instPayment = new validateMod.validate(sdk, args).then(function(result) { console.log("-----------------------------------------") console.log("Validate") console.log(result); console.log("-------------------***-------------------"); }) }) } function exampleTokens(sdk) { //************************* return new Promise(function(resolve, reject) { //SE OBTIENE UN TOKEN DE PAGO var date = new Date().getTime(); console.log('traza 1'); args = { "public_token": "4507994025297787", "volatile_encrypted_data": "YRfrWggICAggsF0nR6ViuAgWsPr5ouR5knIbPtkN+yntd7G6FzN/Xb8zt6+QHnoxmpTraKphZVHvxA==", "public_request_key": "12345678", "issue_date": "123123123123", "flag_security_code": "0", "flag_tokenization": "0", "flag_selector_key": "1", "flag_pei": "1", "card_holder_name": "Horacio", "card_holder_identification_type": "dni", "card_holder_identification_number": "23968498", "fraud_detection_device_unique_identifier": "12345", "apiKey": "aaaaaaaaaaaaaa0ec2452f5d67f13", "Content-Type": "application/json", }; var tokensData = new tokensMod.tokens(args); // send_to_cs = TRUE O FALSE PARA ENVIAR PARAMETROS CS //Se envian sdk y parametros al modulos de validate que realizará el pago var instPayment = new tokensMod.tokens(sdk, args).then(function(result) { console.log("-----------------------------------------") console.log("Tokens") console.log(result); console.log("-------------------***-------------------"); }) }) } // EJEMPLO REQUEST DE PAGO function examplePayment(sdk) { return new Promise(function(resolve, reject) { //SE OBTIENE UN TOKEN DE PAGO //exampleGetToken(sdk).then(function(token) { var date = new Date().getTime(); args = { site_transaction_id: "id_" + date, token: '3af53fe3-96b7-4c0d-a7db-abb8215fbfb9', user_id: 'juanpepito', payment_method_id: 1, bin: "450799", amount: 25.50, currency: "ARS", installments: 1, description: "Description of product", payment_type: "single", sub_payments: [], apiKey: PRIVATE_API_KEY, 'Content-Type': "application/json" }; var customer = { id: "juanpepito", email: "mauricio.ghiorzi@softtek.com" }; var paymentData = new PaymentDataModulo.paymentData(args); paymentData.setCustomer(customer); var args = paymentData.getJSON(); // send_to_cs = TRUE O FALSE PARA ENVIAR PARAMETROS CS var send_to_cs = true; if (send_to_cs == true) { var datos_cs = { send_to_cs: true, channel: 'Web/Mobile/Telefonica', city: 'Buenos Aires', country: 'AR', customer_id: 'martinid', email: 'accept@decidir.com.ar', first_name: 'martin', last_name: 'paoletta', phone_number: '1547766329', postal_code: '1427', state: 'BA', street1: 'GARCIA DEL RIO 4041', street2: 'GARCIA DEL RIO 4041' }; args.data.fraud_detection = datos_cs; } //Se envian sdk y parametros al modulos de payment que realizará el pago var instPayment = new paymentMod.payment(sdk, args).then(function(result) { console.log("") console.log("") console.log("Se realiza una petición de pago enviando el payload y el token de pago ") console.log("generado anteriormente") console.log("") console.log("") console.log(" PAYMENT REQUEST "); console.log("-----------------------------------------"); console.log("sendPaymentRequest result:"); console.log(result); console.log("-----------------------------------------"); console.log("sendPaymentRequest error:"); console.log(err); console.log("-------------------***-------------------"); }) //}) }) } // EJEMPLO PAGO BSA function examplePaymentBsa(sdk) { return new Promise(function(resolve, reject) { bsa_data = { public_token: 'Token', volatile_encrypted_data: 'VOLATILE_ENCRYPTED_DATA', public_request_key: 'publicRequestKey', ip_address: 'ipaddress', issue_date: 'TokenDate', flag_security_code: 'flag_code_check', flag_tokenization: 'flag_token', flag_selector_key: 'SelectorClaveFlag', flag_pei: "1", card_holder_name: 'DatosAdicionales - nombre', card_holder_identification: {"type": 'DatosAdicionales tipoDocumento', "number": 'DatosAdicionales numeroDocumento'}, fraud_detection: {"device_unique_identifier": "12345"} }; var date = new Date().getTime(); args = { site_transaction_id: "id_" + date, payment_mode: "bsa", card_token_bsa: bsa_data, token: 'fe8d1e34-2446-4405-bff5-e51fa90593eb', user_id: 'juanpepito', payment_method_id: 1, bin: "450799", amount: 25.50, currency: "ARS", installments: 1, description: "Description of product", payment_type: "single", sub_payments: [], apiKey: PRIVATE_API_KEY, 'Content-Type': "application/json" }; var customer = { id: "juanpepito", email: "mauricio.ghiorzi@softtek.com" }; var paymentData = new PaymentDataModulo.paymentData(args); paymentData.setCustomer(customer); var args = paymentData.getJSON(); // send_to_cs = TRUE O FALSE PARA ENVIAR PARAMETROS CS var send_to_cs = true; if (send_to_cs == true) { var datos_cs = { send_to_cs: true, channel: 'Web/Mobile/Telefonica', city: 'Buenos Aires', country: 'AR', customer_id: 'martinid', email: 'accept@decidir.com.ar', first_name: 'martin', last_name: 'paoletta', phone_number: '1547766329', postal_code: '1427', state: 'BA', street1: 'GARCIA DEL RIO 4041', street2: 'GARCIA DEL RIO 4041' }; args.data.fraud_detection = datos_cs; } //Se envian sdk y parametros al modulos de payment que realizará el pago var instPayment = new paymentMod.payment(sdk, args).then(function(result) { console.log("") console.log("") console.log("Se realiza una petición de pago enviando el payload y el token de pago ") console.log("generado anteriormente") console.log("") console.log("") console.log(" PAYMENT REQUEST "); console.log("-----------------------------------------"); console.log("sendPaymentRequest result:"); console.log(result); console.log("-----------------------------------------"); console.log("sendPaymentRequest error:"); console.log(err); console.log("-------------------***-------------------"); }) }) } //EJEMPLO DEVOLUCIÖN PARCIAL function examplePartialRefund(sdk) { //SE HACE UN PAGO PARA PODER REALIZAR LA DEVOLUCIÓN var instPayment = examplePaymentRequest(sdk).then(function(result) { paymentId = result.id; amount = 10.50; var args = { data: { amount: amount }, headers: { "apikey": PRIVATE_API_KEY, "Content-Type": "application/json", "Cache-Control": "" } }; setTimeout(function() { var instPartialRefund = new partialRefundMod.partialRefund(sdk, paymentId, args).then(function(resp) { console.log("") console.log("") console.log("Reintegro monto parcial de la transacción ") console.log("") console.log("") console.log(" PARTIAL REFUND "); console.log("-----------------------------------------"); console.log("partial refund result:"); console.log(resp); console.log("-----------------------------------------"); console.log("partial refund error:"); console.log(err); console.log("-------------------***-------------------"); }); }, 2000); }); } //EJEMPLO DE DEVOLUCIÓN TOTAL function exampleRefund(sdk) { //SE HACE UN PAGO PARA PODER REALIZAR LA DEVOLUCIÓN examplePaymentRequest(sdk).then(function(result) { paymentId = result.id; var args = { data: { }, headers: { "apikey": PRIVATE_API_KEY, "Content-Type": "application/json", "Cache-Control": "" } }; setTimeout(function() { var instPartialRefund = new partialRefundMod.partialRefund(sdk, paymentId, args).then(function(resp) { console.log("") console.log("") console.log("Reintegro monto total de la transacción") console.log("") console.log("") console.log(" REFUND "); console.log("-----------------------------------------"); console.log("refund result:"); console.log(resp); console.log("-----------------------------------------"); console.log("refund error:"); console.log(err); console.log("-------------------***-------------------"); }); }, 2000); }) }; //SE OBTIENE INFORMACION DE UN PAGO EN ESPECÍFICO function examplePaymentInfo(sdk) { examplePaymentRequest(sdk).then(function(result) { paymentId = result.id; var args = { data: { }, headers: { "apikey": PRIVATE_API_KEY, "Content-Type": "application/json", "Cache-Control": "no-cache" } }; setTimeout(function() { var paymentInfo = new paymentInfoMod.paymentInfo(sdk, paymentId, args).then(function(resp) { console.log(""); console.log(""); console.log("información de pago previamente realizado"); console.log(""); console.log(""); console.log(result); console.log("-----------------------------------------"); console.log("error:"); console.log(err); console.log("-------------------***-------------------"); }); }, 2000); }); }; //SE OBTIENE INFORMACION DE TODOS LOS PAGOS SEGUN LOS FILTROS QUE SE LE ENVÍEN COMO PARAMETROS function exampleGetAllPayments(sdk) { var args = { data: { }, headers: { "apikey": PRIVATE_API_KEY, "Content-Type": "application/json", "Cache-Control": "no-cache" } }; offset = '10'; pageSize = '20'; siteOperationId = '450799'; merchantId = 'Id001'; var allPayments = new getAllPaymentsMod.getAllPayments(sdk, args).then(function(resp) { console.log("-----------------------------------------"); console.log("infoPayments:"); console.log(resp); console.log("-----------------------------------------"); console.log("infoPayments error:"); console.log(err); console.log("-------------------***-------------------"); }); }; //SE OBTIENE TOKEN DE UNA TARJETA PREVIAMENTE TOKENIZADA function exampleCardTokens(sdk) { return new Promise(function(resolve, reject) { examplePaymentRequest(sdk).then(function(result) { user_id = result.user_id; var args = { data: { }, headers: { "apikey": PRIVATE_API_KEY, "Content-Type": "application/json", "Cache-Control": "no-cache" } }; setTimeout(function() { var cardTokens = new cardTokensMod.cardTokens(sdk, args).then(function(resp) { resolve(resp); console.log(""); console.log(""); console.log("Luego de realizar un primer pago se genera automaticamente un token único"); console.log("para la tarjeta"); console.log(""); console.log(""); console.log("-----------------------------------------"); console.log("cardTokens result:"); console.log(resp); console.log("-----------------------------------------"); console.log("cardTokens error:"); console.log(err); console.log("-------------------***-------------------"); }); }, 3500); }) }) }; //SE REALIZA UN PAGO CON TARJETA TOKENIZADA function examplePayment_Tokenized(sdk) { //SE OBTIENE TOKEN DE PAGO DE TARJETA TOKENIZADA exampleGetToken_Tokenized(sdk).then(function(token) { var date = new Date().getTime(); args = { site_transaction_id: "id_" + date, token: token, user_id: 'juanpepito', payment_method_id: 1, bin: "450799", amount: 2000, currency: "ARS", installments: 1, description: "Description of product", payment_type: "single", sub_payments: [], apiKey: PRIVATE_API_KEY, 'Content-Type': "application/json" }; var paymentData = new PaymentDataModulo.paymentData(args); var args = paymentData.getJSON(); var instPayment = new paymentMod.payment(sdk, args).then(function(result) { console.log("") console.log("") console.log("Se realiza una petición de pago enviando el payload y el token de pago ") console.log("de la tarjeta tokenizada") console.log("") console.log("") console.log(" PAYMENT REQUEST "); console.log("-----------------------------------------"); console.log("sendPaymentRequest result:"); console.log(result); console.log("-----------------------------------------"); console.log("sendPaymentRequest error:"); console.log(err); console.log("-------------------***-------------------"); }); }) } function exampleDeleteTokenizedCard(sdk) { args = { data: { }, headers: { apiKey: PRIVATE_API_KEY, 'Content-Type': "application/json", 'Cache-Control': "no-cache" } } var instDeleteCardToken = new deleteCardTokenMod.deleteCardToken(sdk, '4507990000004905', args).then(function(result) { console.log("------------ -----------------------------"); console.log("deleteCardToken result:"); console.log(result); console.log("-----------------------------------------"); console.log("deleteCardToken error:"); console.log(err); console.log("-------------------***-------------------"); }); } //ANULACIÓN DE LA DEVOLUCIÓN PARCIAL function exampleDeletePartialRefund(sdk) { examplePaymentRequest(sdk).then(function(result) { paymentId = result.id; amount = 10.50; var args = { data: { "amount": amount }, headers: { "apikey": PRIVATE_API_KEY, "Content-Type": "application/json", "Cache-Control": "" } }; setTimeout(function() { var instDeletePartialRef = new deletePartialRefMod.deletePartialRefund(sdk, paymentId, args).then(function(result) { console.log("") console.log("") console.log("Reintegro monto parcial de la transacción ") console.log("") console.log("") console.log(" PARTIAL REFUND "); console.log("-----------------------------------------"); console.log("partial refund result:"); console.log(result); console.log("-----------------------------------------"); console.log("partial refund error:"); console.log(err); console.log("-------------------***-------------------"); }); }, 2000); }); }; //ANULACIÓN DE LA DEVOLUCIÓN function exampleDeleteRefund(sdk) { examplePaymentRequest(sdk).then(function(result) { paymentId = result.id; var args = { data: { }, headers: { "apikey": PRIVATE_API_KEY, "Content-Type": "application/json", "Cache-Control": "" } }; setTimeout(function() { var instDeleteRef = new deleteRefMod.deleteRefund(sdk, paymentId, args).then(function(result) { console.log("") console.log("") console.log("Reintegro monto total de la transacción") console.log("") console.log("") console.log(" REFUND "); console.log("-----------------------------------------"); console.log("refund result:"); console.log(result); console.log("-----------------------------------------"); console.log("refund error:"); console.log(err); console.log("-------------------***-------------------"); }); }, 2000); }) }; // EXTRAS //exampleGetToken(sdk); //exampleGetToken_Tokenized(sdk); function exampleGetToken(sdk) { return new Promise(function(resolve, reject) { args = { card_number: '4507990000004905', card_expiration_month: '08', card_expiration_year: "20", security_code: "775", card_holder_name: "John Doe", type: "dni", number: "25123456", apiKey: PUBLIC_API_KEY, 'Content-Type': "application/json", 'Cache-Control': "no-cache" } var tokenData = new TokenDataModulo.tokenData(args); var args = tokenData.getJSON(); var client = new Client(); client.post(ENDPOINT_DEVELOPER + "/tokens", args, function(data, response) { resolve(data.id); if (Buffer.isBuffer(data)) { data = JSON.parse(data.toString('utf8')); //console.log(response); } }); }); } // PAGO CON TARJETA TOKENIZADA function exampleGetToken_Tokenized(token, security_code) { return new Promise(function(resolve, reject) { exampleCardTokens(sdk).then(function(result) { args = { token: result.tokens[0].token, security_code: '775', apiKey: PUBLIC_API_KEY, 'Content-Type': "application/json", 'Cache-Control': "no-cache" } var tokenData = new TokenDataModulo.tokenData(args); var args = tokenData.getJSON(); var client = new Client(); client.post(ENDPOINT_DEVELOPER + "/tokens", args, function(data, response) { resolve(data.id); var payToken = data.id; console.log("") console.log("") console.log("Se genera un token de pago enviando únicamente el id de la tarjeta tokenizada y el security_code") console.log("") console.log("") console.log("Token") console.log("-----------------------------------------"); console.log(payToken); console.log("-----------------------------------------"); console.log("-------------------***-------------------"); }); }); }); } function examplePaymentRequest(sdk) { return new Promise(function(resolve, reject) { exampleGetToken().then(function(token) { var date = new Date().getTime(); args = { site_transaction_id: "id_" + date, token: token, user_id: 'juanpepito', payment_method_id: 1, bin: "450799", amount: 25.50, currency: "ARS", installments: 1, description: "Description of product", payment_type: "single", sub_payments: [], apiKey: PRIVATE_API_KEY, 'Content-Type': "application/json" }; var paymentData = new paymentMod.paymentData(args); var args = paymentData.getJSON(); var datos_cs = { send_to_cs: 'true', channel: 'Web/Mobile/Telefonica', city: 'Buenos Aires', country: 'AR', customer_id: 'martinid', email: 'accept@decidir.com.ar', first_name: 'martin', last_name: 'paoletta', phone_number: '1547766329', postal_code: '1427', state: 'BA', street1: 'GARCIA DEL RIO 4041', street2: 'GARCIA DEL RIO 4041' } sdk.payment(args, function(result, err) { resolve(result); console.log("") console.log("") console.log("Se realiza una petición de pago enviando el payload y el token de pago ") console.log("generado anteriormente") console.log("") console.log("") console.log(" PAYMENT REQUEST "); console.log("-----------------------------------------"); console.log("sendPaymentRequest result:"); console.log(result); console.log("-----------------------------------------"); console.log("sendPaymentRequest error:"); console.log(err); console.log("-------------------***-------------------"); }); }); }); }