require('dotenv').config({path: '../.env_homologation'})
console.log(process.env.GN_CLIENT_KEY_PROD)
const urlApiProduction = '
https://api-pix.gerencianet.com.br'
const urlApiStaging = '
https://api-pix-h.gerencianet.com.br'
const https = require('https') //conexão segura para enviar dados
const axios = require('axios')
const fs = require('fs')//ler arquivos no disco
const baseURL = process.env.GN_ENV === 'production' ? urlApiProduction : urlApiStaging
const getToken = async() =>{
//console.log('getToken', process.env.GN_CLIENT_KEY_PROD)
const certificate = fs.readFileSync('../' + process.env.GN_CERTIFICATE)
const credentials ={
client_id: process.env.GN_CLIENT_KEY_PROD,
client_secret: process.env.GN_CLIENT_SECRET_PROD
}
const data = JSON.stringify({grant_type: 'client_credentials'})//o que será retornado da api
const dataCredentials = credentials.client_id + ':' + credentials.client_secret
const auth = Buffer.from(dataCredentials).toString('base64')
const agent = new https.Agent({
pfx: certificate,
passphrase: '',
})
const config ={
method: 'POST',
url: baseURL + '/oauth/token',
headers: {
Authorization: 'Basic' + auth,
'Content-Type': 'application/json'
},
httpsAgent: agent,
data: data,
}
const result = await axios(config)
console.log(result.data)
}
getToken()