bank_payments 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +10 -0
  4. data/.rspec +2 -0
  5. data/.todo.reek +3 -0
  6. data/.travis.yml +7 -0
  7. data/Gemfile +8 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +92 -0
  10. data/Rakefile +6 -0
  11. data/bank_payments.gemspec +38 -0
  12. data/bank_specifications/teknisk_manual_swedbank.pdf +0 -0
  13. data/bin/console +14 -0
  14. data/bin/setup +8 -0
  15. data/lib/bank_payments.rb +42 -0
  16. data/lib/bank_payments/beneficiary.rb +69 -0
  17. data/lib/bank_payments/spisu_record.rb +130 -0
  18. data/lib/bank_payments/swedbank_export/address_record.rb +40 -0
  19. data/lib/bank_payments/swedbank_export/bank_record.rb +28 -0
  20. data/lib/bank_payments/swedbank_export/credit_memo_record.rb +49 -0
  21. data/lib/bank_payments/swedbank_export/enums.rb +22 -0
  22. data/lib/bank_payments/swedbank_export/field_definition.rb +21 -0
  23. data/lib/bank_payments/swedbank_export/file.rb +26 -0
  24. data/lib/bank_payments/swedbank_export/money_record.rb +21 -0
  25. data/lib/bank_payments/swedbank_export/name_record.rb +15 -0
  26. data/lib/bank_payments/swedbank_export/opening_record.rb +30 -0
  27. data/lib/bank_payments/swedbank_export/payment_record.rb +20 -0
  28. data/lib/bank_payments/swedbank_export/reason_record.rb +16 -0
  29. data/lib/bank_payments/swedbank_export/reconciliation_record.rb +54 -0
  30. data/lib/bank_payments/swedbank_export/sequence.rb +148 -0
  31. data/lib/bank_payments/swedbank_import/account_record.rb +18 -0
  32. data/lib/bank_payments/swedbank_import/address_record.rb +35 -0
  33. data/lib/bank_payments/swedbank_import/amount_converter.rb +38 -0
  34. data/lib/bank_payments/swedbank_import/file.rb +35 -0
  35. data/lib/bank_payments/swedbank_import/money_record.rb +49 -0
  36. data/lib/bank_payments/swedbank_import/name_record.rb +20 -0
  37. data/lib/bank_payments/swedbank_import/opening_record.rb +25 -0
  38. data/lib/bank_payments/swedbank_import/reconciliation_record.rb +35 -0
  39. data/lib/bank_payments/swedbank_import/sequence.rb +42 -0
  40. data/lib/bank_payments/transaction.rb +38 -0
  41. data/lib/bank_payments/version.rb +3 -0
  42. data/lib/core_extensions/numeric/spisu.rb +15 -0
  43. metadata +169 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 43707bdafa2f5e3084c528617a5d66b3e7d1552a
