Skip to content

Commit b5d3f41

Browse files
authored
Merge pull request #10 from lunasoft/feature/ss-391
Merge a Develop con cambios para timbrado Json, por detalle de Form.
2 parents bb7d4ee + 43d03a8 commit b5d3f41

File tree

2 files changed

+68
-45
lines changed

2 files changed

+68
-45
lines changed

Issue/JsonIssue/IssueJsonRequest.pas

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,55 @@ interface
2525
implementation
2626

2727
function IssueServiceJSON(URL, Token, Json, Version: String): String;
28-
var
28+
var
2929
HTTP: TIdHTTP;
3030
RequestBody: TStream;
31+
IsUTF8: Boolean;
3132
begin
32-
try
33-
HTTP := TIdHTTP.Create;
34-
finally
35-
36-
end;
33+
try
34+
HTTP := TIdHTTP.Create;
35+
finally
36+
end;
3737

38-
try
39-
try
40-
RequestBody := TStringStream.Create(UTF8ToWideString(Json), TEncoding.UTF8);
41-
try
42-
HTTP.Request.Accept := 'application/json';
43-
HTTP.Request.ContentType := 'application/jsontoxml';
44-
HTTP.Request.CustomHeaders.FoldLines := False;
45-
HTTP.Request.CustomHeaders.Add('Authorization:Bearer ' + Token);
38+
try
39+
try
40+
IsUTF8 := TEncoding.UTF8.GetString(TEncoding.UTF8.GetBytes(Json)) = Json;
4641

47-
URL := URL+'/v3/cfdi33/issue/json/'+Version;
48-
Result := HTTP.Post(URL,RequestBody);
49-
HTTP.responseCode;
50-
finally
51-
RequestBody.Free;
52-
end;
53-
except
54-
on E: EIdHTTPProtocolException do
42+
if not IsUTF8 then
5543
begin
56-
Result := E.ErrorMessage;
57-
end;
58-
on E: Exception do
44+
RequestBody := TStringStream.Create(UTF8ToWideString(Json), TEncoding.UTF8);
45+
end
46+
else
5947
begin
60-
Result := E.Message;
48+
RequestBody := TStringStream.Create(Json, TEncoding.UTF8);
6149
end;
50+
51+
HTTP.Request.Accept := 'application/json';
52+
HTTP.Request.ContentType := 'application/jsontoxml';
53+
HTTP.Request.CustomHeaders.FoldLines := False;
54+
HTTP.Request.CustomHeaders.Add('Authorization:Bearer ' + Token);
55+
56+
URL := URL+'/v3/cfdi33/issue/json/'+Version;
57+
Result := HTTP.Post(URL, RequestBody);
58+
HTTP.responseCode;
59+
finally
60+
RequestBody.Free;
61+
end;
62+
except
63+
on E: EIdHTTPProtocolException do
64+
begin
65+
Result := E.ErrorMessage;
66+
end;
67+
on E: Exception do
68+
begin
69+
Result := E.Message;
6270
end;
63-
finally
64-
HTTP.Free;
6571
end;
72+
73+
HTTP.Free;
6674
ReportMemoryLeaksOnShutdown := False;
6775
end;
6876

69-
end.
77+
78+
79+
end.

TestServices.pas

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function fechaDeVencimiento(expiresIn: String): String;
4242

4343
type
4444
TForm1 = class(TForm)
45-
Autenticación: TPageControl;
45+
Autenticación: TPageControl;
4646
TabSheet1: TTabSheet;
4747
TabSheet2: TTabSheet;
4848
TabSheet3: TTabSheet;
@@ -60,7 +60,7 @@ TForm1 = class(TForm)
6060
txtURL: TEdit;
6161
Label1: TLabel;
6262
txtToken: TEdit;
63-
Expiración: TLabel;
63+
Expiración: TLabel;
6464
txtCaducidad: TEdit;
6565
Label2: TLabel;
6666
txtTokenTipo: TEdit;
@@ -218,7 +218,7 @@ TForm1 = class(TForm)
218218
Label61: TLabel;
219219
Label62: TLabel;
220220
Label63: TLabel;
221-
Cancelación: TPageControl;
221+
Cancelación: TPageControl;
222222
TabSheet14: TTabSheet;
223223
TabSheet15: TTabSheet;
224224
TabSheet17: TTabSheet;
@@ -431,6 +431,7 @@ TForm1 = class(TForm)
431431
procedure Button20Click(Sender: TObject);
432432
procedure Button21Click(Sender: TObject);
433433
procedure Button10Click(Sender: TObject);
434+
procedure memoJsonChange(Sender: TObject);
434435
private
435436
{ Private declarations }
436437
public
@@ -624,19 +625,21 @@ procedure TForm1.Button15Click(Sender: TObject);
624625
end;
625626

626627
procedure TForm1.Button16Click(Sender: TObject);
627-
var
628-
stream: TMemoryStream;
629628
begin
630-
memoJson.Text := '';
631-
OpenDialog1.Filter := 'Archivos JSON (*.json)|*.json|';
632-
stream := TMemoryStream.Create;
633-
634-
if OpenDialog1.Execute then
635-
if FileExists(OpenDialog1.FileName) then
636-
stream.LoadFromFile(OpenDialog1.FileName)
637-
else
638-
raise Exception.Create('File does not exist.');
639-
memoJson.Lines.LoadFromFile(OpenDialog1.FileName);
629+
memoJson.Text := ''; // Borra cualquier contenido previo en el TMemo
630+
OpenDialog1.Filter := 'Archivos JSON (*.json)|*.json|';
631+
632+
if OpenDialog1.Execute then
633+
begin
634+
if FileExists(OpenDialog1.FileName) then
635+
begin
636+
memoJson.Lines.LoadFromFile(OpenDialog1.FileName, TEncoding.UTF8); // Carga el archivo utilizando UTF-8
637+
end
638+
else
639+
begin
640+
raise Exception.Create('File does not exist.');
641+
end;
642+
end;
640643
end;
641644

642645
procedure TForm1.Button17Click(Sender: TObject);
@@ -1361,6 +1364,16 @@ procedure TForm1.cleanStamp();
13611364
txtMessageStamp.Text := '';
13621365
txtMessageDetailStamp.Text := '';
13631366
end;
1367+
procedure TForm1.memoJsonChange(Sender: TObject);
1368+
var
1369+
TextToProcess: string;
1370+
begin
1371+
TextToProcess := memoJson.Text;
1372+
1373+
TextToProcess := UTF8Encode(TextToProcess);
1374+
1375+
memoJson.Text := TextToProcess;
1376+
end;
13641377

13651378
procedure TForm1.cleanIssueStamp();
13661379
begin
@@ -1394,4 +1407,4 @@ procedure TForm1.cleanJsonStamp();
13941407
txtJsonMessageDetail.Text := '';
13951408
end;
13961409

1397-
end.
1410+
end.

0 commit comments

Comments
 (0)