This repository has been archived by the owner on Sep 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit aa4cb71
Showing
10 changed files
with
307 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/_build | ||
/deps | ||
erl_crash.dump | ||
*.ez | ||
mix.lock | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
language: erlang | ||
otp_release: | ||
- 17.1 | ||
before_install: | ||
- git clone https://github.com/elixir-lang/elixir | ||
- cd elixir && git checkout v1.0.4 && make && cd .. | ||
before_script: | ||
- export PATH=`pwd`/elixir/bin:$PATH | ||
- mix local.hex --force | ||
- mix deps.get | ||
script: "MIX_ENV=test mix do deps.get, test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
%% The MIT License | ||
|
||
%% Copyright (c) 2015 Florian J. Breunig | ||
|
||
%% Permission is hereby granted, free of charge, to any person obtaining a copy | ||
%% of this software and associated documentation files (the "Software"), to deal | ||
%% in the Software without restriction, including without limitation the rights | ||
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
%% copies of the Software, and to permit persons to whom the Software is | ||
%% furnished to do so, subject to the following conditions: | ||
|
||
%% The above copyright notice and this permission notice shall be included in | ||
%% all copies or substantial portions of the Software. | ||
|
||
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
%% THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
MT940 parser for Elixir | ||
======================= | ||
|
||
[![Build Status](https://travis-ci.org/my-flow/mt940.svg?branch=master)](https://travis-ci.org/my-flow/mt940) | ||
|
||
MT940 is a standard structured SWIFT Customer Statement message. In short, it | ||
is an electronic bank account statement which has been developed by SWIFT. It | ||
is a end of day statement file which details all entries booked to an account. | ||
|
||
|
||
## Basic usage | ||
|
||
Include a dependency in your `mix.exs`: | ||
|
||
```elixir | ||
deps: [{:mt940, "~> 0.1.0"}, ...] | ||
``` | ||
|
||
`use MT940` and `parse` the raw input: | ||
|
||
``` | ||
":20:TELEWIZORY S.A. | ||
:25:BPHKPLPK/320000546101 | ||
:28C:00084/001 | ||
:60F:C031002PLN40000,00 | ||
:61:0310201020C20000,00FMSCNONREF//8327000090031789 | ||
Card transaction | ||
:86:020?00Wyplata-(dysp/przel)?2008106000760000777777777777?2115617? 22INFO INFO INFO INFO INFO INFO 1 END?23INFO INFO INFO INFO INFO INFO 2 END?24ZAPLATA ZA FABRYKATY DO TUB?25 - 200 S ZTUK, TRANZY STORY-?26300 SZT GR544 I OPORNIKI-5?2700 SZT GTX847 FAKTURA 333/ 2?28003.?3010600076?310000777777777777?32HUTA SZKLA TOPIC UL PRZEMY?33SLOWA 67 32-669 WROCLAW?38PL081060007600007777777 | ||
77777 | ||
:62F:C020325PLN50040,00" | ||
iex(2)> use MT940 | ||
nil | ||
iex(3)> parse raw | ||
[":20:": "TELEWIZORY S.A.", ":25:": {"BPHKPLPK", "320000546101"}, | ||
":28C:": {84, 1}, ":60F:": {"C", "031002", "PLN", "40000,00"}, | ||
":61:": ["031020", "1020", "C", "", "20000,00", "FMSC", "NONREF", "//", | ||
"8327000090031789", "\n", "Card transaction"], | ||
":86:": {"020", | ||
#HashDict<[{20, "08106000760000777777777777"}, {28, "003."}, | ||
{21, "15617? 22INFO INFO INFO INFO INFO INFO 1 END"}, | ||
{25, " - 200 S ZTUK, TRANZY STORY-"}, {27, "00 SZT GTX847 FAKTURA 333/ 2"}, | ||
{32, "HUTA SZKLA TOPIC UL PRZEMY"}, {0, "Wyplata-(dysp/przel)"}, | ||
{24, "ZAPLATA ZA FABRYKATY DO TUB"}, {26, "300 SZT GR544 I OPORNIKI-5"}, | ||
{38, "PL08106000760000777777777777"}, {33, "SLOWA 67 32-669 WROCLAW"}, | ||
{30, "10600076"}, {31, "0000777777777777"}, | ||
{23, "INFO INFO INFO INFO INFO INFO 2 END"}]>}, | ||
":62F:": {"C", "020325", "PLN", "50040,00"}] | ||
``` | ||
|
||
|
||
## Copyright & License | ||
|
||
Copyright (c) 2015 [Florian J. Breunig](http://www.my-flow.com) | ||
|
||
Licensed under MIT, see LICENSE file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# This file is responsible for configuring your application | ||
# and its dependencies with the aid of the Mix.Config module. | ||
use Mix.Config | ||
|
||
# This configuration is loaded before any dependency and is restricted | ||
# to this project. If another project depends on this project, this | ||
# file won't be loaded nor affect the parent project. For this reason, | ||
# if you want to provide default values for your application for third- | ||
# party users, it should be done in your mix.exs file. | ||
|
||
# Sample configuration: | ||
# | ||
# config :logger, :console, | ||
# level: :info, | ||
# format: "$date $time [$level] $metadata$message\n", | ||
# metadata: [:user_id] | ||
|
||
# It is also possible to import configuration files, relative to this | ||
# directory. For example, you can emulate configuration per environment | ||
# by uncommenting the line below and defining dev.exs, test.exs and such. | ||
# Configuration from the imported file will override the ones defined | ||
# here (which is why it is important to import them last). | ||
# | ||
# import_config "#{Mix.env}.exs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule MT940 do | ||
|
||
defmacro __using__(_) do | ||
quote do | ||
import MT940.Parser | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
defmodule MT940.Parser do | ||
|
||
def parse(raw) when is_binary(raw) do | ||
raw | ||
|> String.strip | ||
|> String.split(Regex.compile!("(\R?):\\d{1,2}\\w?:()"), on: :all_but_first, trim: true) | ||
|> Stream.map(&String.strip/1) | ||
|> Stream.chunk(2) | ||
|> Stream.map(fn [k, v] -> {k |> remove_newline |> String.to_atom(), split(k, v)} end) | ||
|> Enum.into(Keyword.new) | ||
end | ||
|
||
|
||
defp split(":25:", v) do | ||
case v |> String.contains?("/") do | ||
true -> ~r/^(\d{8}|\w{8,11})\/(\d{1,23})(\D{3})?$/ | ||
false -> ~r/^(\w+)(\D{3})?$/ | ||
end | ||
|> Regex.run(v, capture: :all_but_first) | ||
|> List.to_tuple | ||
end | ||
|
||
|
||
defp split(":28C:", v) do | ||
~r/^(\d+)\/(\d+)$/ | ||
|> Regex.run(v, capture: :all_but_first) | ||
|> Enum.map(&String.to_integer/1) | ||
|> List.to_tuple | ||
end | ||
|
||
|
||
defp split(":60" <> <<_>> <> ":", v) do | ||
v |> remove_newline |> balance | ||
end | ||
|
||
|
||
defp split(":61:", v) do | ||
~r/^(\d{6})(\d{4}?)(C|RC|D|RD)(\D)?([0-9,]{4,15})(\w{4})(NONREF|.{1,22})(\/\/)?(\w{0,16})?([\s\R]{1,2})?(.{0,34})?$/ | ||
|> Regex.run(v, capture: :all_but_first) | ||
end | ||
|
||
|
||
defp split(":86:", v) do | ||
s = ~r/^(\d{3})(.)/ | ||
|> Regex.run(v, capture: :all_but_first) | ||
|
||
case s do | ||
[code, separator] -> | ||
fields = v | ||
|> remove_newline | ||
|> String.split(Regex.compile!("(^\\d{3})?(\\#{separator})\\d{2}()"), on: :all_but_first, trim: true) | ||
|> Stream.chunk(2) | ||
|> Stream.map(fn [k, v] -> {String.to_integer(k), v} end) | ||
|> Enum.into(HashDict.new) | ||
{code, fields} | ||
_ -> | ||
Regex.split(~r/\R/, v) | ||
end | ||
end | ||
|
||
|
||
defp split(":62" <> <<_>> <> ":", v) do | ||
v |> remove_newline |> balance | ||
end | ||
|
||
|
||
defp split(":90" <> <<_>> <> ":", v) do | ||
~r/^(\d{1,5})(\w{3})([0-9,]{1,15})$/ | ||
|> Regex.run(v, capture: :all_but_first) | ||
|> List.to_tuple | ||
end | ||
|
||
|
||
defp split(_, v) when is_binary(v) do | ||
v | ||
end | ||
|
||
|
||
defp balance(v) do | ||
~r/^(\w{1})(\d{6})(\w{3})([0-9,]{1,15}).*$/ | ||
|> Regex.run(v, capture: :all_but_first) | ||
|> List.to_tuple | ||
end | ||
|
||
|
||
defp remove_newline(string) when is_binary(string) do | ||
~r(\R) |> Regex.replace(string, "") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule Mt940.Mixfile do | ||
use Mix.Project | ||
|
||
def project do | ||
[ | ||
app: :mt940, | ||
version: "0.1.0", | ||
elixir: "~> 1.0", | ||
description: description, | ||
package: package, | ||
deps: deps | ||
] | ||
end | ||
|
||
|
||
def application do | ||
[applications: [:logger]] | ||
end | ||
|
||
|
||
defp deps do | ||
[] | ||
end | ||
|
||
|
||
defp description do | ||
""" | ||
MT940 parser for Elixir. | ||
""" | ||
end | ||
|
||
|
||
defp package do | ||
[ | ||
files: ["lib", "priv", "mix.exs", "README*", "readme*", "LICENSE*", "license*"], | ||
contributors: ["Florian J. Breunig"], | ||
licenses: ["MIT"], | ||
links: %{"GitHub" => "https://github.com/my-flow/mt940"} | ||
] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule ParserTest do | ||
use ExUnit.Case | ||
use MT940 | ||
|
||
|
||
test "short name of account owner" do | ||
assert [":20:": "TELEWIZORY S.A."] == ":20:TELEWIZORY S.A." |> parse | ||
end | ||
|
||
|
||
test "account identification option A with IBAN" do | ||
assert [":25:": {"PL25106000760000888888888888"}] == ":25:PL25106000760000888888888888" |> parse | ||
end | ||
|
||
|
||
test "account identification option B with 8 chars BIC" do | ||
assert [":25:": {"BPHKPLPK", "320000546101"}] == ":25:BPHKPLPK/320000546101" |> parse | ||
end | ||
|
||
|
||
test "account identification option B with 8 digits bank code" do | ||
assert [":25:": {"20041111", "301086000"}] == ":25:20041111/301086000" |> parse | ||
end | ||
|
||
|
||
test "account identification option B with 8 digits bank code and currency" do | ||
assert [":25:": {"20041111", "301086000", "EUR"}] == ":25:20041111/301086000EUR" |> parse | ||
end | ||
|
||
|
||
test "account identification option B with 8 digits bank code and currency and new line" do | ||
assert [":25:": {"20041111", "301086000", "EUR"}] == ":25:20041111/301086000EUR\r\n" |> parse | ||
end | ||
|
||
|
||
test "Statement Number/Sequence Number" do | ||
assert [":28C:": {84, 1}] == ":28C:00084/001" |> parse | ||
end | ||
|
||
|
||
test "Opening balance" do | ||
assert [":60F:": {"C", "031002", "PLN", "40000,00"}] == ":60F:C031002PLN40000,00" |> parse | ||
end | ||
|
||
|
||
test "Opening balance with new line" do | ||
assert [":60F:": {"C", "150401", "EUR", "2446,61"}] == ":60F:C150401EUR2446,61\r\n" |> parse | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ExUnit.start() |