PHP code example of paybook / sync-php

1. Go to this page and download the library: Download paybook/sync-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

paybook / sync-php example snippets



define('API_KEY', '<TU_API_KEY>');
use Paybook\Sync\Sync;

// Crear una sesión para un usuario
$token = Sync::auth(
    array("api_key" => API_KEY), // Tu API KEY
    array("id_user"=>$id_user) // ID de usuario
);

// Consumir un recurso de Sync
$response = Sync::run(
    $token, // Autenticación
    "/credentials", // Recurso
    null, // Parametros
    'GET' // Método HTTP
);


use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

"Hello from my webhook!");
    return $response;
});

$app->run();

$token = Sync::auth(
  array("api_key" => API_KEY), // Tu API KEY
  array("id_user"=>$id_user) // ID de usuario
);

  print_r($token);
  // #Imprime: { token: "d5b33dcf996ac34fd2fa56782d72bff6"}

{ api_key: API_KEY }

{
    "id_external", // Opcional
    "fields", // Opcional
    "limit", // Opcional
    "skip", // Opcional
    "order" // Opcional
}

{
  "id_external", // Opcional
  "name"
}

$response = Sync::run(
    array("api_key" => SYNC_API_KEY),
    '/users', 
    array(), 
    'GET'
);

 $response = Sync::run(
  array("api_key" => API_KEY),
  '/users', 
  array("id_user"=>'5df859c4a7a6442757726ef4'), 
  'GET'
);

$response = Sync::run(
  array("api_key" => API_KEY),
  '/users', 
  array(
      "id_external"=> 'MIST030794',
      "name"=> 'Rey Misterio'
  ), 
  'POST'
);
$id_user = $response->id_user;

$response = Sync::run(
  array("api_key" => API_KEY),
  "/users/$id_user", 
  array(
      "name"=> 'El Santo Jr.'
  ), 
  'PUT'
);

$response = Sync::run(
  array("api_key" => API_KEY),
  "/users/$id_user", 
  array(), 
  'DELETE'
);

{ token: TOKEN }

{
    "fields", // opcional
    "limit", // opcional
    "skip", // opcional
    "order", // opcional
}

{
  "id_site", //opcional
  "id_site_organization", //opcional
  "id_site_organization_type", //opcional
  "fields", //opcional
  "limit", //opcional
  "skip", //opcional
  "order", //opciona
}

{
  "id_site", //opcional
  "id_site_organization", //opcional
  "id_site_organization_type", //opcional
}

// Consultar catálogos
$response = Sync::run(
  $token,
  "/catalogues/sites", 
  null,
  'GET'
);

{ token: TOKEN }

{}

{
  "id_site",
  "credentials"
}

{}

  // Consultar catálogos
$payload = array("id_site"=>"5da784f1f9de2a06483abec1");
$response = Sync::run(
  $token,
  "/catalogues/sites", 
  $payload,
  'GET'
);
$site = $response[0];
print_r($site)
/* Algo como esto:
{
    "id_site": "56cf5728784806f72b8b4568",
    .
    .
    .
    "credentials": [
        {
            "name": "username",
            "type": "text",
            "label": "Username",
            "s[$site->credentials[0]->name] = 'ACM010101ABC';
$credentials[$site->credentials[1]->name] = 'test';
$payload['credentials'] = $credentials;
$normalCredential = Sync::run(
  $token,
  "/credentials", 
  $payload,
  'POST'
);

$response = Sync::run(
  $token,
  "/credentials", 
  null,
  'GET'
);

// Consultar catálogos
$payload = array("id_site"=>"56cf5728784806f72b8b4569");
$response = Sync::run(
  $token,
  "/catalogues/sites", 
  $payload,
  'GET'
);

// Crear credenciales con Token o Autenticación de dos pasos
$twofaSite = $response[0];
$credentials = array();
$credentials[$twofaSite->credentials[0]->name] = 'test';
$credentials[$twofaSite->credentials[1]->name] = 'test';
$payload['credentials'] = $credentials;
$twofaCredential = Sync::run(
  $token,
  "/credentials", 
  $payload,
  'POST'
);
/* Regresa:
{
  id_credential: "5e27c17ca2fc48614c41b33e",
  id_job_uuid: "5e27c17ce6fea94c7c6b4193",
  id_job: "5e27c17ce6fea94c7c6b4194",
  is_new: 1,
  username: "t**t",
  ws: "wss://sync.paybook.com/v1/status/5e27c17ce6fea94c7c6b4194",
  status: "https://sync.paybook.com/v1/jobs/5e27c17ce6fea94c7c6b4194/status",
  twofa: "https://sync.paybook.com/v1/jobs/5e27c17ce6fea94c7c6b4194/twofa"
}
*/

// Consulta Status Credenciales twofa
$id_job = $twofaCredential->id_job;
$response = Sync::run(
  $token,
  "/jobs/$id_job/status", 
  null,
  'GET'
);
/* Regresa:
[
  {
      code: 100
  },
  {
      code: 101
  },
  {
      code: 410,
      address: "https://sync.paybook.com/v1/jobs/5e27c17ce6fea94c7c6b4194/twofa",
      twofa: [
          {
              name: "token",
              type: "text",
              label: "Enter any number sequence as a token"
          }
      ]
  }
]
*/
$is_twofa = False;
if($response[sizeof($response)->code] == 410) {
  $is_twofa = True;
}

