Created
May 21, 2018 14:28
-
-
Save davidalves1/664c76d2afc275a4e595ea852eafbcb7 to your computer and use it in GitHub Desktop.
Comunicar com o webservice da Celcoin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const router = express.Router(); | |
const Celcoin = require('../controllers/celcoin'); | |
router.get('/', (req, res) => res.json({message: 'Welcome to our API! :)'})); | |
// Receive sample: {ddd: 11} | |
router.post('/celcoin/operators', (req, res, next) => { | |
const celcoin = new Celcoin(); | |
const ddd = req.body.ddd; | |
celcoin.getOperatorsDDD(ddd) | |
.then(result => { | |
console.log('sucesso'); | |
res.json(result); | |
}) | |
.catch(err => { | |
console.log('erro'); | |
res.json(err); | |
}); | |
}); | |
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const soap = require('strong-soap').soap; | |
class Celcoin { | |
getOperatorsDDD(dddValue) { | |
return new Promise((resolve, reject) => { | |
const wsdlUri = 'http://hmlgtodaconta.is2b.com.br:54003/TodaConta/WebService?wsdl'; | |
const options = { | |
wsdl_headers: { | |
'SOAPAction': 'http://GatewayWebService/IGatewayWeb/ProcessaTransacao', | |
'Content-Type': 'text/xml' | |
}, | |
envelopeKey: 's' | |
}; | |
const args = { | |
transacao: { | |
// Set attributes to node | |
$attributes: { | |
$xsiType: { | |
type: 'TransacaoConsultaOperadoraDDD', | |
xmlns: 'http://schemas.datacontract.org/2004/07/TodaConta.WebService.Transacoes' | |
}, | |
}, | |
CpfCnpj: '39233281922', | |
PontoAtendimento: { | |
Login: 'teste', | |
Senha: 'teste' | |
}, | |
TipoTransacao: 'CONSULTAOPERADORADDD', | |
CategoriaRecarga: 'TODOS', | |
TipoRecarga: 'ONLINE', | |
ddd: dddValue | |
} | |
} | |
soap.createClient(wsdlUri, options, function(err, client) { | |
const method = client['GatewayWeb']['BasicHttpBinding_IGatewayWeb']['ProcessaTransacao']; | |
method(args, function(err, result, envelope, soapHeader) { | |
if (err) reject(err); | |
const { CodigoErro, MensagemErro, Operadoras } = result.ProcessaTransacaoResult; | |
if (CodigoErro !== '000') { | |
reject({ | |
error: true, | |
code: CodigoErro, | |
message: MensagemErro | |
}); | |
} | |
const operadoras = Operadoras.Operadora.map(item => { | |
return { | |
id: parseInt(item.OperadoraId), | |
name: item.Nome, | |
max: parseFloat(item.ValorMax), | |
min: parseFloat(item.ValorMin) | |
} | |
}); | |
resolve(operadoras); | |
}); | |
}); | |
}); | |
} | |
} | |
module.exports = Celcoin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment