Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get final batch id from SEPA message #28

Merged
merged 2 commits into from
Feb 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions lib/sepa_king/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module SEPA
class Message
include ActiveModel::Validations

attr_reader :account, :transactions
attr_reader :account, :grouped_transactions

validates_presence_of :transactions
validate do |record|
Expand All @@ -21,14 +21,19 @@ class Message
class_attribute :account_class, :transaction_class, :xml_main_tag, :known_schemas

def initialize(account_options={})
@transactions = []
@grouped_transactions = {}
@account = account_class.new(account_options)
end

def add_transaction(options)
transaction = transaction_class.new(options)
raise ArgumentError.new(transaction.errors.full_messages.join("\n")) unless transaction.valid?
@transactions << transaction
@grouped_transactions[transaction_group(transaction)] ||= []
@grouped_transactions[transaction_group(transaction)] << transaction
end

def transactions
grouped_transactions.values.flatten
end

# @return [String] xml
Expand Down Expand Up @@ -68,6 +73,20 @@ def message_identification
@message_identification ||= "SEPA-KING/#{Time.now.to_i}"
end

# Returns the id of the batch to which the given transaction belongs
# Identified based upon the reference of the transaction
def batch_id(transaction_reference)
grouped_transactions.each do |group, transactions|
if transactions.select { |transaction| transaction.reference == transaction_reference }.any?
return payment_information_identification(group)
end
end
end

def batches
grouped_transactions.keys.collect { |group| payment_information_identification(group) }
end

private
# @return {Hash<Symbol=>String>} xml schema information used in output xml
def xml_schema(schema_name)
Expand All @@ -89,10 +108,13 @@ def build_group_header(builder)
end

# Unique and consecutive identifier (used for the <PmntInf> blocks)
def payment_information_identification
@payment_information_counter ||= 0
@payment_information_counter += 1
"#{message_identification}/#{@payment_information_counter}"
def payment_information_identification(group)
"#{message_identification}/#{grouped_transactions.keys.index(group)+1}"
end

# Returns a key to determine the group to which the transaction belongs
def transaction_group(transaction)
transaction
end
end
end
14 changes: 6 additions & 8 deletions lib/sepa_king/message/credit_transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ class CreditTransfer < Message

private
# Find groups of transactions which share the same values of some attributes
def grouped_transactions
transactions.group_by do |transaction|
{ requested_date: transaction.requested_date,
batch_booking: transaction.batch_booking,
service_level: transaction.service_level
}
end
def transaction_group(transaction)
{ requested_date: transaction.requested_date,
batch_booking: transaction.batch_booking,
service_level: transaction.service_level
}
end

def build_payment_informations(builder)
# Build a PmtInf block for every group of transactions
grouped_transactions.each do |group, transactions|
# All transactions with the same requested_date are placed into the same PmtInf block
builder.PmtInf do
builder.PmtInfId(payment_information_identification)
builder.PmtInfId(payment_information_identification(group))
builder.PmtMtd('TRF')
builder.BtchBookg(group[:batch_booking])
builder.NbOfTxs(transactions.length)
Expand Down
18 changes: 8 additions & 10 deletions lib/sepa_king/message/direct_debit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ class DirectDebit < Message

private
# Find groups of transactions which share the same values of some attributes
def grouped_transactions
transactions.group_by do |transaction|
{ requested_date: transaction.requested_date,
local_instrument: transaction.local_instrument,
sequence_type: transaction.sequence_type,
batch_booking: transaction.batch_booking,
account: transaction.creditor_account || account
}
end
def transaction_group(transaction)
{ requested_date: transaction.requested_date,
local_instrument: transaction.local_instrument,
sequence_type: transaction.sequence_type,
batch_booking: transaction.batch_booking,
account: transaction.creditor_account || account
}
end

def build_payment_informations(builder)
# Build a PmtInf block for every group of transactions
grouped_transactions.each do |group, transactions|
builder.PmtInf do
builder.PmtInfId(payment_information_identification)
builder.PmtInfId(payment_information_identification(group))
builder.PmtMtd('DD')
builder.BtchBookg(group[:batch_booking])
builder.NbOfTxs(transactions.length)
Expand Down
30 changes: 30 additions & 0 deletions spec/direct_debit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,36 @@
end
end

describe :batch_id do
it 'returns the id of the batch where the given transactions belongs to (1 batch)' do
direct_debit.add_transaction(direct_debt_transaction(reference: "EXAMPLE REFERENCE"))

expect(direct_debit.batch_id("EXAMPLE REFERENCE")).to match(/SEPA-KING\/[0-9]+\/1/)
end

it 'returns the id of the batch where the given transactions belongs to (2 batches)' do
direct_debit.add_transaction(direct_debt_transaction(reference: "EXAMPLE REFERENCE 1"))
direct_debit.add_transaction(direct_debt_transaction(reference: "EXAMPLE REFERENCE 2", requested_date: Date.today.next.next))
direct_debit.add_transaction(direct_debt_transaction(reference: "EXAMPLE REFERENCE 3"))

expect(direct_debit.batch_id("EXAMPLE REFERENCE 1")).to match(/SEPA-KING\/[0-9]+\/1/)
expect(direct_debit.batch_id("EXAMPLE REFERENCE 2")).to match(/SEPA-KING\/[0-9]+\/2/)
expect(direct_debit.batch_id("EXAMPLE REFERENCE 3")).to match(/SEPA-KING\/[0-9]+\/1/)
end
end

describe :batches do
it 'returns an array of batch ids in the sepa message' do
direct_debit.add_transaction(direct_debt_transaction(reference: "EXAMPLE REFERENCE 1"))
direct_debit.add_transaction(direct_debt_transaction(reference: "EXAMPLE REFERENCE 2", requested_date: Date.today.next.next))
direct_debit.add_transaction(direct_debt_transaction(reference: "EXAMPLE REFERENCE 3"))

expect(direct_debit.batches).to have(2).items
expect(direct_debit.batches[0]).to match(/SEPA-KING\/[0-9]+/)
expect(direct_debit.batches[1]).to match(/SEPA-KING\/[0-9]+/)
end
end

describe :to_xml do
context 'for invalid creditor' do
it 'should fail' do
Expand Down
7 changes: 4 additions & 3 deletions spec/support/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ def credit_transfer_transaction
}
end

def direct_debt_transaction
def direct_debt_transaction(attributes={})
{ name: 'Müller & Schmidt oHG',
bic: 'GENODEF1JEV',
iban: 'DE68210501700012345678',
amount: 750.00,
reference: 'XYZ/2013-08-ABO/6789',
reference: attributes[:reference] || 'XYZ/2013-08-ABO/6789',
remittance_information: 'Vielen Dank für Ihren Einkauf!',
mandate_id: 'K-08-2010-42123',
mandate_date_of_signature: Date.new(2010,7,25)
mandate_date_of_signature: Date.new(2010,7,25),
requested_date: attributes[:requested_date]
}
end