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);
    procedure SetpRESTResponseDataSetAdapter(const Value: TRESTResponseDataSetAdapter);
    procedure SetRESTRequest1(const Value: TRESTRequest);
  //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, pNumSequencial, pOrigem: String; pData: TDateTime);
    procedure getCanelarAutorizacao(pCNPJ, pNumAutorizacao, pNumSequencial, pOrigem, pNumCartao:String; pValor:Currency; 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, Vcl.Dialogs;

{ TFuncionalCardAPI }

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


  RESTResponseDataSetAdapter.RootElement := '';
// RESTResponseDataSetAdapter.StringFieldSize := 1000;

  RESTRequest.Resource := 'api/Autorizacao/VendaPreAutorizadaProgramaAcesso/';
  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   := 'numeroCartao';
    value  := pNumCartao;
    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   := 'DataHora';
    value  := FormatDateTime('yyyy-mm-dd',pData);
    Kind  := pkGETorPOST;
  end;


  with RESTRequest.Params.AddItem do
  begin
    Name   := 'ValorTransacao';
    value  := FormatFloat('0.00', pValor);
    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;

procedure TFuncionalCardAPI.getConfirmarPreAutorizacao(pNumAutorizacao, pNumSequencial, pOrigem: String; pData: TDateTime);
begin

  RESTResponseDataSetAdapter.RootElement := '';
//  RESTResponseDataSetAdapter.StringFieldSize := 1000;

  RESTRequest.Resource := 'api/Autorizacao/VendaPreAutorizada/';
  RESTRequest.Method   := rmPost;
  RESTRequest.Params.Clear;


  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  := '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;

function TFuncionalCardAPI.getLogin(FDMemTable: TFDMemTable; pLogin, pSenha: String): widestring;
begin

  try
  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;
  except
    on e: exception do
       showMessage( e.Message);
  end;
end;

procedure TFuncionalCardAPI.getResgatarPreAutorizacao(pCNPJ, pNumCartao, pNumAutorizacao, pOrigem: String; pData: TDateTime);
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;

  //RESTResponseDataSetAdapterITENS.RootElement := 'Itens';
  //RESTResponseDataSetAdapterITENS.StringFieldSize := 1000;
  //RESTResponseDataSetAdapterITENS.Response := RESTRequest.Response

end;

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

procedure TFuncionalCardAPI.SetpRESTResponseDataSetAdapter(
  const Value: TRESTResponseDataSetAdapter);
begin
  FpRESTResponseDataSetAdapter := Value;
end;

procedure TFuncionalCardAPI.SetRESTRequest1(const Value: TRESTRequest);
begin
  FRESTRequest1 := Value;
end;

procedure TFuncionalCardAPI.setRetornaItens(FDMemTabel: TFDMemTable);
begin
  RESTResponseDataSetAdapter.Dataset     := FDMemTabel;
  RESTResponseDataSetAdapter.RootElement := 'Itens';
//  RESTResponseDataSetAdapter.StringFieldSize := 1000;

end;

procedure TFuncionalCardAPI.setRetornaNo(pNomeNo: String; FDMemTabel: TFDMemTable; SizeCampo:Integer=255);
begin
  RESTResponseDataSetAdapter.Dataset     := FDMemTabel;
  RESTResponseDataSetAdapter.RootElement := pNomeNo;
//  RESTResponseDataSetAdapter.StringFieldSize := SizeCampo;
end;

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

end.
