1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Libraries/OFX.php

250 lines
7.4 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Libraries;
2016-01-20 00:07:31 +01:00
// https://github.com/denvertimothy/OFX
2016-05-03 13:05:35 +02:00
use Log;
2016-01-20 00:07:31 +01:00
use SimpleXMLElement;
2017-01-30 20:40:43 +01:00
use Utils;
2016-01-20 00:07:31 +01:00
class OFX
{
public $bank;
public $request;
public $response;
public $responseHeader;
public $responseBody;
2017-01-30 20:40:43 +01:00
2016-01-20 00:07:31 +01:00
public function __construct($bank, $request)
{
$this->bank = $bank;
$this->request = $request;
}
2017-01-30 20:40:43 +01:00
2016-01-20 00:07:31 +01:00
public function go()
{
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $this->bank->url);
curl_setopt($c, CURLOPT_POST, 1);
2016-05-03 13:05:35 +02:00
// User-Agent: http://www.ofxhome.com/ofxforum/viewtopic.php?pid=108091#p108091
curl_setopt($c, CURLOPT_HTTPHEADER, ['Content-Type: application/x-ofx', 'User-Agent: httpclient']);
2016-01-20 00:07:31 +01:00
curl_setopt($c, CURLOPT_POSTFIELDS, $this->request);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
2016-05-15 12:58:11 +02:00
2016-01-20 00:07:31 +01:00
$this->response = curl_exec($c);
2016-05-15 12:58:11 +02:00
2016-05-03 13:05:35 +02:00
if (Utils::isNinjaDev()) {
2017-07-10 20:03:35 +02:00
//Log::info(print_r($this->response, true));
2016-05-03 13:05:35 +02:00
}
2016-05-15 12:58:11 +02:00
2016-01-20 00:07:31 +01:00
curl_close($c);
2016-05-15 12:58:11 +02:00
2016-01-20 00:07:31 +01:00
$tmp = explode('<OFX>', $this->response);
$this->responseHeader = $tmp[0];
$this->responseBody = '<OFX>'.$tmp[1];
}
2017-01-30 20:40:43 +01:00
2016-01-20 00:07:31 +01:00
public function xml()
{
$xml = $this->responseBody;
2016-05-03 13:05:35 +02:00
$xml = self::closeTags($xml);
2016-01-20 00:07:31 +01:00
$x = new SimpleXMLElement($xml);
return $x;
}
2016-05-15 12:58:11 +02:00
2016-05-03 13:05:35 +02:00
public static function closeTags($x)
2016-01-20 00:07:31 +01:00
{
2016-05-03 13:05:35 +02:00
$x = preg_replace('/\s+/', '', $x);
2017-01-30 20:40:43 +01:00
2016-05-03 13:05:35 +02:00
return preg_replace('/(<([^<\/]+)>)(?!.*?<\/\2>)([^<]+)/', '\1\3</\2>', $x);
2016-01-20 00:07:31 +01:00
}
}
class Finance
{
public $banks;
}
class Bank
{
public $logins; // array of class User
2017-01-30 17:05:31 +01:00
public $finance; // the Finance object that hold this Bank object
public $fid;
2016-01-20 00:07:31 +01:00
public $org;
public $url;
2017-01-30 20:40:43 +01:00
2016-01-20 00:07:31 +01:00
public function __construct($finance, $fid, $url, $org)
{
$this->finance = $finance;
$this->fid = $fid;
$this->url = $url;
$this->org = $org;
}
}
class Login
{
public $accounts;
public $bank;
public $id;
public $pass;
2017-07-11 12:22:11 +02:00
public $ofxVersion;
public $appVersion;
2017-01-30 20:40:43 +01:00
2016-01-20 00:07:31 +01:00
public function __construct($bank, $id, $pass)
{
$this->bank = $bank;
$this->id = $id;
$this->pass = $pass;
}
2017-01-30 20:40:43 +01:00
2016-01-20 00:07:31 +01:00
public function setup()
{
$ofxRequest =
2017-01-30 17:05:31 +01:00
"OFXHEADER:100\n".
"DATA:OFXSGML\n".
2017-07-11 12:22:11 +02:00
"VERSION:" . $this->ofxVersion . "\n".
2017-01-30 17:05:31 +01:00
"SECURITY:NONE\n".
"ENCODING:USASCII\n".
"CHARSET:1252\n".
"COMPRESSION:NONE\n".
"OLDFILEUID:NONE\n".
"NEWFILEUID:NONE\n".
"\n".
"<OFX>\n".
"<SIGNONMSGSRQV1>\n".
"<SONRQ>\n".
"<DTCLIENT>20110412162900.000[-7:MST]\n".
'<USERID>'.$this->id."\n".
'<USERPASS>'.$this->pass."\n".
"<GENUSERKEY>N\n".
"<LANGUAGE>ENG\n".
"<FI>\n".
'<ORG>'.$this->bank->org."\n".
'<FID>'.$this->bank->fid."\n".
"</FI>\n".
"<APPID>QWIN\n".
2017-07-11 12:22:11 +02:00
"<APPVER>" . $this->appVersion . "\n".
2017-01-30 17:05:31 +01:00
"</SONRQ>\n".
"</SIGNONMSGSRQV1>\n".
"<SIGNUPMSGSRQV1>\n".
"<ACCTINFOTRNRQ>\n".
'<TRNUID>'.md5(time().$this->bank->url.$this->id)."\n".
"<ACCTINFORQ>\n".
"<DTACCTUP>19900101\n".
"</ACCTINFORQ>\n".
"</ACCTINFOTRNRQ> \n".
"</SIGNUPMSGSRQV1>\n".
"</OFX>\n";
2016-01-20 00:07:31 +01:00
$o = new OFX($this->bank, $ofxRequest);
$o->go();
$x = $o->xml();
foreach ($x->xpath('/OFX/SIGNUPMSGSRSV1/ACCTINFOTRNRS/ACCTINFORS/ACCTINFO/BANKACCTINFO/BANKACCTFROM') as $a) {
$this->accounts[] = new Account($this, (string) $a->ACCTID, 'BANK', (string) $a->ACCTTYPE, (string) $a->BANKID);
}
foreach ($x->xpath('/OFX/SIGNUPMSGSRSV1/ACCTINFOTRNRS/ACCTINFORS/ACCTINFO/CCACCTINFO/CCACCTFROM') as $a) {
$this->accounts[] = new Account($this, (string) $a->ACCTID, 'CC');
}
}
}
class Account
{
public $login;
public $id;
public $type;
public $subType;
public $bankId;
public $ledgerBalance;
public $availableBalance;
public $response;
2017-01-30 20:40:43 +01:00
2016-01-20 00:07:31 +01:00
public function __construct($login, $id, $type, $subType = null, $bankId = null)
{
$this->login = $login;
$this->id = $id;
$this->type = $type;
$this->subType = $subType;
$this->bankId = $bankId;
}
2017-01-30 20:40:43 +01:00
2016-01-20 00:07:31 +01:00
public function setup($includeTransactions = true)
{
$ofxRequest =
2017-01-30 17:05:31 +01:00
"OFXHEADER:100\n".
"DATA:OFXSGML\n".
2017-07-11 12:22:11 +02:00
"VERSION:" . $this->login->ofxVersion . "\n".
2017-01-30 17:05:31 +01:00
"SECURITY:NONE\n".
"ENCODING:USASCII\n".
"CHARSET:1252\n".
"COMPRESSION:NONE\n".
"OLDFILEUID:NONE\n".
"NEWFILEUID:NONE\n".
"\n".
"<OFX>\n".
"<SIGNONMSGSRQV1>\n".
"<SONRQ>\n".
"<DTCLIENT>20110412162900.000[-7:MST]\n".
'<USERID>'.$this->login->id."\n".
'<USERPASS>'.$this->login->pass."\n".
"<LANGUAGE>ENG\n".
"<FI>\n".
'<ORG>'.$this->login->bank->org."\n".
'<FID>'.$this->login->bank->fid."\n".
"</FI>\n".
"<APPID>QWIN\n".
2017-07-11 12:22:11 +02:00
"<APPVER>" . $this->login->appVersion . "\n".
2017-01-30 17:05:31 +01:00
"</SONRQ>\n".
"</SIGNONMSGSRQV1>\n";
2016-01-20 00:07:31 +01:00
if ($this->type == 'BANK') {
$ofxRequest .=
2017-01-30 17:05:31 +01:00
" <BANKMSGSRQV1>\n".
" <STMTTRNRQ>\n".
' <TRNUID>'.md5(time().$this->login->bank->url.$this->id)."\n".
" <STMTRQ>\n".
" <BANKACCTFROM>\n".
' <BANKID>'.$this->bankId."\n".
' <ACCTID>'.$this->id."\n".
' <ACCTTYPE>'.$this->subType."\n".
" </BANKACCTFROM>\n".
" <INCTRAN>\n".
" <DTSTART>20110301\n".
' <INCLUDE>'.($includeTransactions ? 'Y' : 'N')."\n".
" </INCTRAN>\n".
" </STMTRQ>\n".
" </STMTTRNRQ>\n".
" </BANKMSGSRQV1>\n";
2016-01-20 00:07:31 +01:00
} elseif ($this->type == 'CC') {
$ofxRequest .=
2017-01-30 17:05:31 +01:00
" <CREDITCARDMSGSRQV1>\n".
" <CCSTMTTRNRQ>\n".
' <TRNUID>'.md5(time().$this->login->bank->url.$this->id)."\n".
" <CCSTMTRQ>\n".
" <CCACCTFROM>\n".
' <ACCTID>'.$this->id."\n".
" </CCACCTFROM>\n".
" <INCTRAN>\n".
" <DTSTART>20110320\n".
' <INCLUDE>'.($includeTransactions ? 'Y' : 'N')."\n".
" </INCTRAN>\n".
" </CCSTMTRQ>\n".
" </CCSTMTTRNRQ>\n".
" </CREDITCARDMSGSRQV1>\n";
2016-01-20 00:07:31 +01:00
}
$ofxRequest .=
2017-01-30 17:05:31 +01:00
'</OFX>';
2016-01-20 00:07:31 +01:00
$o = new OFX($this->login->bank, $ofxRequest);
$o->go();
$this->response = $o->response;
$x = $o->xml();
$a = $x->xpath('/OFX/*/*/*/LEDGERBAL/BALAMT');
2017-01-30 20:40:43 +01:00
$this->ledgerBalance = (float) $a[0];
2016-01-20 00:07:31 +01:00
$a = $x->xpath('/OFX/*/*/*/AVAILBAL/BALAMT');
if (isset($a[0])) {
2017-01-30 20:40:43 +01:00
$this->availableBalance = (float) $a[0];
2016-01-20 00:07:31 +01:00
}
}
}