Import OFX file: Failed to parse OFX file

Hi there,

I got the error “Failed to parse OFX file” after attempting to upload OFX file downloaded from ING Direct Australia.

Below is my OFX file content:

OFXHEADER:100
DATA:OFXSGML
VERSION:102
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>0
<SEVERITY>INFO
</STATUS>
<DTSERVER>20181217111357
<LANGUAGE>ENG
</SONRS>
</SIGNONMSGSRSV1>
<BANKMSGSRSV1>
<STMTTRNRS>
<TRNUID>1
<STATUS>
<CODE>0
<SEVERITY>INFO
</STATUS>
<STMTRS>
<CURDEF>AUD
<BANKTRANLIST>
<STMTTRN>
<TRNTYPE>CREDIT
<DTPOSTED>20181214000000
<TRNAMT>337.50
<FITID>****17
<MEMO>J E LI****      Beau & Rhiannon  - Receipt ****17
</STMTTRN>
...
</BANKTRANLIST>
</STMTRS>
</STMTTRNRS>
</BANKMSGSRSV1>
</OFX>

Any help would be much appreciated.

Thanks
Nico

Below is the log associated the error described previously.

[2018-12-17 01:55:00] production.ERROR: ***RuntimeException*** [0] : /home/donnangc/invoice.donnang.com.au/vendor/asgrim/ofxparser/lib/OfxParser/Parser.php 
[Line 83] => Failed to parse OFX: array (   
0 =>    LibXMLError::__set_state(array(      'level' => 3,      'code' => 68,      'column' => 30,      'message' => 'xmlParseEntityRef: no name ',      'file' => '',      'line' => 27,   )),   
1 =>    LibXMLError::__set_state(array(      'level' => 3,      'code' => 68,      'column' => 30,      'message' => 'xmlParseEntityRef: no name ',      'file' => '',      'line' => 321,   )),   
2 =>    LibXMLError::__set_state(array(      'level' => 3,      'code' => 68,      'column' => 30,      'message' => 'xmlParseEntityRef: no name ',      'file' => '',      'line' => 405,   )),   
3 =>    LibXMLError::__set_state(array(      'level' => 3,      'code' => 68,      'column' => 30,      'message' => 'xmlParseEntityRef: no name ',      'file' => '',      'line' => 594,   )),   
4 =>    LibXMLError::__set_state(array(      'level' => 3,      'code' => 68,      'column' => 30,      'message' => 'xmlParseEntityRef: no name ',      'file' => '',      'line' => 860,   )),   
5 =>    LibXMLError::__set_state(array(      'level' => 3,      'code' => 68,      'column' => 30,      'message' => 'xmlParseEntityRef: no name ',      'file' => '',      'line' => 944,   )),   
6 =>    LibXMLError::__set_state(array(      'level' => 3,      'code' => 68,      'column' => 30,      'message' => 'xmlParseEntityRef: no name ',      'file' => '',      'line' => 993,   )), )  
{"context":"PHP","user_id":1,"account_id":"1","user_name":"Nico","method":"POST","user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36","locale":"en_GB","ip":"***.***.***.***","count":2,"is_console":"no","is_api":"no","db_server":"mysql","url":"bank_accounts/import_ofx"} []

Are you using invoiceninja.com or are you self hosting?

Hi Hillel,

It’s self-hosted, version 4.5.1 and installed with Softaculous.

I dug a little and discovered that SGML string cannot be parsed into XML string because of the presence of special characters in the memo tag. When a tag value contains the at sign (@), the double quote sign ("), the inferior sign (<), the superior sign (>) and/or the hat sign (^), the parser fails.

Not sure if those characters are supposed to be in the OFX file but I have created a patch to convert @ and " into their respective html codes and the XML parsing comes out nicely. However, I couldn’t make it work with <, > (which I assumed shouldn’t be in the OFX file) and ^.

Below is the code added to invoiceninja/vendor/asgrim/ofxparser/lib/OfxParser/Parser.php at line 120:

$sgml=preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $sgml);

Then the function createDateTimeFromStr from OFX.php returned

***RuntimeException*** [0] : /Users/nic/Sites/ninja/vendor/asgrim/ofxparser/lib/OfxParser/Ofx.php 
[Line 295] => Failed to initialize DateTime for string:   

Some of the fields passed on to this function didn’t exist so I had to test beforehand that the $dateString was set and had a value.

Below is the code added to invoiceninja/vendor/asgrim/ofxparser/lib/OfxParser/Ofx.php at line 268:

if((!isset($dateString) || trim($dateString) === '')) return null;

It looks like a similar fix was added in a newer version of the OFX library we’re using.

https://github.com/asgrim/ofxparser/commit/02aef21c495d1382ce87e7f23af1a8acd354c5f0

Hi Nico, did you get this sorted? I also am with ING Direct here in Australia. When I download an OFX file and import I get the parsing error.

thanks Rod

Hi @mccomr,

Yes, I have been able to make it work and sent my fix to ofxparser (https://github.com/asgrim/ofxparser/commit/5ce19a5ed34c2da412b819d67daa1d4e5569433e). The pull request was approved and merged with the master branch but I don’t think asgrim has released a new version since. Therefore I assume invoiceninja is still using the buggy version of the ofx library v-1.2.2.

If you apply the fix in the Parser.php file you will be sorted until you update invoiceninja.

Cheers
Nico