Last active
December 3, 2020 13:50
-
-
Save VinnyLima/d1d26f3cc3409c328190aa0722f6371e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import FormData from 'form-data'; | |
import JunoToken from '../models/JunoToken.Model'; | |
import JunoApi from '../providers/PaymentProviders/Juno.Providers'; | |
import JunoApiAuth from '../providers/PaymentProviders/JunoAuthSand.Providers'; | |
import encodeBase64 from 'nodejs-base64-encode'; | |
import qs from 'querystring'; | |
const generateJunoToken = async () => { | |
/*const tokenExists = await JunoToken.query().modify('activeToken'); | |
if (!!tokenExists) { | |
const active_token = tokenExists.access_token; | |
JunoApi.defaults.headers.authorization = `Bearer ${active_token}`; | |
return active_token; | |
}*/ | |
console.log(process.env.JUNO_ENV === 'development' ? process.env.JUNO_CLIENT_ID_SAND : process.env.JUNO_CLIENT_ID); | |
console.log(process.env.JUNO_ENV === 'development' ? process.env.JUNO_CLIENT_SECRET_SAND : process.env.JUNO_CLIENT_SECRET); | |
console.log(process.env.JUNO_ENV === 'development' ? process.env.JUNO_URL_AUTH_SAND : process.env.JUNO_URL); | |
if (process.env.JUNO_ENV === 'development') { | |
const encodedToken = encodeBase64.encode(`${process.env.JUNO_CLIENT_ID_SAND}:${process.env.JUNO_CLIENT_SECRET_SAND}`, 'base64' | |
); | |
console.log(encodedToken); | |
const requestBody = { | |
grant_type: 'client_credentials', | |
}; | |
const config = { | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
Authorization: `Basic ${encodedToken}`, | |
}, | |
}; | |
const response = await JunoApiAuth.post( | |
'authorization-server/oauth/token', | |
qs.stringify(requestBody), | |
config, | |
).catch(err => { | |
console.log(err.response); | |
//console.log("ERRO DO Paulo: " + err) | |
}); | |
//const { access_token } = response.data; | |
return; | |
} else { | |
const formData = new FormData(); | |
formData.append('grant_type', 'client_credentials'); | |
const response = await JunoApi.post( | |
'authorization-server/oauth/token', | |
formData, | |
{ | |
auth: { | |
username: process.env.JUNO_CLIENT_ID, | |
password: process.env.JUNO_CLIENT_SECRET, | |
}, | |
headers: { ...formData.getHeaders() }, | |
} | |
).catch(err => { | |
console.log(err.response); | |
//console.log("ERRO DO LEANDRO: " + err) | |
}); | |
const { access_token: active_token, expires_in } = response.data; | |
const expire_datetime = new Date(); | |
expire_datetime.setSeconds(expire_datetime.getSeconds() + expires_in); | |
await JunoToken.query().insert({ | |
access_token: active_token, | |
expire_datetime, | |
}); | |
JunoApi.defaults.headers.authorization = `Bearer ${active_token}`; | |
return active_token; | |
}; | |
} | |
export default generateJunoToken; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment