Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
svobik7 committed May 10, 2015
1 parent 1a8f5cd commit f8580ce
Show file tree
Hide file tree
Showing 17 changed files with 937 additions and 0 deletions.
59 changes: 59 additions & 0 deletions Banking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* @link http://www.digitaldeals.cz/
* @copyright Copyright (c) 2014 Digital Deals s.r.o.
* @license http://www.digitaldeals.cz/license/
*/

namespace dlds\banking;

use yii\helpers\ArrayHelper;
use dlds\banking\interfaces\ApiBankAdapterInterface;

/**
* This is the main class of the dlds\mlm component that should be registered as an application component.
*
* @author Jiri Svoboda <[email protected]>
* @package mlm
*/
class Banking extends \yii\base\Component {

/**
* @var array adapters
*/
public $adapters;

/**
* Retrieves appropriate adapter based on give adapter key
*/
public function getAdapter($key)
{
$adapter = ArrayHelper::getValue($this->adapters, $key);

if ($adapter)
{
var_dump($adapter);
}

return false;
}

/**
* Downloads transactions list from banks using module adapters
* @return type
*/
public function downloadTransactionsList($adapter)
{
$transactions = new adapters\fio\TransactionList;

foreach ($this->adapters as $key => $adapter)
{
if ($adapter instanceof ApiBankAdapterInterface)
{
$transactions[$key] = $adapter->downloadTransactionsList();
}
}

return $transactions;
}
}
77 changes: 77 additions & 0 deletions adapters/fio/Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace dlds\banking\adapters\fio;

class Account {

/** @var string */
protected $accountNumber;

/** @var string */
protected $bankCode;

/** @var string */
protected $currency;

/** @var string */
protected $iban;

/** @var string */
protected $bic;

/**
* @param string $accountNumber
* @param string $bankCode
* @param string $currency
* @param string $iban
* @param string $bic
*/
public function __construct($accountNumber, $bankCode, $currency, $iban, $bic)
{
$this->accountNumber = $accountNumber;
$this->bankCode = $bankCode;
$this->currency = $currency;
$this->iban = $iban;
$this->bic = $bic;
}

/**
* @return string
*/
public function getAccountNumber()
{
return $this->accountNumber;
}

/**
* @return string
*/
public function getBankCode()
{
return $this->bankCode;
}

/**
* @return string
*/
public function getCurrency()
{
return $this->currency;
}

/**
* @return string
*/
public function getIban()
{
return $this->iban;
}

/**
* @return string
*/
public function getBic()
{
return $this->bic;
}
}
21 changes: 21 additions & 0 deletions adapters/fio/Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace dlds\apibank\adapters\fio;

class FioAdapter implements \dlds\banking\interfaces\ApiBankAdapterInterface {

public function downloadTransactionsLast()
{

}

public function downloadTransactionsSince($datetime)
{

}

public function uploadTransactionsList()
{

}
}
100 changes: 100 additions & 0 deletions adapters/fio/Downloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace dlds\banking\adapters\fio;

use FioApi\Exceptions\InternalErrorException;
use FioApi\Exceptions\TooGreedyException;
use GuzzleHttp\Client as Guzzle;

class Downloader {

/** @var UrlBuilder */
protected $urlBuilder;

/** @var \GuzzleHttp\Client */
protected $client;

/** @var string */
protected $certificatePath;

/**
* @param string $token
*/
public function __construct($token)
{
$this->urlBuilder = new UrlBuilder($token);
}

/**
* @param string $path
*/
public function setCertificatePath($path)
{
$this->certificatePath = $path;
}

public function getCertificatePath()
{
if ($this->certificatePath)
{
return $this->certificatePath;
}

//Key downloaded from https://www.geotrust.com/resources/root-certificates/
return __DIR__.'/keys/Equifax_Secure_Certificate_Authority.pem';
}

/**
* @return Guzzle
*/
public function getClient()
{
if (!$this->client)
{
$this->client = new Guzzle();
}
return $this->client;
}

/**
* @param \DateTime $from
* @param \DateTime $to
* @return TransactionList
*/
public function downloadFromTo(\DateTime $from, \DateTime $to)
{
$client = $this->getClient();
$url = $this->urlBuilder->buildPeriodsUrl($from, $to);

try
{
$response = $client->get($url, ['verify' => $this->getCertificatePath()]);
}
catch (\GuzzleHttp\Exception\BadResponseException $e)
{
if ($e->getCode() == 409)
{
throw new TooGreedyException('You can use one token for API call every 30 seconds', $e->getCode(), $e);
}
if ($e->getCode() == 500)
{
throw new InternalErrorException('Server returned 500 Internal Error (probably invalid token?)', $e->getCode(), $e);
}
throw $e;
}

return TransactionList::create($response->json([
'object' => true,
'big_int_strings' => true,
])->accountStatement);
}

/**
* @param \DateTime $since
* @return TransactionList
*/
public function downloadSince(\DateTime $since)
{
return $this->downloadFromTo($since, new \DateTime());
}
}
100 changes: 100 additions & 0 deletions adapters/fio/Uploader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace dlds\banking\adapters\fio;

use FioApi\Exceptions\InternalErrorException;
use FioApi\Exceptions\TooGreedyException;
use GuzzleHttp\Client as Guzzle;

class Uploader {

/** @var UrlBuilder */
protected $urlBuilder;

/** @var \GuzzleHttp\Client */
protected $client;

/** @var string */
protected $certificatePath;

/**
* @param string $token
*/
public function __construct($token)
{
$this->urlBuilder = new UrlBuilder($token);
}

/**
* @param string $path
*/
public function setCertificatePath($path)
{
$this->certificatePath = $path;
}

public function getCertificatePath()
{
if ($this->certificatePath)
{
return $this->certificatePath;
}

//Key downloaded from https://www.geotrust.com/resources/root-certificates/
return __DIR__.'/keys/Equifax_Secure_Certificate_Authority.pem';
}

/**
* @return Guzzle
*/
public function getClient()
{
if (!$this->client)
{
$this->client = new Guzzle();
}
return $this->client;
}

/**
* @param \DateTime $from
* @param \DateTime $to
* @return TransactionList
*/
public function downloadFromTo(\DateTime $from, \DateTime $to)
{
$client = $this->getClient();
$url = $this->urlBuilder->buildPeriodsUrl($from, $to);

try
{
$response = $client->get($url, ['verify' => $this->getCertificatePath()]);
}
catch (\GuzzleHttp\Exception\BadResponseException $e)
{
if ($e->getCode() == 409)
{
throw new TooGreedyException('You can use one token for API call every 30 seconds', $e->getCode(), $e);
}
if ($e->getCode() == 500)
{
throw new InternalErrorException('Server returned 500 Internal Error (probably invalid token?)', $e->getCode(), $e);
}
throw $e;
}

return TransactionList::create($response->json([
'object' => true,
'big_int_strings' => true,
])->accountStatement);
}

/**
* @param \DateTime $since
* @return TransactionList
*/
public function downloadSince(\DateTime $since)
{
return $this->downloadFromTo($since, new \DateTime());
}
}
Loading

0 comments on commit f8580ce

Please sign in to comment.