forked from efipay/sdk-node-apis-efi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathefiServices.js
170 lines (143 loc) · 4.72 KB
/
efiServices.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
let AWS = require('aws-sdk')
const fs = require('fs')
const fsPromise = require('fs').promises
import { S3 } from 'aws-sdk/index'
const https = require('https')
const axios = require('axios')
// TODO: CHANGE HERE
const options = {
sandbox: false,
client_id: 'your_Client_Id',
client_secret: 'your_Client_Secret',
certificate: '/tmp/certificate-name.p12', // put your certificate name here
}
const { AWS_REGION_ENV_VARIABLE } = process.env
// TODO: move to aws secrets
AWS.config.update({
"accessKeyId": "ADD_YOU_AWS_KEY_HERE", // TODO: CHANGE HERE
"secretAccessKey": "ADD_YOUR_AWS_SECRET_KEY_HERE" // TODO: CHANGE HERE
})
let auth
let agent
function isExpired(auth) {
let current_time = new Date().getTime() / 1000
if (current_time > auth.authDate + auth.expires_in) {
return true
}
return false
}
const DEFAULT_API = 'pix'
// oauth url
let authURL = 'https://pix.api.efipay.com.br/oauth/token'
async function authenticate() {
let authParams = {
method: 'POST',
url: authURL, // this.baseUrl + this.authRoute.route,
headers: {
'api-sdk': 'node-1.1.2',
},
data: {
grant_type: 'client_credentials',
},
}
if (DEFAULT_API == 'pix') {
let token = Buffer.from(options.client_id + ':' + options.client_secret).toString('base64')
authParams.headers['Authorization'] = 'Basic ' + token
authParams.headers['Content-Type'] = 'application/json'
authParams.httpsAgent = agent
} else {
authParams.auth = {
username: options.client_id,
password: options.client_secret,
}
}
return axios(authParams)
.then((res) => {
console.log('return axios authenticate res.data:', res.data)
auth = res.data
auth.authDate = new Date().getTime() / 1000
})
.catch((error) => {
throw error.data
})
}
async function createRequest(url, body, method) {
console.log('createRequest')
let headers = new Object()
headers['x-skip-mtls-checking'] = true // !this.options.validateMtls
let req = {
method,
url: url,
headers,
data: body,
}
req['httpsAgent'] = agent
let axiosInstance = axios.create()
axiosInstance.interceptors.request.use(
async (config) => {
if (!auth || isExpired(auth)) {
await authenticate()
}
config.headers = {
Authorization: `Bearer ${auth.access_token}`,
'x-skip-mtls-checking': true // !this.options.validateMtls
}
return config
},
(error) => {
Promise.reject(error)
},
)
return axiosInstance(req)
.then((res) => {
console.log('return axiosInstance req req.data:', req.data)
return res.data
})
.catch((error) => {
console.log('error axiosInstance error.response:', error.response)
throw error.response.data
})
}
export async function testEfi() {
console.log('testEfi')
const s3 = new S3({ region: AWS_REGION_ENV_VARIABLE })
try {
const fileName = 'YOUR_FILE_CERTIFICATE_NAME_ON_AWS_S3_BUCKET' // TODO: CHANGE HERE
let params = {
Bucket: 'YOUR_BUCKET_SAME_NAME_IN_serverless.yml', // TODO: CHANGE HERE
Key: fileName
}
const data = await s3.getObject(params).promise();
// console.log('data from getObject', data)
await fsPromise.writeFile('/tmp/' + fileName, data.Body);
let body = {
calendario: {
expiracao: 7200,
},
valor: {
original: '12.45',
},
chave: 'you_pix_key_here' // TODO: CHANGE HERE
}
let txid = 'd65480d7b502487590eb83007d70oooo' // TODO: CHANGE HERE
try {
agent = new https.Agent({
pfx: fs.readFileSync(options.certificate),
passphrase: '',
})
} catch (error) {
console.log('error reading the certificate')
throw `FALHA AO LER O CERTIFICADO, VERIFIQUE O CAMINHO INFORMADO: ${options.certificate}`
}
// pixCreateCharge example
let pixUrl = 'https://pix.api.efipay.com.br' + '/v2/cob/' + txid
let pix = await createRequest(pixUrl, body, 'PUT')
console.log('pix:', pix)
console.log('pix.loc:', pix.loc)
console.log('pix.location:', pix.location)
console.log('pix.pixCopiaECola:', pix.pixCopiaECola)
} catch (e) {
console.log('Error executing Efi API: ' + e.message)
throw new Error('Error executing Efi API: ' + e.message)
}
}