Skip to content

Instantly share code, notes, and snippets.

@fasmide
Created November 14, 2011 22:27
Show Gist options
  • Save fasmide/1365402 to your computer and use it in GitHub Desktop.
Save fasmide/1365402 to your computer and use it in GitHub Desktop.
Danske Bank console app and php class
<?
class DanskeBank {
private $_magicKey;
private $_curlHandle;
public function __construct()
{
$this->_curlHandle = curl_init();
$data = array(
'os' => 'Android', //hmmf tried with php_uname('s') but then the service rejects logins
'model' => php_uname('m'),
'osVersion' => php_uname('v'),
'appVersion' => '2.3',
'manufacturer' => 'unknown',
'stopOnErrors' => array(),
'language' => 'DK',
'deviceId' => uniqid(), //this properly should be some persistent id…
'country' => 'DK'
);
$ch = curl_init('https://mb.danskebank.dk/smartphones/gmb.svc/CreateSession');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/json',
'Content-Encoding: UTF-8'
));
$this->processResponse(curl_exec($ch));
}
private function processResponse($response)
{
$decoded = json_decode($response);
if(json_last_error() != JSON_ERROR_NONE)
{
throw new Exception('Could not decode: "' . $response . "\"");
}
//set new magickey and check status.
if($decoded->Status->StatusCode != 0)
{
throw new Exception("Got error: '" . $decoded->Status->StatusText . "'\n");
}
$this->_magicKey = $decoded->MagicKey;
return $decoded;
}
public function getMagicKey()
{
return $this->_magicKey;
}
public function login($user, $pin)
{
$ch = curl_init('https://mb.danskebank.dk/smartphones/gmb.svc/Login?magicKey='.urlencode($this->getMagicKey()));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
'loginId' => $user,
'loginCode' => $pin
)));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/json',
'Content-Encoding: UTF-8'
));
return $this->processResponse(curl_exec($ch));
}
public function getAccounts()
{
$ch = curl_init('https://mb.danskebank.dk/smartphones/gmb.svc/Accounts?magicKey=' . urlencode($this->getMagicKey()));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
return $this->processResponse(curl_exec($ch));
}
public function getTransactions($accountID, $limit = 20, $page = 0, $search = "")
{
$query = http_build_query(array(
'magicKey' => $this->getMagicKey(),
'pageSize' => $limit,
'pageNumber' => $page,
'searchText' => $search
));
$ch = curl_init('https://mb.danskebank.dk/smartphones/gmb.svc/Accounts/'.$accountID.'/Transactions/Current?'.$query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
return $this->processResponse(curl_exec($ch));
}
}
<?
require_once('DanskeBank.php');
$db = new DanskeBank();
echo "User: ";
system('stty -echo');
$userID = trim(fgets(STDIN));
system('stty echo');
// add a new line since the users CR didn't echo
echo "\n";
echo "Pin code: ";
system('stty -echo');
$pin = trim(fgets(STDIN));
system('stty echo');
// add a new line since the users CR didn't echo
echo "\n";
try {
$login = $db->login($userID, $pin);
} catch(Exception $e) {
exit($e->getMessage());
}
echo "Hello " . $login->Name . "\n";
$accounts = $db->getAccounts();
echo "Found " . count($accounts->Accounts) . " accounts:\n";
$totalAvailableAmount = 0;
foreach($accounts->Accounts as $key => $account)
{
echo "\t[" . $key . "] " . $account->AccountNumber . "\t" . $account->AccountName . "\n";
$totalAvailableAmount += $account->AvailableAmount;
}
echo "Wooh! your good for " . $totalAvailableAmount . " DKR\n";
echo "Choose Account: ";
$accountIndex = trim(fgets(STDIN));
$transactions = $db->getTransactions($accounts->Accounts[$accountIndex]->AccountId);
echo "\tText\t\t\t\tAmount\tState\n";
foreach($transactions->Transactions as $transaction)
{
echo "\t". $transaction->Text;
$need = strlen($transaction->Text)/8 - 4;
while($need <0)
{
echo "\t";
$need++;
}
echo $transaction->Amount . "\t" . $transaction->StateText . "\n";
}
echo "Farvel og tak\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment