Skip to content

Commit 91d5b25

Browse files
committed
Add cert creation and ws authorization
1 parent eda610c commit 91d5b25

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
<!-- ABOUT THE PROJECT -->
3232
## Acerca del proyecto
33-
Con más de 60.000 descargas en sus versiones de Python, PHP, Node y Ruby desde el 2017 Afip SDK es la librería elegida por los desarrolladores para integrar sus plataformas con AFIP.
33+
Con más de 60k descargas, desde el 2017 Afip SDK es la librería elegida por los desarrolladores para integrar sus plataformas con AFIP.
3434

3535
Esta librería fue creada con la intención de ayudar a los programadores a usar los Web Services de AFIP sin romperse la cabeza ni perder tiempo tratando de entender la complicada documentación que AFIP provee.
3636

src/Afip.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function Afip(options = {}){
2525
/**
2626
* SDK version
2727
**/
28-
this.sdk_version_number = '1.0.1';
28+
this.sdk_version_number = '1.0.2';
2929

3030
/**
3131
* X.509 certificate in PEM format
@@ -166,4 +166,84 @@ Afip.prototype.WebService = function (service, options = {}) {
166166
options['generic'] = true;
167167

168168
return new AfipWebService({ afip: this }, options);
169+
}
170+
171+
/**
172+
* Create AFIP cert
173+
*
174+
* @param {string} username Username used in AFIP page
175+
* @param {string} password Password used in AFIP page
176+
* @param {string} alias Alias for the cert
177+
**/
178+
Afip.prototype.CreateCert = async function(username, password, alias) {
179+
// Prepare data to for request
180+
const data = {
181+
environment: this.options['production'] === true ? "prod" : "dev",
182+
tax_id: this.options['CUIT'],
183+
username,
184+
password,
185+
alias
186+
};
187+
188+
// Wait for max 120 seconds
189+
let retry = 24;
190+
191+
while (retry-- >= 0) {
192+
193+
// Execute request
194+
const result = await this.AdminClient.post('v1/afip/certs', data);
195+
196+
if (result.data.status === 'complete') {
197+
return result.data.data;
198+
}
199+
200+
if (result.data.long_job_id) {
201+
data.long_job_id = result.data.long_job_id;
202+
}
203+
// Wait 5 seconds
204+
await (new Promise(resolve => setTimeout(resolve, 5000)));
205+
}
206+
207+
throw new Error('Error: Waiting for too long');
208+
}
209+
210+
/**
211+
* Create authorization to use a web service
212+
*
213+
* @param {string} username Username used in AFIP page
214+
* @param {string} password Password used in AFIP page
215+
* @param {string} alias Cert alias
216+
* @param {string} wsid Web service id
217+
**/
218+
Afip.prototype.CreateWSAuth = async function(username, password, alias, wsid) {
219+
// Prepare data to for request
220+
const data = {
221+
environment: this.options['production'] === true ? "prod" : "dev",
222+
tax_id: this.options['CUIT'],
223+
username,
224+
password,
225+
wsid,
226+
alias
227+
};
228+
229+
// Wait for max 120 seconds
230+
let retry = 24;
231+
232+
while (retry-- >= 0) {
233+
// Execute request
234+
const result = await this.AdminClient.post('v1/afip/ws-auths', data);
235+
236+
if (result.data.status === 'complete') {
237+
return result.data.data;
238+
}
239+
240+
if (result.data.long_job_id) {
241+
data.long_job_id = result.data.long_job_id;
242+
}
243+
244+
// Wait 5 seconds
245+
await (new Promise(resolve => setTimeout(resolve, 5000)));
246+
}
247+
248+
throw new Error('Error: Waiting for too long');
169249
}

0 commit comments

Comments
 (0)