Skip to content

Instantly share code, notes, and snippets.

@analogic
Created June 4, 2019 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save analogic/2a7b846cefc4625d98280fcedc3af88e to your computer and use it in GitHub Desktop.
Save analogic/2a7b846cefc4625d98280fcedc3af88e to your computer and use it in GitHub Desktop.
Minimalistic Creditas Bank API client
<?php
class Creditas {
private $id;
private $secret;
public function __construct(string $id, string $secret)
{
$this->id = $id;
$this->secret = $secret;
}
private function get(string $method, ?array $parameters = [])
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.creditas.cz/oam/v1/'.$method,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($parameters),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer ".$this->secret,
"Authorization-Bearer: ".$this->secret,
"Content-Type: application/json",
"Accept: application/json",
"Pragma: no-cache",
"Expires: -1",
"Accept-Language: cs",
"Cache-Control: no-cache,no-store,must-revalidate,max-age=-1,private"
]
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #01: " . $err;
} else {
$response = json_decode($response, true);
if ($response) return $response;
if (array_key_exists("error", $response)) throw new \RuntimeException($response["error_description"]);
throw new \RuntimeException("no response");
}
}
public function account(string $id)
{
return $this->get('account/current/get', ['accountId' => $id]);
}
public function lastTransactions(string $id, int $num = 20): array
{
return $this->get('account/transaction/search', ['accountId' => $id, 'filter' => [], 'pageItemCount' => $num, "pageIndex" => 0]);
}
}
$account = '...client id...';
$secret = '...your secret...';
$c = new Creditas($account, $secret);
//$c->account($account);
print_r($c->lastTransactions($account));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment