Skip to content

Commit 61a570d

Browse files
committed
Add Issue/Stamp
1 parent bee0e05 commit 61a570d

File tree

5 files changed

+139
-59
lines changed

5 files changed

+139
-59
lines changed

src/main/java/mx/com/sw/services/issue/BaseStampIssueJsonV4.java

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,76 +56,111 @@ protected BaseStampIssueJsonV4(String url, String user, String password, String
5656
}
5757

5858
/**
59-
* Timbra una representacion de CFDI en formato JSON
60-
* utilizando la versión 1 de timbrado.
61-
* @param email String email.
62-
* @return StampResponseV1
63-
* @see StampResponseV1
59+
* Obtiene los headers para su funcionamiento.
60+
* @param emails String emails receptor(max 5).
61+
* @param customId String identificador único asignado al comprobante.
62+
* @param extra boolean confirma la generación de un PDF.
63+
* @return Map String, String
6464
* @throws ServicesException exception en caso de error.
6565
*/
66-
protected Map<String, String> getHeaders(String email) throws ServicesException {
66+
protected Map<String, String> getHeaders(String emails, String customId, boolean extra) throws ServicesException {
6767
Map<String, String> headers = this.getHeaders();
68-
headers.put("email", email);
68+
69+
headers.put("Authorization", "bearer " + this.getToken());
70+
if (emails != null && validateEmails(emails)) {
71+
headers.put("email", emails);
72+
}
73+
if (customId != null) {
74+
validateCustomId(customId);
75+
headers.put("customid", customId);
76+
}
77+
if(extra){
78+
headers.put("extra", "pdf");
79+
}
6980
return headers;
7081
}
7182

7283
/**
7384
* Timbra una representacion de CFDI en formato JSON
7485
* utilizando la versión 1 de timbrado.
7586
* @param json String json.
76-
* @param email String email receptor.
87+
* @param emails String emails receptor.(max 5).
88+
* @param customId String identificador único asignado al comprobante.
89+
* @param extra boolean confirma la generación de un PDF.
7790
* @return StampResponseV1
7891
* @see StampResponseV1
7992
* @throws ServicesException exception en caso de error.
8093
*/
81-
public StampResponseV1 timbrarV1(String json, String email) throws ServicesException {
94+
public StampResponseV1 timbrarV1(String json, String emails, String customId, boolean extra) throws ServicesException {
8295
StampResponseHandlerV1 handler = new StampResponseHandlerV1();
83-
Map<String, String> headers = this.getHeaders(email);
84-
return super.timbrar(json, headers, formatPath, operation, "v1", handler, StampResponseV1.class);
96+
try{
97+
Map<String, String> headers = this.getHeaders(emails, customId, extra);
98+
return super.timbrar(json, headers, formatPath, operation, "v1", handler, StampResponseV1.class);
99+
}catch (ServicesException e) {
100+
return handler.handleException(e);
101+
}
85102
}
86103

87104
/**
88105
* Timbra una representacion de CFDI en formato JSON
89106
* utilizando la versión 2 de timbrado.
90107
* @param json String json.
91-
* @param email String email receptor.
108+
* @param emails String emails receptor.(max 5).
109+
* @param customId String identificador único asignado al comprobante.
110+
* @param extra boolean confirma la generación de un PDF.
92111
* @return StampResponseV2
93112
* @see StampResponseV2
94113
* @throws ServicesException exception en caso de error.
95114
*/
96-
public StampResponseV2 timbrarV2(String json, String email) throws ServicesException {
115+
public StampResponseV2 timbrarV2(String json, String emails, String customId, boolean extra) throws ServicesException {
97116
StampResponseHandlerV2 handler = new StampResponseHandlerV2();
98-
Map<String, String> headers = this.getHeaders(email);
99-
return super.timbrar(json, headers, formatPath, operation, "v2", handler, StampResponseV2.class);
117+
try{
118+
Map<String, String> headers = this.getHeaders(emails, customId, extra);
119+
return super.timbrar(json, headers, formatPath, operation, "v2", handler, StampResponseV2.class);
120+
}catch (ServicesException e) {
121+
return handler.handleException(e);
122+
}
100123
}
101124

102125
/**
103126
* Timbra una representacion de CFDI en formato JSON
104127
* utilizando la versión 3 de timbrado.
105128
* @param json String json.
106-
* @param email String email receptor.
129+
* @param emails String emails receptor.(max 5).
130+
* @param customId String identificador único asignado al comprobante.
131+
* @param extra boolean confirma la generación de un PDF.
107132
* @return StampResponseV3
108133
* @see StampResponseV3
109134
* @throws ServicesException exception en caso de error.
110135
*/
111-
public StampResponseV3 timbrarV3(String json, String email) throws ServicesException {
136+
public StampResponseV3 timbrarV3(String json, String emails, String customId, boolean extra) throws ServicesException {
112137
StampResponseHandlerV3 handler = new StampResponseHandlerV3();
113-
Map<String, String> headers = this.getHeaders(email);
114-
return super.timbrar(json, headers, formatPath, operation, "v3", handler, StampResponseV3.class);
138+
try{
139+
Map<String, String> headers = this.getHeaders(emails, customId, extra);
140+
return super.timbrar(json, headers, formatPath, operation, "v3", handler, StampResponseV3.class);
141+
}catch (ServicesException e) {
142+
return handler.handleException(e);
143+
}
115144
}
116145

117146
/**
118147
* Timbra una representacion de CFDI en formato JSON
119148
* utilizando la versión 4 de timbrado.
120149
* @param json String json.
121-
* @param email String email receptor.
150+
* @param emails String emails receptor.(max 5).
151+
* @param customId String identificador único asignado al comprobante.
152+
* @param extra boolean confirma la generación de un PDF.
122153
* @return StampResponseV4
123154
* @see StampResponseV4
124155
* @throws ServicesException exception en caso de error.
125156
*/
126-
public StampResponseV4 timbrarV4(String json, String email) throws ServicesException {
157+
public StampResponseV4 timbrarV4(String json, String emails, String customId, boolean extra) throws ServicesException {
127158
StampResponseHandlerV4 handler = new StampResponseHandlerV4();
128-
Map<String, String> headers = this.getHeaders(email);
129-
return super.timbrar(json, headers, formatPath, operation, "v4", handler, StampResponseV4.class);
159+
try{
160+
Map<String, String> headers = this.getHeaders(emails, customId, extra);
161+
return super.timbrar(json, headers, formatPath, operation, "v4", handler, StampResponseV4.class);
162+
}catch (ServicesException e) {
163+
return handler.handleException(e);
164+
}
130165
}
131166
}

src/main/java/mx/com/sw/services/stamp/BaseStampV4.java

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,74 +60,94 @@ protected BaseStampV4(String url, String token, String operation, String proxy,
6060
* utilizando la versión 1 de timbrado.
6161
* @param xml String xml.
6262
* @param isBase64 indica si es base64.
63-
* @param email String email receptor.
63+
* @param emails String emails receptor(max 5).
64+
* @param customId String identificador único asignado al comprobante.
65+
* @param extra boolean confirma la generación de un PDF.
6466
* @return StampResponseV1
6567
* @see StampResponseV1
6668
* @throws ServicesException exception en caso de error.
6769
*/
68-
public StampResponseV1 timbrarV1(String xml, String email, boolean isBase64) throws ServicesException {
70+
public StampResponseV1 timbrarV1(String xml, String emails, String customId, boolean extra, boolean isBase64) throws ServicesException {
6971
StampResponseHandlerV1 handler = new StampResponseHandlerV1();
70-
Map<String, String> headers = getHeaders();
71-
headers.put("email", email);
72-
String format = isBase64 ? "b64" : "";
73-
String path = String.format(formatPath, operation, "v1", format);
74-
return super.timbrar(xml, path, headers, handler, StampResponseV1.class);
72+
try{
73+
Map<String, String> headers = getHeaders(emails,customId, extra);
74+
String format = isBase64 ? "b64" : "";
75+
String path = String.format(formatPath, operation, "v1", format);
76+
return super.timbrar(xml, path, headers, handler, StampResponseV1.class);
77+
}catch (ServicesException e) {
78+
return handler.handleException(e);
79+
}
7580
}
7681

7782
/**
7883
* Timbra un documento CFDI versión XML
7984
* utilizando la versión 2 de timbrado.
8085
* @param xml String xml.
8186
* @param isBase64 indica si es base64.
82-
* @param email String email receptor.
87+
* @param emails String emails receptor(max 5).
88+
* @param customId String identificador único asignado al comprobante.
89+
* @param extra boolean confirma la generación de un PDF.
8390
* @return StampResponseV2
8491
* @see StampResponseV2
8592
* @throws ServicesException exception en caso de error.
8693
*/
87-
public StampResponseV2 timbrarV2(String xml, String email, boolean isBase64) throws ServicesException {
94+
public StampResponseV2 timbrarV2(String xml, String emails, String customId,boolean extra, boolean isBase64) throws ServicesException {
8895
StampResponseHandlerV2 handler = new StampResponseHandlerV2();
89-
Map<String, String> headers = getHeaders();
90-
headers.put("email", email);
91-
String format = isBase64 ? "b64" : "";
92-
String path = String.format(formatPath, operation, "v2", format);
93-
return super.timbrar(xml, path, headers, handler, StampResponseV2.class);
96+
try{
97+
Map<String, String> headers = getHeaders(emails,customId, extra);
98+
String format = isBase64 ? "b64" : "";
99+
String path = String.format(formatPath, operation, "v2", format);
100+
return super.timbrar(xml, path, headers, handler, StampResponseV2.class);
101+
}catch (ServicesException e) {
102+
return handler.handleException(e);
103+
}
94104
}
95105

96106
/**
97107
* Timbra un documento CFDI versión XML
98108
* utilizando la versión 3 de timbrado.
99109
* @param xml String xml.
100110
* @param isBase64 indica si es base64.
101-
* @param email String email receptor.
111+
* @param emails String emails receptor(max 5).
112+
* @param customId String identificador único asignado al comprobante.
113+
* @param extra boolean confirma la generación de un PDF.
102114
* @return StampResponseV3
103115
* @see StampResponseV3
104116
* @throws ServicesException exception en caso de error.
105117
*/
106-
public StampResponseV3 timbrarV3(String xml, String email, boolean isBase64) throws ServicesException {
118+
public StampResponseV3 timbrarV3(String xml, String emails, String customId, boolean extra, boolean isBase64) throws ServicesException {
107119
StampResponseHandlerV3 handler = new StampResponseHandlerV3();
108-
Map<String, String> headers = getHeaders();
109-
headers.put("email", email);
110-
String format = isBase64 ? "b64" : "";
111-
String path = String.format(formatPath, operation, "v3", format);
112-
return super.timbrar(xml, path, headers, handler, StampResponseV3.class);
120+
try{
121+
Map<String, String> headers = getHeaders(emails,customId, extra);
122+
String format = isBase64 ? "b64" : "";
123+
String path = String.format(formatPath, operation, "v3", format);
124+
return super.timbrar(xml, path, headers, handler, StampResponseV3.class);
125+
}catch (ServicesException e) {
126+
return handler.handleException(e);
127+
}
113128
}
114129

115130
/**
116131
* Timbra un documento CFDI versión XML
117132
* utilizando la versión 4 de timbrado.
118133
* @param xml String xml.
119134
* @param isBase64 indica si es base64.
120-
* @param email String email receptor.
135+
* @param emails String emails receptor(max 5).
136+
* @param customId String identificador único asignado al comprobante.
137+
* @param extra boolean confirma la generación de un PDF.
121138
* @return StampResponseV4
122139
* @see StampResponseV4
123140
* @throws ServicesException exception en caso de error.
124141
*/
125-
public StampResponseV4 timbrarV4(String xml, String email, boolean isBase64) throws ServicesException {
142+
public StampResponseV4 timbrarV4(String xml, String emails, String customId, boolean extra, boolean isBase64) throws ServicesException {
126143
StampResponseHandlerV4 handler = new StampResponseHandlerV4();
127-
Map<String, String> headers = getHeaders();
128-
headers.put("email", email);
129-
String format = isBase64 ? "b64" : "";
130-
String path = String.format(formatPath, operation, "v4", format);
131-
return super.timbrar(xml, path, headers, handler, StampResponseV4.class);
144+
try{
145+
Map<String, String> headers = getHeaders(emails,customId, extra);
146+
String format = isBase64 ? "b64" : "";
147+
String path = String.format(formatPath, operation, "v4", format);
148+
return super.timbrar(xml, path, headers, handler, StampResponseV4.class);
149+
}catch (ServicesException e) {
150+
return handler.handleException(e);
151+
}
132152
}
133153
}

src/main/java/mx/com/sw/services/stamp/StampService.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@ protected Map<String, String> getHeaders() throws ServicesException {
5454
return headers;
5555
}
5656

57+
/**
58+
* Obtiene los headers para su funcionamiento.
59+
* @param emails String emails receptor(max 5).
60+
* @param customId String identificador único asignado al comprobante.
61+
* @param extra boolean confirma la generación de un PDF.
62+
* @return Map String, String
63+
* @throws ServicesException exception en caso de error.
64+
*/
65+
protected Map<String, String> getHeaders(String emails, String customId, boolean extra) throws ServicesException {
66+
super.setupRequest();
67+
Map<String, String> headers = new HashMap<String, String>();
68+
headers.put("Authorization", "bearer " + this.getToken());
69+
if (emails != null && validateEmails(emails)) {
70+
headers.put("email", emails);
71+
}
72+
if (customId != null) {
73+
validateCustomId(customId);
74+
headers.put("customid", customId);
75+
}
76+
if(extra){
77+
headers.put("extra", "pdf");
78+
}
79+
return headers;
80+
}
81+
5782
/**
5883
* Realiza el timbrado de un documento dada la configuracion.
5984
* @param <T> generic response type

src/test/java/mx/com/sw/services/cfdi40/helpers/IssueJsonV4.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public IssueJsonV4(Boolean isToken) throws ServicesException {
3939
@Override
4040
public StampResponseV1 stampResponseV1(String fileName, String stampVersion, boolean signed,
4141
boolean isBase64) throws ServicesException {
42-
return issue.timbrarV1(getSettings().getJsonCFDI(fileName, isBase64), getSettings().getEmail());
42+
return issue.timbrarV1(getSettings().getJsonCFDI(fileName, isBase64), getSettings().getEmail(), null, false);
4343
}
4444

4545
/**
@@ -48,7 +48,7 @@ public StampResponseV1 stampResponseV1(String fileName, String stampVersion, boo
4848
@Override
4949
public StampResponseV2 stampResponseV2(String fileName, String stampVersion, boolean signed,
5050
boolean isBase64) throws ServicesException {
51-
return issue.timbrarV2(getSettings().getJsonCFDI(fileName, isBase64), getSettings().getEmail());
51+
return issue.timbrarV2(getSettings().getJsonCFDI(fileName, isBase64), getSettings().getEmail(), null, false);
5252
}
5353

5454
/**
@@ -57,7 +57,7 @@ public StampResponseV2 stampResponseV2(String fileName, String stampVersion, boo
5757
@Override
5858
public StampResponseV3 stampResponseV3(String fileName, String stampVersion, boolean signed,
5959
boolean isBase64) throws ServicesException {
60-
return issue.timbrarV3(getSettings().getJsonCFDI(fileName, isBase64), getSettings().getEmail());
60+
return issue.timbrarV3(getSettings().getJsonCFDI(fileName, isBase64), getSettings().getEmail(), null, false);
6161
}
6262

6363
/**
@@ -66,6 +66,6 @@ public StampResponseV3 stampResponseV3(String fileName, String stampVersion, boo
6666
@Override
6767
public StampResponseV4 stampResponseV4(String fileName, String stampVersion, boolean signed,
6868
boolean isBase64) throws ServicesException {
69-
return issue.timbrarV4(getSettings().getJsonCFDI(fileName, isBase64), getSettings().getEmail());
69+
return issue.timbrarV4(getSettings().getJsonCFDI(fileName, isBase64), getSettings().getEmail(), null, false);
7070
}
7171
}

src/test/java/mx/com/sw/services/cfdi40/helpers/IssueV4.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public IssueV4(Boolean isToken) throws ServicesException {
3939
public StampResponseV1 stampResponseV1(String fileName, String stampVersion, boolean signed,
4040
boolean isBase64) throws ServicesException {
4141
return issue.timbrarV1(getSettings().getCFDI(fileName, false, "4.0", isBase64),
42-
getSettings().getEmail(), isBase64);
42+
getSettings().getEmail(), null, false, isBase64);
4343
}
4444

4545
/**
@@ -49,7 +49,7 @@ public StampResponseV1 stampResponseV1(String fileName, String stampVersion, boo
4949
public StampResponseV2 stampResponseV2(String fileName, String stampVersion, boolean signed,
5050
boolean isBase64) throws ServicesException {
5151
return issue.timbrarV2(getSettings().getCFDI(fileName, false, "4.0", isBase64),
52-
getSettings().getEmail(), isBase64);
52+
getSettings().getEmail(), null, false, isBase64);
5353
}
5454

5555
/**
@@ -59,7 +59,7 @@ public StampResponseV2 stampResponseV2(String fileName, String stampVersion, boo
5959
public StampResponseV3 stampResponseV3(String fileName, String stampVersion, boolean signed,
6060
boolean isBase64) throws ServicesException {
6161
return issue.timbrarV3(getSettings().getCFDI(fileName, false, "4.0", isBase64),
62-
getSettings().getEmail(), isBase64);
62+
getSettings().getEmail(), null, false, isBase64);
6363
}
6464

6565
/**
@@ -69,6 +69,6 @@ public StampResponseV3 stampResponseV3(String fileName, String stampVersion, boo
6969
public StampResponseV4 stampResponseV4(String fileName, String stampVersion, boolean signed,
7070
boolean isBase64) throws ServicesException {
7171
return issue.timbrarV4(getSettings().getCFDI(fileName, false, "4.0", isBase64),
72-
getSettings().getEmail(), isBase64);
72+
getSettings().getEmail(), null, false, isBase64);
7373
}
7474
}

0 commit comments

Comments
 (0)