# encoding: utf-8 require 'spec_helper' describe SEPA::CreditTransfer do let(:credit_transfer) { SEPA::CreditTransfer.new name: 'Schuldner GmbH', bic: 'BANKDEFFXXX', iban: 'DE87200500001234567890' } describe :new do it 'should accept missing options' do expect { SEPA::CreditTransfer.new }.to_not raise_error end end describe :add_transaction do it 'should add valid transactions' do 3.times do credit_transfer.add_transaction(credit_transfer_transaction) end expect(credit_transfer).to have(3).transactions end it 'should fail for invalid transaction' do expect { credit_transfer.add_transaction name: '' }.to raise_error(ArgumentError) end end describe :to_xml do context 'for invalid debtor' do it 'should fail' do expect { SEPA::CreditTransfer.new.to_xml }.to raise_error(RuntimeError) end end context 'for valid debtor' do context 'without BIC (IBAN-only)' do subject do sct = SEPA::CreditTransfer.new name: 'Schuldner GmbH', iban: 'DE87200500001234567890' sct.add_transaction name: 'Telekomiker AG', bic: 'PBNKDEFF370', iban: 'DE37112589611964645802', amount: 102.50, reference: 'XYZ-1234/123', remittance_information: 'Rechnung vom 22.08.2013' sct end it 'should create valid XML file' do expect(subject.to_xml).to validate_against('pain.001.003.03.xsd') end it 'should fail for pain.001.002.03' do expect { subject.to_xml(SEPA::PAIN_001_002_03) }.to raise_error(RuntimeError) end end context 'with BIC' do subject do sct = credit_transfer sct.add_transaction name: 'Telekomiker AG', bic: 'PBNKDEFF370', iban: 'DE37112589611964645802', amount: 102.50, reference: 'XYZ-1234/123', remittance_information: 'Rechnung vom 22.08.2013' sct end it 'should validate against pain.001.002.03' do expect(subject.to_xml('pain.001.002.03')).to validate_against('pain.001.002.03.xsd') end it 'should validate against pain.001.003.03' do expect(subject.to_xml('pain.001.003.03')).to validate_against('pain.001.003.03.xsd') end end context 'without requested_date given' do subject do sct = credit_transfer sct.add_transaction name: 'Telekomiker AG', bic: 'PBNKDEFF370', iban: 'DE37112589611964645802', amount: 102.50, reference: 'XYZ-1234/123', remittance_information: 'Rechnung vom 22.08.2013' sct.add_transaction name: 'Amazonas GmbH', iban: 'DE27793589132923472195', amount: 59.00, reference: 'XYZ-5678/456', remittance_information: 'Rechnung vom 21.08.2013' sct.to_xml end it 'should create valid XML file' do expect(subject).to validate_against('pain.001.003.03.xsd') end it 'should have message_identification' do subject.should have_xml('//Document/CstmrCdtTrfInitn/GrpHdr/MsgId', /SEPA-KING\/[0-9]+/) end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/PmtInfId', /SEPA-KING\/[0-9]+\/1/) end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/ReqdExctnDt', Date.today.next.iso8601) end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/PmtMtd', 'TRF') end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/BtchBookg', 'true') end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/NbOfTxs', '2') end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CtrlSum', '161.50') end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/Dbtr/Nm', 'Schuldner GmbH') end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/DbtrAcct/Id/IBAN', 'DE87200500001234567890') end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/DbtrAgt/FinInstnId/BIC', 'BANKDEFFXXX') end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf[1]/PmtId/EndToEndId', 'XYZ-1234/123') subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf[2]/PmtId/EndToEndId', 'XYZ-5678/456') end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf[1]/Amt/InstdAmt', '102.50') subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf[2]/Amt/InstdAmt', '59.00') end it 'should contain for every BIC given' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf[1]/CdtrAgt/FinInstnId/BIC', 'PBNKDEFF370') subject.should_not have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf[2]/CdtrAgt') end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf[1]/Cdtr/Nm', 'Telekomiker AG') subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf[2]/Cdtr/Nm', 'Amazonas GmbH') end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf[1]/CdtrAcct/Id/IBAN', 'DE37112589611964645802') subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf[2]/CdtrAcct/Id/IBAN', 'DE27793589132923472195') end it 'should contain ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf[1]/RmtInf/Ustrd', 'Rechnung vom 22.08.2013') subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf/CdtTrfTxInf[2]/RmtInf/Ustrd', 'Rechnung vom 21.08.2013') end end context 'with different requested_date given' do subject do sct = credit_transfer sct.add_transaction(credit_transfer_transaction.merge requested_date: Date.today + 1) sct.add_transaction(credit_transfer_transaction.merge requested_date: Date.today + 2) sct.add_transaction(credit_transfer_transaction.merge requested_date: Date.today + 2) sct.to_xml end it 'should contain two payment_informations with ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[1]/ReqdExctnDt', (Date.today + 1).iso8601) subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[2]/ReqdExctnDt', (Date.today + 2).iso8601) subject.should_not have_xml('//Document/CstmrCdtTrfInitn/PmtInf[3]') end it 'should contain two payment_informations with different ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[1]/PmtInfId', /SEPA-KING\/[0-9]+\/1/) subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[2]/PmtInfId', /SEPA-KING\/[0-9]+\/2/) end end context 'with different batch_booking given' do subject do sct = credit_transfer sct.add_transaction(credit_transfer_transaction.merge batch_booking: false) sct.add_transaction(credit_transfer_transaction.merge batch_booking: true) sct.add_transaction(credit_transfer_transaction.merge batch_booking: true) sct.to_xml end it 'should contain two payment_informations with ' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[1]/BtchBookg', 'false') subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[2]/BtchBookg', 'true') subject.should_not have_xml('//Document/CstmrCdtTrfInitn/PmtInf[3]') end end context 'with transactions containing different group criteria' do subject do sct = credit_transfer sct.add_transaction(credit_transfer_transaction.merge requested_date: Date.today + 1, batch_booking: false, amount: 1) sct.add_transaction(credit_transfer_transaction.merge requested_date: Date.today + 1, batch_booking: true, amount: 2) sct.add_transaction(credit_transfer_transaction.merge requested_date: Date.today + 2, batch_booking: false, amount: 4) sct.add_transaction(credit_transfer_transaction.merge requested_date: Date.today + 2, batch_booking: true, amount: 8) sct.to_xml end it 'should contain multiple payment_informations' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[1]/ReqdExctnDt', (Date.today + 1).iso8601) subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[1]/BtchBookg', 'false') subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[2]/ReqdExctnDt', (Date.today + 1).iso8601) subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[2]/BtchBookg', 'true') subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[3]/ReqdExctnDt', (Date.today + 2).iso8601) subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[3]/BtchBookg', 'false') subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[4]/ReqdExctnDt', (Date.today + 2).iso8601) subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[4]/BtchBookg', 'true') end it 'should have multiple control sums' do subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[1]/CtrlSum', '1.00') subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[2]/CtrlSum', '2.00') subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[3]/CtrlSum', '4.00') subject.should have_xml('//Document/CstmrCdtTrfInitn/PmtInf[4]/CtrlSum', '8.00') end end end end end