unit uClass.FunionalCard.Api;

interface

Uses REST.Response.Adapter, REST.Client, // Data.Bind.Components, Data.Bind.ObjectScope,
  FireDAC.Comp.Client, System.Types, Data.DB;

type

  TFuncionalCardAPI = class
  private
    FChaveLogin: String;
    // FpRESTResponseDataSetAdapter: TRESTResponseDataSetAdapter;
    // FRESTRequest1: TRESTRequest;
    procedure SetChaveLogin(const Value: String);
    // protected
  published
    // property RESTRequest:TRESTRequest read FRESTRequest1 write SetRESTRequest1;
    // property RESTResponseDataSetAdapter:TRESTResponseDataSetAdapter read FpRESTResponseDataSetAdapter write SetpRESTResponseDataSetAdapter;
    property ChaveLogin: String read FChaveLogin write SetChaveLogin;
  public
    function getLogin(FDMemTable: TFDMemTable; pLogin, pSenha: String): widestring;
    procedure getResgatarPreAutorizacao(pCNPJ, pNumCartao, pNumAutorizacao, pOrigem: String; pData: TDateTime);
    procedure getConfirmarPreAutorizacao(pNumAutorizacao, pCNPJ, pNumeroCartao, pOrigem: String; pData: TDateTime);
    procedure getCanelarAutorizacao(pCNPJ, pNumAutorizacao, pNumSequencial, pOrigem, pNumCartao, pValor: String; pData: TDateTime);

    procedure setRetornaItens(FDMemTabel: TFDMemTable);
    procedure setRetornaReceita(pIten: integer; FDMemTabel: TFDMemTable; Raiz: String = '');
    procedure setRetornaNo(pNomeNo: String; FDMemTabel: TFDMemTable; SizeCampo: integer = 255);

  end;

implementation

uses
  Vcl.Forms, REST.Types, System.SysUtils, ufrm.Principal, Vcl.Dialogs;

{ TFuncionalCardAPI }

procedure TFuncionalCardAPI.getCanelarAutorizacao(pCNPJ, pNumAutorizacao, pNumSequencial, pOrigem, pNumCartao, pValor: String; pData: TDateTime);
begin

  with frmPrincipal do
  begin
    RESTResponseDataSetAdapter.RootElement     := '';
    RESTResponseDataSetAdapter.StringFieldSize := 1000;

    RESTRequest.Resource := 'api/Autorizacao/CancelarTransacao/';
    RESTRequest.Method   := rmPOST;
    RESTRequest.Params.Clear;

    with RESTRequest.Params.AddItem do
    begin
      Name  := 'CNPJ';
      Value := pCNPJ;
      Kind  := pkGETorPOST;
    end;

    with RESTRequest.Params.AddItem do
    begin
      Name  := 'NumeroAutorizacao';
      Value := pNumAutorizacao;
      Kind  := pkGETorPOST;
    end;

    with RESTRequest.Params.AddItem do
    begin
      Name  := 'NumeroSequencial';
      Value := pNumSequencial;
      Kind  := pkGETorPOST;
    end;


    with RESTRequest.Params.AddItem do
    begin
      Name  := 'numeroCartao';
      Value := pNumCartao;
      Kind  := pkGETorPOST;
    end;

    with RESTRequest.Params.AddItem do
    begin
      Name  := 'ValorTransacao';
      Value := pValor;
      Kind  := pkGETorPOST;
    end;

    with RESTRequest.Params.AddItem do
    begin
      Name  := 'DataHora';
      Value := FormatDateTime('yyyy-mm-dd', pData);
      Kind  := pkGETorPOST;
    end;

    with RESTRequest.Params.AddItem do
    begin
      Name  := 'ApiKey';
      Value := ChaveLogin;
      Kind  := pkHTTPHEADER;
    end;

    with RESTRequest.Params.AddItem do
    begin
      Name  := 'Origem';
      Value := pOrigem;
      Kind  := pkHTTPHEADER;
    end;

    RESTRequest.Execute;
  end;

