unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, REST.Types, FireDAC.Stan.Intf,
  FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS,
  FireDAC.Phys.Intf, FireDAC.DApt.Intf, Data.DB, Vcl.Grids, Vcl.DBGrids,
  FireDAC.Comp.DataSet, FireDAC.Comp.Client, REST.Response.Adapter, REST.Client,
  Data.Bind.Components, Data.Bind.ObjectScope, Vcl.StdCtrls, Vcl.ExtCtrls,
  Vcl.ComCtrls, Vcl.DBCtrls,Soap.XSBuiltIns,XML.Win.msxmldom, uClass.FunionalCard.Api, IPPeerClient, uFuncionalSoap3, Datasnap.DBClient, Soap.SOAPConn, Soap.InvokeRegistry, Soap.Rio, Soap.SOAPHTTPClient,
  ACBrBase, ACBrSocket, ACBrSedex, Xml.xmldom, Json, Xml.XMLIntf, Xml.XMLDoc, Soap.SOAPHTTPDisp, Soap.WebBrokerSOAP, Soap.WSDLIntf, Soap.SOAPPasInv, Soap.SOAPHTTPPasInv, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdHTTP;

 type TAPIKeyTeste2 = Class(TSoapHeader)
   private
    FChave: string;
  public
    property Chave: string  read FChave write FChave;
 End;

 TAPIKeyTeste = Class(TAPIKeyTeste2)
 private
 published
 End;


type
  TForm2 = class(TForm)
    RESTRequest1: TRESTRequest;
    RESTResponse1: TRESTResponse;
    RESTResponseDataSetAdapter1: TRESTResponseDataSetAdapter;
    FDMemTable: TFDMemTable;
    dsFDMemTable: TDataSource;
    FDMemTableITENS: TFDMemTable;
    dsFDMemTableITENS: TDataSource;
    RESTClient2: TRESTClient;
    dsFDMemTableReceita: TDataSource;
    FDMemTableReceita: TFDMemTable;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    memoKey: TMemo;
    Button1: TButton;
    DBGrid2: TDBGrid;
    Label4: TLabel;
    Label3: TLabel;
    DBGrid3: TDBGrid;
    DBGrid1: TDBGrid;
    Button2: TButton;
    cmbOrigem: TComboBox;
    edtData: TDateTimePicker;
    edtAutorizacao: TLabeledEdit;
    edtCartao: TLabeledEdit;
    edtCNPJ: TLabeledEdit;
    Label2: TLabel;
    Label1: TLabel;
    edtNumSequencil: TLabeledEdit;
    edtUsuario: TLabeledEdit;
    edtPassword: TLabeledEdit;
    Button3: TButton;
    Button4: TButton;
    edtValor: TLabeledEdit;
    Panel1: TPanel;
    rbREST: TRadioButton;
    rbSOAP: TRadioButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure dsFDMemTableITENSDataChange(Sender: TObject; Field: TField);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure PreAutoriza;
    procedure ConfirmaSoap;
    procedure ResgataSOAP;
    procedure GravaMemo(FLimpa :Boolean; FTexto :String);
  private
    { Private declarations }
  public
   FuncionalSOAP :IServiceAutorizador;
   Sol           :AutorizacaoRequest2;
   Resp         : AutorizacaoResponse2;
   ApiTeste     : TApiKeyTeste;


   oFuncionalCard :TFuncionalCardAPI;

    { Public declarations }
  end;

var
  Form2: TForm2;
  oFuncionalCard :TFuncionalCardAPI;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
   oFuncionalCard.RESTRequest := RESTRequest1;
   oFuncionalCard.RESTResponseDataSetAdapter := RESTResponseDataSetAdapter1;
   oFuncionalCard.getLogin(FDMemTable, edtUsuario.Text, edtPassword.Text);
   memoKey.Lines.Add(oFuncionalCard.ChaveLogin);
   RESTResponseDataSetAdapter1.Active := False;

end;

procedure TForm2.Button2Click(Sender: TObject);
begin
   if rbSOAP.Checked then
   begin
     ResgataSOAP;
   end;

   if rbREST.Checked then
   begin
  //RESTRequest1.c
    oFuncionalCard.getResgatarPreAutorizacao(edtCNPJ.Text, edtCartao.Text, edtAutorizacao.Text, cmbOrigem.Text[1],  edtData.DateTime);

    oFuncionalCard.setRetornaItens(FDMemTableITENS);
   end;


end;

procedure TForm2.Button3Click(Sender: TObject);
begin

 if rbREST.Checked then
 begin
  oFuncionalCard.getConfirmarPreAutorizacao(edtAutorizacao.Text, edtNumSequencil.Text,cmbOrigem.Text,edtData.DateTime);
  oFuncionalCard.setRetornaNo('Transacao',FDMemTable);
  oFuncionalCard.setRetornaNo('Transacao.Itens',FDMemTableITENS,100);
 end;

 if rbSOAP.Checked then
 begin

 end;

