|
| 1 | +package ofx |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + |
| 6 | + "github.com/mayswind/ezbookkeeping/pkg/models" |
| 7 | +) |
| 8 | + |
| 9 | +const ofxVersion1 = "100" |
| 10 | +const ofxVersion2 = "200" |
| 11 | + |
| 12 | +const ofxDefaultTimezoneOffset = "+00:00" |
| 13 | + |
| 14 | +// ofxAccountType represents account type in open financial exchange (ofx) file |
| 15 | +type ofxAccountType string |
| 16 | + |
| 17 | +// OFX account types |
| 18 | +const ( |
| 19 | + ofxCheckingAccount ofxAccountType = "CHECKING" |
| 20 | + ofxSavingsAccount ofxAccountType = "SAVINGS" |
| 21 | + ofxMoneyMarketAccount ofxAccountType = "MONEYMRKT" |
| 22 | + ofxLineOfCreditAccount ofxAccountType = "CREDITLINE" |
| 23 | + ofxCertificateOfDepositAccount ofxAccountType = "CD" |
| 24 | +) |
| 25 | + |
| 26 | +// ofxTransactionType represents transaction type in open financial exchange (ofx) file |
| 27 | +type ofxTransactionType string |
| 28 | + |
| 29 | +// OFX transaction types |
| 30 | +const ( |
| 31 | + ofxGenericCreditTransaction ofxTransactionType = "CREDIT" |
| 32 | + ofxGenericDebitTransaction ofxTransactionType = "DEBIT" |
| 33 | + ofxInterestTransaction ofxTransactionType = "INT" |
| 34 | + ofxDividendTransaction ofxTransactionType = "DIV" |
| 35 | + ofxFIFeeTransaction ofxTransactionType = "FEE" |
| 36 | + ofxServiceChargeTransaction ofxTransactionType = "SRVCHG" |
| 37 | + ofxDepositTransaction ofxTransactionType = "DEP" |
| 38 | + ofxATMTransaction ofxTransactionType = "ATM" |
| 39 | + ofxPOSTransaction ofxTransactionType = "POS" |
| 40 | + ofxTransferTransaction ofxTransactionType = "XFER" |
| 41 | + ofxCheckTransaction ofxTransactionType = "CHECK" |
| 42 | + ofxElectronicPaymentTransaction ofxTransactionType = "PAYMENT" |
| 43 | + ofxCashWithdrawalTransaction ofxTransactionType = "CASH" |
| 44 | + ofxDirectDepositTransaction ofxTransactionType = "DIRECTDEP" |
| 45 | + ofxMerchantInitiatedDebitTransaction ofxTransactionType = "DIRECTDEBIT" |
| 46 | + ofxRepeatingPaymentTransaction ofxTransactionType = "REPEATPMT" |
| 47 | + ofxHoldTransaction ofxTransactionType = "HOLD" |
| 48 | + ofxOtherTransaction ofxTransactionType = "OTHER" |
| 49 | +) |
| 50 | + |
| 51 | +var ofxTransactionTypeMapping = map[ofxTransactionType]models.TransactionType{ |
| 52 | + ofxGenericCreditTransaction: models.TRANSACTION_TYPE_EXPENSE, |
| 53 | + ofxGenericDebitTransaction: models.TRANSACTION_TYPE_EXPENSE, |
| 54 | + ofxDividendTransaction: models.TRANSACTION_TYPE_INCOME, |
| 55 | + ofxFIFeeTransaction: models.TRANSACTION_TYPE_EXPENSE, |
| 56 | + ofxServiceChargeTransaction: models.TRANSACTION_TYPE_EXPENSE, |
| 57 | + ofxDepositTransaction: models.TRANSACTION_TYPE_INCOME, |
| 58 | + ofxTransferTransaction: models.TRANSACTION_TYPE_TRANSFER, |
| 59 | + ofxCheckTransaction: models.TRANSACTION_TYPE_EXPENSE, |
| 60 | + ofxElectronicPaymentTransaction: models.TRANSACTION_TYPE_EXPENSE, |
| 61 | + ofxCashWithdrawalTransaction: models.TRANSACTION_TYPE_EXPENSE, |
| 62 | + ofxDirectDepositTransaction: models.TRANSACTION_TYPE_INCOME, |
| 63 | + ofxMerchantInitiatedDebitTransaction: models.TRANSACTION_TYPE_EXPENSE, |
| 64 | + ofxRepeatingPaymentTransaction: models.TRANSACTION_TYPE_EXPENSE, |
| 65 | +} |
| 66 | + |
| 67 | +// ofxFile represents the struct of open financial exchange (ofx) file |
| 68 | +type ofxFile struct { |
| 69 | + XMLName xml.Name `xml:"OFX"` |
| 70 | + FileHeader *ofxFileHeader |
| 71 | + BankMessageResponseV1 *ofxBankMessageResponseV1 `xml:"BANKMSGSRSV1"` |
| 72 | + CreditCardMessageResponseV1 *ofxCreditCardMessageResponseV1 `xml:"CREDITCARDMSGSRSV1"` |
| 73 | +} |
| 74 | + |
| 75 | +// ofxFileHeader represents the struct of open financial exchange (ofx) file header |
| 76 | +type ofxFileHeader struct { |
| 77 | + OFXVersion string |
| 78 | + OFXDataVersion string |
| 79 | + Security string |
| 80 | + OldFileUid string |
| 81 | + NewFileUid string |
| 82 | +} |
| 83 | + |
| 84 | +// ofxBankMessageResponseV1 represents the struct of open financial exchange (ofx) bank message response v1 |
| 85 | +type ofxBankMessageResponseV1 struct { |
| 86 | + StatementTransactionResponse *ofxBankStatementTransactionResponse `xml:"STMTTRNRS"` |
| 87 | +} |
| 88 | + |
| 89 | +// ofxCreditCardMessageResponseV1 represents the struct of open financial exchange (ofx) credit card message response v1 |
| 90 | +type ofxCreditCardMessageResponseV1 struct { |
| 91 | + StatementTransactionResponse *ofxCreditCardStatementTransactionResponse `xml:"CCSTMTTRNRS"` |
| 92 | +} |
| 93 | + |
| 94 | +// ofxBankStatementTransactionResponse represents the struct of open financial exchange (ofx) bank statement transaction response |
| 95 | +type ofxBankStatementTransactionResponse struct { |
| 96 | + StatementResponse *ofxBankStatementResponse `xml:"STMTRS"` |
| 97 | +} |
| 98 | + |
| 99 | +// ofxCreditCardStatementTransactionResponse represents the struct of open financial exchange (ofx) credit card statement transaction response |
| 100 | +type ofxCreditCardStatementTransactionResponse struct { |
| 101 | + StatementResponse *ofxCreditCardStatementResponse `xml:"CCSTMTRS"` |
| 102 | +} |
| 103 | + |
| 104 | +// ofxBankStatementResponse represents the struct of open financial exchange (ofx) bank statement response |
| 105 | +type ofxBankStatementResponse struct { |
| 106 | + DefaultCurrency string `xml:"CURDEF"` |
| 107 | + AccountFrom *ofxBankAccount `xml:"BANKACCTFROM"` |
| 108 | + TransactionList *ofxBankTransactionList `xml:"BANKTRANLIST"` |
| 109 | +} |
| 110 | + |
| 111 | +// ofxCreditCardStatementResponse represents the struct of open financial exchange (ofx) credit card statement response |
| 112 | +type ofxCreditCardStatementResponse struct { |
| 113 | + DefaultCurrency string `xml:"CURDEF"` |
| 114 | + AccountFrom *ofxCreditCardAccount `xml:"CCACCTFROM"` |
| 115 | + TransactionList *ofxCreditCardTransactionList `xml:"BANKTRANLIST"` |
| 116 | +} |
| 117 | + |
| 118 | +// ofxBankAccount represents the struct of open financial exchange (ofx) bank account |
| 119 | +type ofxBankAccount struct { |
| 120 | + BankId string `xml:"BANKID"` |
| 121 | + BranchId string `xml:"BRANCHID"` |
| 122 | + AccountId string `xml:"ACCTID"` |
| 123 | + AccountType ofxAccountType `xml:"ACCTTYPE"` |
| 124 | + AccountKey string `xml:"ACCTKEY"` |
| 125 | +} |
| 126 | + |
| 127 | +// ofxCreditCardAccount represents the struct of open financial exchange (ofx) credit card account |
| 128 | +type ofxCreditCardAccount struct { |
| 129 | + AccountId string `xml:"ACCTID"` |
| 130 | + AccountKey string `xml:"ACCTKEY"` |
| 131 | +} |
| 132 | + |
| 133 | +// ofxBankTransactionList represents the struct of open financial exchange (ofx) bank transaction list |
| 134 | +type ofxBankTransactionList struct { |
| 135 | + StartDate string `xml:"DTSTART"` |
| 136 | + EndDate string `xml:"DTEND"` |
| 137 | + StatementTransactions []*ofxBankStatementTransaction `xml:"STMTTRN"` |
| 138 | +} |
| 139 | + |
| 140 | +// ofxCreditCardTransactionList represents the struct of open financial exchange (ofx) credit card transaction list |
| 141 | +type ofxCreditCardTransactionList struct { |
| 142 | + StartDate string `xml:"DTSTART"` |
| 143 | + EndDate string `xml:"DTEND"` |
| 144 | + StatementTransactions []*ofxCreditCardStatementTransaction `xml:"STMTTRN"` |
| 145 | +} |
| 146 | + |
| 147 | +// ofxBasicStatementTransaction represents the struct of open financial exchange (ofx) basic statement transaction |
| 148 | +type ofxBasicStatementTransaction struct { |
| 149 | + TransactionId string `xml:"FITID"` |
| 150 | + TransactionType ofxTransactionType `xml:"TRNTYPE"` |
| 151 | + PostedDate string `xml:"DTPOSTED"` |
| 152 | + Amount string `xml:"TRNAMT"` |
| 153 | + Name string `xml:"NAME"` |
| 154 | + Payee *ofxPayee `xml:"PAYEE"` |
| 155 | + Memo string `xml:"MEMO"` |
| 156 | + Currency string `xml:"CURRENCY"` |
| 157 | + OriginalCurrency string `xml:"ORIGCURRENCY"` |
| 158 | +} |
| 159 | + |
| 160 | +// ofxBankStatementTransaction represents the struct of open financial exchange (ofx) bank statement transaction |
| 161 | +type ofxBankStatementTransaction struct { |
| 162 | + ofxBasicStatementTransaction |
| 163 | + AccountTo *ofxCreditCardAccount `xml:"BANKACCTTO"` |
| 164 | +} |
| 165 | + |
| 166 | +// ofxCreditCardStatementTransaction represents the struct of open financial exchange (ofx) credit card statement transaction |
| 167 | +type ofxCreditCardStatementTransaction struct { |
| 168 | + ofxBasicStatementTransaction |
| 169 | + AccountTo *ofxCreditCardAccount `xml:"CCACCTTO"` |
| 170 | +} |
| 171 | + |
| 172 | +// ofxPayee represents the struct of open financial exchange (ofx) payee info |
| 173 | +type ofxPayee struct { |
| 174 | + Name string `xml:"NAME"` |
| 175 | + Address1 string `xml:"ADDR1"` |
| 176 | + Address2 string `xml:"ADDR2"` |
| 177 | + Address3 string `xml:"ADDR3"` |
| 178 | + City string `xml:"CITY"` |
| 179 | + State string `xml:"STATE"` |
| 180 | + PostalCode string `xml:"POSTALCODE"` |
| 181 | + Country string `xml:"COUNTRY"` |
| 182 | + Phone string `xml:"PHONE"` |
| 183 | +} |
0 commit comments