Skip to content

Commit d412cc3

Browse files
committed
Add create pdf method
1 parent 461121e commit d412cc3

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

src/Afip.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ const RegisterScopeThirteen = require('./Class/RegisterScopeThirteen');
4242
module.exports = Afip;
4343

4444
function Afip(options = {}){
45+
/**
46+
* SDK version
47+
**/
48+
this.sdk_version_number = '0.7.8';
49+
4550
/**
4651
* File name for the WSDL corresponding to WSAA
4752
*
@@ -129,6 +134,20 @@ function Afip(options = {}){
129134
this.WSAA_URL = 'https://wsaahomo.afip.gov.ar/ws/services/LoginCms';
130135
}
131136

137+
// Create an instance of the admin client
138+
/** @private */
139+
this.AdminClient = axios.create({
140+
baseURL: 'https://app.afipsdk.com/api/',
141+
timeout: 10000
142+
});
143+
144+
this.AdminClient.defaults.headers.common['sdk-version-number'] = this.sdk_version_number;
145+
this.AdminClient.defaults.headers.common['sdk-library'] = 'javascript';
146+
147+
if (this.options['access_token']) {
148+
this.AdminClient.defaults.headers.common['Authorization'] = `Bearer ${this.options['access_token']}`;
149+
}
150+
132151
this.ElectronicBilling = new ElectronicBilling(this);
133152
this.RegisterScopeFour = new RegisterScopeFour(this);
134153
this.RegisterScopeFive = new RegisterScopeFive(this);
@@ -302,20 +321,7 @@ Afip.prototype.TrackUsage = async function(web_service, operation, params = {})
302321
this.mixpanel.track(web_service+'.'+operation, Object.assign({}, this.mixpanelRegister, options));
303322
} catch (e) {}
304323

305-
if (!this.AdminClient && this.options['production'] === true) {
306-
/** @private */
307-
this.AdminClient = axios.create({
308-
baseURL: 'https://app.afipsdk.com/api/',
309-
timeout: 10000
310-
});
311-
312-
this.AdminClient.defaults.headers.common['sdk-version-number'] = '0.7.8';
313-
this.AdminClient.defaults.headers.common['sdk-library'] = 'javascript';
314-
315-
if (this.options['access_token']) {
316-
this.AdminClient.defaults.headers.common['Authorization'] = `Bearer ${this.options['access_token']}`;
317-
}
318-
324+
if (!this.AdminClientInitialized && this.options['production'] === true) {
319325
try {
320326
await this.AdminClient.post('v1/sdk-events', {
321327
"name": "initialized",
@@ -325,6 +331,9 @@ Afip.prototype.TrackUsage = async function(web_service, operation, params = {})
325331
"afip_sdk_library": "javascript"
326332
}
327333
});
334+
335+
/** @private */
336+
this.AdminClientInitialized = true;
328337
} catch (error) {
329338
if (!error.response) {
330339
throw error;

src/Class/ElectronicBilling.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@ module.exports = class ElectronicBilling extends AfipWebService {
1919
super(options, { service: 'wsfe' });
2020
}
2121

22+
/**
23+
* Create PDF
24+
*
25+
* Send a request to Afip SDK server to create a PDF
26+
*
27+
* @param {object} data Data for PDF creation
28+
**/
29+
async createPDF(data)
30+
{
31+
try {
32+
const { data: { file, file_name } } = await this.afip.AdminClient.post('v1/pdfs', data);
33+
34+
return { file, file_name };
35+
} catch (error) {
36+
if (!error.response) {
37+
throw error;
38+
}
39+
else if (error.response.data && error.response.data.message) {
40+
throw Object.assign(new Error(error.response.data.message), error.response.data);
41+
}
42+
else {
43+
throw Object.assign(new Error(error.response.statusText), error.response);
44+
}
45+
}
46+
}
47+
2248
/**
2349
* Gets last voucher number
2450
*

0 commit comments

Comments
 (0)