end;

procedure TForm2.Button4Click(Sender: TObject);
begin
  oFuncionalCard.getCanelarAutorizacao(edtCNPJ.Text,edtAutorizacao.Text, edtNumSequencil.Text, cmbOrigem.Text, edtCartao.Text, StrToCurrDef(edtValor.text,0), edtData.Datetime);
end;

procedure TForm2.ConfirmaSoap;
begin

// FuncionalSOAP.VendaPreAutorizadaProgramaAcesso()

// FuncionalSOAP.VendaPreAutorizada(edtCNPJ.Text,edtAutorizacao.Text,Now,Sol);
end;

procedure TForm2.dsFDMemTableITENSDataChange(Sender: TObject; Field: TField);
begin
  try
    oFuncionalCard.setRetornaReceita(FDMemTable.RecNo-1, FDMemTableReceita);
  except
    oFuncionalCard.setRetornaReceita(FDMemTable.RecNo-1, FDMemTableReceita,'Transacao.');
  end;
end;

procedure TForm2.FormShow(Sender: TObject);
begin
  oFuncionalCard := TFuncionalCardAPI.Create;
  oFuncionalCard.RESTRequest := RESTRequest1;
  oFuncionalCard.RESTResponseDataSetAdapter := RESTResponseDataSetAdapter1;


  Sol     := AutorizacaoRequest2.Create;

  edtData.DateTime := Now;
  Xml.Win.msxmldom.MSXMLDOMDocumentFactory.AddDOMProperty('ProhibitDTD', False);
  ApiTeste := TApiKeyTeste.Create;
end;

procedure TForm2.GravaMemo(FLimpa :Boolean; FTexto :String);
begin
  if FLimpa then
  Memo1.Lines.Clear;

  Memo1.Lines.Add(FTexto);
end;

procedure TForm2.PreAutoriza;
var Itens   :ArrayOfAutorizacaoRequestItem;
    Item2   :AutorizacaoRequestItem2;
    I, QtdeItens :Integer;
    Receita :DadosReceita2;
begin
//  I := 0;
//
//  SetLength(Sol.Itens,FDMemTableITENS.RecordCount);
//
//  FDMemTableITENS.First;
//
//  while not FDMemTableITENS.Eof do
//  begin
//    Item2.EAN             := FDMemTableITENS.FieldByName('EAN').AsString;
//    Item2.Preco           := FDMemTableITENS.FieldByName('PRECOVENDA').AsFloat;
//    Item2.QuantidadeVenda := FDMemTableITENS.FieldByName('QUANTIDADEVENDA').AsInteger;
//    Item2.Receita         := Receita;
//    Sol.Itens[I]          := Item2;
//    Inc(I);
//  end;
//
//
//  Sol.NumeroCartao := edtCartao.Text;
//
//  Resp := FuncionalSOAP.ResgatarPreAutorizacao(edtCNPJ, Sol);
//  ShowMEssage(Resp.Message_)

end;

procedure TForm2.ResgataSOAP;
 var DateX :TXSDateTime;
     Resgate :ResgataPreAutorizacaoResponse2;
     Auth: AuthenticationRequest2;
     Validacao :  AuthenticationResponse2;
     HD :iSOAPHeaders;
     A:InvString;
begin
  Auth          :=  AuthenticationRequest2.Create;
  Resgate       :=  ResgataPreAutorizacaoResponse2.Create;
  Validacao     :=  AuthenticationResponse2.Create;


  FuncionalSOAP := GetIServiceAutorizador(False);
  Auth.Login    := edtUsuario.Text;
  Auth.Password := edtPassword.Text;

  Validacao    := FuncionalSOAP.AutenticarEstabelecimentoSenha(Auth);

  GravaMemo(True, Validacao.ApiKey.Key);

  ApiTeste.Chave := Validacao.ApiKey.Key;

  DateX := TXSDateTime.Create;
  Datex.AsDateTime := edtData.DateTime;

  InvRegistry.RegisterHeaderClass(TypeInfo(IServiceAutorizador),TAPIKeyTeste2,'ApiKey', 'http://tempuri.org/');


  HD := (FuncionalSOAP as ISOAPHeaders);
  HD.Send(ApiTeste);

 A :=  InvRegistry.GetHeaderName(TypeInfo(IServiceAutorizador),TAPIKeyTeste2);

  Resgate          := FuncionalSOAP.ResgatarPreAutorizacao(edtCNPJ.Text, edtCartao.Text, DateX, StrToInt(edtAutorizacao.Text));
  GravaMemo(True, Resgate.Message_);

end;

end.