end;

procedure TFuncionalCardAPI.getConfirmarPreAutorizacao(pNumAutorizacao, pCNPJ, pNumeroCartao, pOrigem: String; pData: TDateTime);
begin
  with frmPrincipal do
  begin

    frmPrincipal.RESTRequest1.Params.ParameterByName('ApiKey').Value := ChaveLogin;
    frmPrincipal.RESTRequest1.Params.ParameterByName('Origem').Value := pOrigem;

      frmPrincipal.RESTRequest1.Params.ParameterByName('body').Value := '{"Cnpj": "' + pCNPJ + '",' + ' "NumeroAutorizacao": ' + pNumAutorizacao + ',' + ' "DataHoraAutorizacao": "' + FormatDateTime('yyyy-mm-dd', pData) + '",' + ' "NumeroCartao": "' + pNumeroCartao + '",' + ' "Itens": [';
      frmPrincipal.FDMemTableITENS.First;
      while not frmPrincipal.FDMemTableITENS.eof do
      begin
         frmPrincipal.RESTRequest1.Params.ParameterByName('body').Value := RESTRequest1.Params.ParameterByName('body').Value
         +'{'
         +'"EAN": "'                   +Trim(frmPrincipal.FDMemTableITENS.FieldByName('EAN').AsString)+'",'
         +'"Preco": '                  +StringReplace(Trim(frmPrincipal.FDMemTableITENS.FieldByName('PrecoVenda').AsString),',','.',[rfReplaceAll, rfIgnoreCase])+','
         +'"QuantidadeVenda": '        +Trim(frmPrincipal.FDMemTableITENS.FieldByName('QuantidadeVenda').AsString)+','
         +'"Receita": {'
         +'  "QuantidadeReceita": '      +Trim(frmPrincipal.FDMemTableReceita.FieldByName('QuantidadeReceita').AsString)+','
         +'  "DataReceita": "'           +Trim(frmPrincipal.FDMemTableReceita.FieldByName('DataReceita').AsString)+'",'
         +'  "Conselho": '               +Trim(frmPrincipal.FDMemTableReceita.FieldByName('Conselho').AsString)+','
         +'  "NumeroRegistroConselho": ' +Trim(frmPrincipal.FDMemTableReceita.FieldByName('NumeroRegistroConselho').AsString)+','
         +'  "SiglaEstado": "'           +Trim(frmPrincipal.FDMemTableReceita.FieldByName('SiglaEstado').AsString+'"')
         +'}'
         +'}';
        frmPrincipal.FDMemTableITENS.Next;
        if not frmPrincipal.FDMemTableITENS.eof then
          frmPrincipal.RESTRequest1.Params.ParameterByName('body').Value := RESTRequest1.Params.ParameterByName('body').Value + ','
      end;

      // --------------
      frmPrincipal.RESTRequest1.Params.ParameterByName('body').Value := RESTRequest1.Params.ParameterByName('body').Value + ']' + '}';


    frmPrincipal.Memo1.Lines.Clear;

    frmPrincipal.Memo1.Lines.Add(RESTRequest1.Params.ParameterByName('body').Value);
    try
      RESTRequest1.Execute;
      //frmPrincipal.RESTResponseDataSetAdapter1.Active := True;
    except
      // on e:exception do
      // ShowMessage(e.Message);

    end;
  end;

end;