// Manda TWOFA
$twofaToken = array("twofa" => array());
$twofaToken["twofa"][$response[2]->twofa[0]->name] = "123456";
$twofa = Sync::run(
  $token,
  "/jobs/$id_job/twofa", 
  $twofaToken, 
  'POST'
);
/* Regresa:
{
  rid: "ea1e848d-6ec2-454d-b296-0fe2b6c958c2",
  code: 200,
  errors: null,
  status: true,
  message: null,
  response: true
}
*/

$id_job = $credential->id_job;
$response = Sync::run(
  $token,
  "/jobs/$id_job/status", 
  null,
  'GET'
);
/* Regresa:
[
  {
      code: 100
  },
  {
      code: 101
  },
  {
      code: 410,
      address: "https://sync.paybook.com/v1/jobs/5e27c17ce6fea94c7c6b4194/twofa",
      twofa: [
          {
              name: "token",
              type: "text",
              label: "Enter any number sequence as a token"
          }
      ]
  },
  {
      code: 102
  },
  {
      code: 200
  }
]
*/

$response = Sync::run(
  $token,
  "/credentials", 
  null,
  'GET'
);

$id_credential = $credential->id_credential;
$response = Sync::run(
  $token,
  "/credentials/$id_credential", 
  null,
  'DELETE'
);

// Despues de crear una credencial (como en ejemplos anteriores)
$id_job = $twofaCredential->id_job;
$response = Sync::run(
  $token,
  "/jobs/$id_job/status", 
  null,
  'GET'
);

{ token: token }

{
    "id_account", //opcional
    "id_credential", //opcional
    "id_site", //opcional
    "id_site_organization", //opcional
    "id_site_organization_type", //opcional
    "is_disable", //opcional
    "fields", //opcional
    "limit", //opcional
    "skip", //opcional
    "order", //opcional
    "keywords", //opcional
    "skip_keywords" //opcional
}

$response = Sync::run(
  $token,
  "/accounts", 
  array("id_credential"=>$id_credential),
  'GET'
);

{ token: token }

{
    "id_transaction", //opcional
    "id_account", //opcional
    "id_credential", //opcional
    "id_site", //opcional
    "id_site_organization", //opcional
    "id_site_organization_type", //opcional
    "has_attachment", //opcional
    "is_disable", //opcional
    "dt_refresh_from", //opcional
    "dt_refresh_to", //opcional
    "dt_transaction_from", //opcional
    "dt_transaction_to", //opcional
    "fields", //opcional
    "limit", //opcional
    "skip", //opcional
    "order", //opcional
    "keywords", //opcional
    "skip_keywords" //opcional
}

{
    "id_transaction", //opcional
    "id_account", //opcional
    "id_credential", //opcional
    "id_site", //opcional
    "id_site_organization", //opcional
    "id_site_organization_type", //opcional
    "is_disable", //opcional
    "dt_refresh_from", //opcional
    "dt_refresh_to", //opcional
    "dt_transaction_from", //opcional
    "dt_transaction_to", //opcional
    "keywords", //opcional
    "skip_keywords" //opcional
}

$response = Sync::run(
  $token,
  "/transactions", 
  array(
      "id_credential"=>$id_credential,
      "limit"=>5
  ),
  'GET'
);

$response = Sync::run(
  $token,
  "/transactions/count", 
  array("id_credential"=>$id_credential),
  'GET'
);

{ api_key: API_KEY }

{}

{
    "url", 
    "events" //["credential_create","credential_update","refresh"]
}

{}

$response = Sync::run(
  array("api_key" => API_KEY),
  "/webhooks", 
  array(
      "url"=>'http://mydomain.ngrok.io/webhook'; // Tu endpoint donde recibiras la devolución de llamada, 
      "events"=>array("credential_create","credential_update","refresh")
  ),
  'POST'
);

$response = Sync::run(
  array("api_key" => API_KEY),
  "/webhooks", 
  null,
  'GET'
);

$id_webhook = $response->id_webhook;
$response = Sync::run(
  array("api_key" => API_KEY),
  "/webhooks/$id_webhook", 
  null,
  'DELETE'
);

{token: TOKEN}

{
    "id_account", //opcional
    "id_attachment_type", //opcional
    "id_credential", //opcional
    "id_transaction", //opcional
    "is_valid", //opcional
    "dt_refresh_from", //opcional
    "dt_refresh_to", //opcional
    "fields", //opcional
    "limit", //opcional
    "skip", //opcional
    "order", //opcional
    "keywords", //opcional
    "skip_keywords" //opcional
}

{
  "id_account", //opcional
  "id_attachment_type", //opcional
  "id_credential", //opcional
  "id_transaction", //opcional
  "is_valid", //opcional
  "dt_refresh_from", //opcional
  "dt_refresh_to", //opcional
  "keywords", //opcional
  "skip_keywords" //opcional
}

{}

{}

$response = Sync::run(
  $token,
  "/attachments", 
  array("id_credential"=>$id_credential),
  'GET'
);

$attachment = $response[0];
$attachmentUrl = $attachment->url;
$response = Sync::run(
  $token,
  $attachmentUrl, 
  null,
  'GET'
);

$attachment = $response[0];
$attachmentUrl = $attachment->url;
$response = Sync::run(
  $token,
  $attachmentUrl."/extra", 
  null,
  'GET'
);
bash
  composer 
json
[
   {
      "id_webhook":"5e17c4746cee651e7b03df34",
      "id_user":null,
      "is_disabled":0,
      "events":[
         "credential_create",
         "credential_update",
         "refresh"
      ],
      "url":"http://8e763e9e.ngrok.io/webhook",
      "delay":0,
      "ct_failed":0,
      "dt_created":"2020-01-10T00:25:24+00:00",
      "dt_modified":null
   }
]