generated from ecomplus/application-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
2,473 additions
and
203 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const createAxios = require('./create-axios') | ||
const getOAuth = require('./get-token') | ||
|
||
module.exports = function (clientId, clientSecret, storeId, firestoreColl = 'banrisul_auth') { | ||
const self = this | ||
|
||
let documentRef | ||
// const hashLogin = Buffer.from(`${galaxpayId}:${galaxpayHash}`).toString('base64') | ||
|
||
if (firestoreColl) { | ||
documentRef = require('firebase-admin') | ||
.firestore() | ||
.doc(`${firestoreColl}/${storeId}`) | ||
} | ||
|
||
this.preparing = new Promise((resolve, reject) => { | ||
const authenticate = (accessToken, documentRef) => { | ||
self.axios = createAxios(accessToken) | ||
self.documentRef = documentRef | ||
if (documentRef) { | ||
documentRef | ||
.set({ accessToken }, { merge: true }) | ||
.catch(console.error) | ||
} | ||
resolve(self) | ||
} | ||
|
||
const handleAuth = () => { | ||
console.log('> Banrisul Auth02 ', storeId) | ||
getOAuth(clientId, clientSecret) | ||
.then((accessToken) => { | ||
console.log(`>> s:${storeId} token => ${accessToken}`) | ||
authenticate(accessToken, documentRef) | ||
}) | ||
.catch(reject) | ||
} | ||
|
||
if (documentRef) { | ||
documentRef.get() | ||
.then((documentSnapshot) => { | ||
if (documentSnapshot.exists && | ||
Date.now() - documentSnapshot.updateTime.toDate().getTime() <= 59 * 60 * 1000 // access token expires in 60 minutes | ||
) { | ||
authenticate(documentSnapshot.get('accessToken'), documentRef) | ||
} else { | ||
handleAuth() | ||
} | ||
}) | ||
.catch(console.error) | ||
} else { | ||
handleAuth() | ||
} | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const axios = require('axios') | ||
module.exports = (accessToken, isGetToken) => { | ||
const isSandbox = true | ||
|
||
const headers = { | ||
'Content-Type': 'application/json' | ||
} | ||
|
||
if (accessToken) { | ||
console.log('> token ', accessToken) | ||
headers.Authorization = `Bearer ${accessToken}` | ||
} | ||
|
||
return axios.create({ | ||
baseURL: `https://api${isSandbox ? 'dev' : ''}.banrisul.com.br${isGetToken ? '' : '/cobranca/v1'}`, | ||
headers | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const axios = require('./create-axios') | ||
const qs = require('qs') | ||
|
||
module.exports = (clientId, clientSecret) => new Promise((resolve, reject) => { | ||
const banrisulAxios = axios(null, true) | ||
|
||
const request = async (isRetry) => { | ||
try { | ||
const headers = { | ||
'content-type': 'application/x-www-form-urlencoded', | ||
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}` | ||
} | ||
|
||
const body = { | ||
grant_type: 'client_credentials', | ||
scope: 'boletos' | ||
} | ||
|
||
const { data: { access_token: token } } = await banrisulAxios.post( | ||
'/auth/oauth/v2/token', | ||
qs.stringify(body), | ||
{ headers } | ||
) | ||
|
||
resolve(token) | ||
} catch (err) { | ||
if (!isRetry && err.response && err.response.status >= 429) { | ||
setTimeout(() => request(true), 7000) | ||
} | ||
reject(err) | ||
} | ||
} | ||
|
||
request() | ||
}) |
Oops, something went wrong.