4
+ data.tar.gz: 9e0ce3837589276b89558e1e79e08827de49f24f
5
+ SHA512:
6
+ metadata.gz: def9f02a0bde03a699e0e27b534861c9294edfb13cab2ae9190f33720aff866933c43a1c80fc2f9b59cf1ab5e899eacb6648757b119c3e3b5d5b2d7dfeb9d933
7
+ data.tar.gz: 7cbeaeb48a7a595e847160f59edd14048c09423546a88a0d811320666473232577c93bbfe895c47f7dc73829c6fa3d5b4cca39f3588adf24188d24e93892378c
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ ---
2
+ Attribute:
3
+ enabled: false
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.5
6
+ after_success:
7
+ - "coveralls push"
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bank_payments.gemspec
4
+ gemspec
5
+
6
+ gem 'byebug'
7
+ gem 'yard'
8
+ gem 'coveralls', require: false
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Michael Litton, ApoEx AB
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,92 @@
1
+ # BankPayments
2
+
3
+ [![Coverage Status](https://coveralls.io/repos/github/apoex/bank_payments/badge.svg?branch=master)](https://coveralls.io/github/apoex/bank_payments?branch=master)
4
+ [![Build Status](https://travis-ci.org/apoex/bank_payments.svg?branch=master)](https://travis-ci.org/apoex/bank_payments)
5
+
6
+ Ruby implementation of bank payments. Starting with
7
+
8
+ * Swedbank International Payment, in SPISU-format
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'bank_payments'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install bank_payments
25
+
26
+ ## International Payments
27
+
28
+ Note that the Swedbank file is in an older format (SPISU). In about six months the Swedish standard will be based on ISO 20022 SEPA (Single Euro Payments Area). At the time of this writing (2016-08-09) there are two alternative gems for this standard:
29
+
30
+ * [Ruby gem for creating SEPA XML files](https://github.com/salesking/sepa_king)
31
+ * [SEPA ISO20022 XML message builder](https://github.com/conanite/sepa)
32
+
33
+ When possible one should consider moving over to that standard instead.
34
+
35
+ ## Usage
36
+
37
+ For anyone using a communications protocol to relay information from one party to another the important part is not the protocol itself. It's the information that needs to be sent. In order to send the information you need two pieces of information about the transaction: The beneciciary and the transactional data itself.
38
+
39
+ **Create a beneficiary**
40
+
41
+ ```ruby
42
+ b = BankPayments::Beneficiary.new do |b|
43
+ b.name = 'Some company AO'
44
+ b.address = 'Byvägen 12 731 00 Rala'
45
+ b.country_code = 'FI'
46
+ b.bank_id = 'HELSFIHH'
47
+ b.account = '10278'
48
+ end
49
+ ```
50
+ **Create a transaction**
51
+
52
+ ```ruby
53
+ t = BankPayments::Transaction.new(
54
+ amount_sek: 100_000,
55
+ amount_foreign: 1_189_104.93,
56
+ currency: 'JPY',
57
+ message: 'Payment, Bill 99',
58
+ pay_date: Date.new(2016,8,9),
59
+ reason: 101
60
+ )
61
+ ```
62
+
63
+ **Create a sequence, add payment and add it to a file**
64
+
65
+ ```ruby
66
+ s = BankPayments::SwedbankExport::Sequence.new(account, name, address)
67
+ s.add_transaction(b, t)
68
+
69
+ f = BankPayments::SwedbankExport::File.new('file_name')
70
+ f << s
71
+ f.to_file_data
72
+ ```
73
+
74
+ Download the result of `f.to_file_data` and upload it to the bank. The important part here is to setup the beneficiary and transactions correctly.
75
+
76
+ Use `yard` to generate the gem documentation which will give you more information about the implementation.
77
+
78
+ ## Development
79
+
80
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
81
+
82
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/apoex/bank_payments.
87
+
88
+
89
+ ## License
90
+
91
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
92
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bank_payments/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bank_payments"
8
+ spec.version = BankPayments::VERSION
9
+ spec.authors = ["Michael Litton"]
10
+ spec.email = ["michael.litton@apoex.se"]
11
+
12
+ spec.summary = "For generating payment files for various banks"
13
+ spec.homepage = "https://github.com/apoex/bank_payments"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ unless spec.respond_to?(:metadata)
19
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
20
+ end
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.12"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.5"
30
+ spec.add_development_dependency "simplecov", "~> 0.12"
31
+
32
+ # For IBAN validation
33
+ spec.add_runtime_dependency "ibandit", "~> 0.11"
34
+
35
+ # Ensure correct upcase for swedish characters. This wont be
36
+ # needed in Ruby 2.4
37
+ spec.add_runtime_dependency "unicode_utils", "~> 1.4"
38
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bank_payments"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,42 @@
1
+ require 'core_extensions/numeric/spisu'
2
+ Numeric.include CoreExtensions::Numeric::SPISU
3
+
4
+ require "bank_payments/version"
5
+ require "bank_payments/beneficiary"
6
+ require "bank_payments/transaction"
7
+ require "bank_payments/spisu_record"
8
+
9
+ require "bank_payments/swedbank_export/enums"
10
+ require "bank_payments/swedbank_export/field_definition"
11
+ require "bank_payments/swedbank_export/opening_record"
12
+ require "bank_payments/swedbank_export/name_record"
13
+ require "bank_payments/swedbank_export/address_record"
14
+ require "bank_payments/swedbank_export/bank_record"
15
+ require "bank_payments/swedbank_export/money_record"
16
+ require "bank_payments/swedbank_export/credit_memo_record"
17
+ require "bank_payments/swedbank_export/payment_record"
18
+ require "bank_payments/swedbank_export/reason_record"
19
+ require "bank_payments/swedbank_export/reconciliation_record"
20
+
21
+ require "bank_payments/swedbank_export/sequence"
22
+ require "bank_payments/swedbank_export/file"
23
+
24
+ require "bank_payments/swedbank_import/sequence"
25
+ require "bank_payments/swedbank_import/file"
26
+ require "bank_payments/swedbank_import/amount_converter"
27
+
28
+ require "bank_payments/swedbank_import/account_record"
29
+ require "bank_payments/swedbank_import/address_record"
30
+ require "bank_payments/swedbank_import/money_record"
31
+ require "bank_payments/swedbank_import/name_record"
32
+ require "bank_payments/swedbank_import/opening_record"
33
+ require "bank_payments/swedbank_import/reconciliation_record"
34
+
35
+ # Includes a simple representation of payments that can be serialized to a
36
+ # number of different banks. At the time of this writing we'll start by
37
+ # implementing the international payments for Swedbank.
38
+ #
39
+ # @author Michael Litton
40
+ module BankPayments
41
+ # Your code goes here...
42
+ end
@@ -0,0 +1,69 @@
1
+ module BankPayments
2
+
3
+ # Encapulates all the information about the destination of a payment.
4
+ # We choose this class name since this is what Swedbank seems to call
5
+ # it on their english homepage. Another viable option is also "Payee".
6
+ #
7
+ # @author Michael Litton
8
+ class Beneficiary
9
+
10
+ # Required regular fields
11
+ attr_accessor :name, :address, :country_code
12
+
13
+ # The BIC (Bank Identification Code) for countires within EU
14
+ attr_accessor :bank_id
15
+
16
+ # Can be empty for European payments
17
+ attr_accessor :bank_name
18
+
19
+ # IBAN
20
+ attr_accessor :account
21
+
22
+ # Configuration used for the address record in Swedbank SPISU
23
+ attr_accessor :account_type, :cost_carrier, :priority
24
+
25
+ # Creates a new Beneficiary object with defaults for part of the
26
+ # Swedbank payments
27
+ def initialize
28
+ yield self if block_given?
29
+
30
+ # Set sensible defaults
31
+ @account_type ||= BankPayments::SwedbankExport::AccountType::CURRENCY_ACCOUNT
32
+ @cost_carrier ||= BankPayments::SwedbankExport::CostResponsibility::OWN_EXPENSES
33
+ @priority ||= BankPayments::SwedbankExport::Priority::NORMAL
34
+ end
35
+
36
+ def eql?(other)
37
+ instance_values == other.instance_values
38
+ end
39
+
40
+ def hash
41
+ instance_values.hash
42
+ end
43
+
44
+ def instance_values
45
+ Hash[instance_variables.map do |variable|
46
+ [variable[1..-1], instance_variable_get(variable)]
47
+ end]
48
+ end
49
+
50
+ def to_spisu_records
51
+ name = BankPayments::SwedbankExport::NameRecord.new
52
+ name.name = @name
53
+
54
+ address = BankPayments::SwedbankExport::AddressRecord.new
55
+ address.address = @address
56
+ address.country_code = @country_code
57
+ address.account_type = @account_type
58
+ address.cost_carrier = @cost_carrier
59
+ address.priority = @priority
60
+
61
+ bank = BankPayments::SwedbankExport::BankRecord.new
62
+ bank.bank_id = @bank_id
63
+ bank.name = @bank_name || ''
64
+ bank.account = @account
65
+
66
+ [name, address, bank]
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,130 @@
1
+ require 'ostruct'
2
+ require 'unicode_utils/upcase'
3
+
4
+ module BankPayments
5
+
6
+ # An "abstract" class that is used by records that conforms to the SPISU-format
7
+ # for Swedbank international payments. It's a flat file format that lets
8
+ # implementations easily describe each row similar to how the documentation is
9
+ # structured by the bank.
10
+ #
11
+ # @author Michael Litton
12
+ class SpisuRecord
13
+ def initialize(data = ' ' * 80)
14
+ @data = data
15
+
16
+ yield self if block_given?
17
+ end
18
+
19
+ # Used to set the attributes in child records during
20
+ # initialization. An example definition is
21
+ #
22
+ # define_field :account, '2:9:N'
23
+ #
24
+ # which means that the account is available in position 2 to 9. The third
25
+ # part of the definition includes the format of the field which can be
26
+ #
27
+ # N: Numeric, zero pad to the left of the value
28
+ # AN: Alpha numeric, blank pad to the right of the value
29
+ #
30
+ # define_field :f1, '2:9:N'
31
+ # define_field :f2, '10:12:AN'
32
+ def self.define_field(field, definition)
33
+ field_class = BankPayments::SwedbankExport::FieldDefinition
34
+ (@fields ||= {})[field] = field_class.new(field, definition)
35
+ end
36
+
37
+ def self.defined_fields
38
+ @fields || {}
39
+ end
40
+
41
+ def self.definition_for(field)
42
+ @fields[field]
43
+ end
44
+
45
+ def set_text_value(start, stop, value)
46
+ value = UnicodeUtils.upcase(value, :sv)
47
+ set_value(start, stop, value, :ljust, ' ')
48
+ end
49
+
50
+ def set_numeric_value(start, stop, value)
51
+ set_value(start, stop, value, :rjust, '0')
52
+ end
53
+
54
+ def set_value(start, stop, value, direction = :ljust, padstr = ' ')
55
+ value = serialize_value(value)
56
+ length = stop - start + 1
57
+ @data[start-1,length] = value[0,length].send(direction, *[length, padstr])
58
+ end
59
+
60
+ def serialize_value(val)
61
+ if val.is_a?(Date)
62
+ val.strftime('%y%m%d')
63
+ else
64
+ val.to_s
65
+ end
66
+ end
67
+
68
+ def type=(type)
69
+ @data[0,1] = type.to_s
70
+ end
71
+
72
+ def type
73
+ @data[0,1]
74
+ end
75
+
76
+ def to_s
77
+ @data
78
+ end
79
+
80
+ # Attempts to handle serialization and deserialization of numeric and
81
+ # text values in the SPISU
82
+ def method_missing(method_name, *arguments, &block)
83
+ record_fields = self.class.defined_fields.keys
84
+ requested_field = method_name.to_s.sub('=','').to_sym
85
+
86
+ if record_fields.include?(requested_field)
87
+ is_setter_field = method_name =~ /=/
88
+ definition = self.class.definition_for(requested_field)
89
+
90
+ if is_setter_field
91
+ args = [definition.start, definition.stop, arguments.first]
92
+ case definition.type
93
+ when 'N' then set_numeric_value(*args)
94
+ when 'AN' then set_text_value(*args)
95
+ end
96
+ else
97
+ extract_field_value(definition)
98
+ end
99
+ else
100
+ super
101
+ end
102
+ end
103
+
104
+ def extract_field_value(definition)
105
+ return_value = @data[definition.start-1, definition.length]
106
+ (return_value.sub(/^0+/, "") || return_value).strip
107
+ end
108
+
109
+ # Used in some child classes in order to construct a ruby Date
110
+ # from the the underlying data. Only used for imports of transaction
111
+ # confirmations
112
+ def extract_date(field)
113
+ value = extract_raw_value(field)
114
+ parts = value.scan(/\d{2}/)
115
+ Date.new(2000 + parts[0].to_i, parts[1].to_i, parts[2].to_i)
116
+ end
117
+
118
+ def extract_raw_value(field)
119
+ definition = self.class.definition_for(field.to_sym)
120
+ @data[definition.start-1, definition.length]
121
+ end
122
+
123
+ # Ensure that we include the defined fields in the parent but still
124
+ # allow them to be overwritten. We can't use @@fields because certain
125
+ # fields that share their name between classes will be overwritten
126
+ def self.inherited(base)
127
+ base.instance_variable_set(:@fields, @fields)
128
+ end
129
+ end
130
+ end