function TFuncionalCardAPI.getLogin(FDMemTable: TFDMemTable; pLogin, pSenha: String): widestring;
begin
  with frmPrincipal do
  begin
    RESTRequest.Resource := 'api/Authentication/EstabelecimentoSenha';
    RESTRequest.Method   := rmPOST;
    RESTRequest.Params.Clear;
    RESTResponseDataSetAdapter.RootElement     := 'ApiKey';
    RESTResponseDataSetAdapter.StringFieldSize := 1000;
    with RESTRequest.Params.AddItem do
    begin
      Name  := 'login';
      Value := pLogin;
      Kind  := pkGETorPOST;
    end;
    with RESTRequest.Params.AddItem do
    begin
      Name  := 'password';
      Value := pSenha;
      Kind  := pkGETorPOST;
    end;

    RESTRequest.Execute;

    if FDMemTable.FieldByName('key').AsString <> '' then
    begin
      result     := FDMemTable.FieldByName('key').Text;
      ChaveLogin := result;
    end
    else
      result := FDMemTable.FieldByName('ResultadoValidacao').Text;
  end;
end;

procedure TFuncionalCardAPI.getResgatarPreAutorizacao(pCNPJ, pNumCartao, pNumAutorizacao, pOrigem: String; pData: TDateTime);
begin
  with frmPrincipal do
  begin
    RESTResponseDataSetAdapter.RootElement     := '';
    RESTResponseDataSetAdapter.StringFieldSize := 1000;

    RESTRequest.Resource := 'api/Autorizacao/ResgatarPreAutorizacao/';
    RESTRequest.Method   := rmGET;
    RESTRequest.Params.Clear;
    with RESTRequest.Params.AddItem do
    begin
      Name  := 'CNPJ';
      Value := pCNPJ;
      Kind  := pkGETorPOST;
    end;

    with RESTRequest.Params.AddItem do
    begin
      Name  := 'numeroCartao';
      Value := pNumCartao;
      Kind  := pkGETorPOST;
    end;
    with RESTRequest.Params.AddItem do
    begin
      Name  := 'dataAutorizacao';
      Value := FormatDateTime('yyyymmddhhmmss', pData);
      Kind  := pkGETorPOST;
    end;
    with RESTRequest.Params.AddItem do
    begin
      Name  := 'numeroAutorizacao';
      Value := pNumAutorizacao;
      Kind  := pkGETorPOST;
    end;

    with RESTRequest.Params.AddItem do
    begin
      Name  := 'ApiKey';
      Value := ChaveLogin;
      Kind  := pkHTTPHEADER;
    end;

    with RESTRequest.Params.AddItem do
    begin
      Name  := 'Origem';
      Value := pOrigem;
      Kind  := pkHTTPHEADER;
    end;

    RESTRequest.Execute;
  end;
end;

procedure TFuncionalCardAPI.SetChaveLogin(const Value: String);
begin
  FChaveLogin := Value;
end;

procedure TFuncionalCardAPI.setRetornaItens(FDMemTabel: TFDMemTable);
begin
  with frmPrincipal do
  begin
    try
      RESTResponseDataSetAdapter.Dataset         := FDMemTabel;
      RESTResponseDataSetAdapter.RootElement     := 'Itens';
      RESTResponseDataSetAdapter.StringFieldSize := 1000;
    except

    end;
  end;
end;

procedure TFuncionalCardAPI.setRetornaNo(pNomeNo: String; FDMemTabel: TFDMemTable; SizeCampo: integer = 255);
begin
  with frmPrincipal do
  begin
    try
      RESTResponseDataSetAdapter.Dataset         := FDMemTabel;
      RESTResponseDataSetAdapter.RootElement     := pNomeNo;
      RESTResponseDataSetAdapter.StringFieldSize := SizeCampo;
    except

    end;
  end;
end;

procedure TFuncionalCardAPI.setRetornaReceita(pIten: integer; FDMemTabel: TFDMemTable; Raiz: String = '');
begin
  with frmPrincipal do
  begin
    try
      RESTResponseDataSetAdapter.Dataset         := FDMemTabel;
      RESTResponseDataSetAdapter.RootElement     := Raiz + 'Itens[' + Trim(IntToStr(pIten)) + '].Receita';
      RESTResponseDataSetAdapter.StringFieldSize := 1000;
    except

    end;
  end;
end;

end.
