Skip to content
This repository has been archived by the owner on Nov 7, 2021. It is now read-only.

ISR improvements #19

Merged
merged 4 commits into from
Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add ISRParticipant account number validation
  • Loading branch information
sdespont committed Feb 27, 2018
commit ff7b91a8dca17c0cbcebeb8ce66a30d56bc813d8
9 changes: 9 additions & 0 deletions src/Z38/SwissPayment/ISRParticipant.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public function __construct($number)
} else {
throw new InvalidArgumentException('ISR participant number is not properly formatted.');
}

if (substr($this->number, 0, 2) !== '01'
&& substr($this->number, 0, 2) !== '03') {
throw new InvalidArgumentException('ISR participant number must start by 01 [CHF] or 03 [EURO].');
}

if (PostalAccount::calculateCheckDigit(substr($this->number, 0, 8)) !== (int) substr($this->number, -1)) {
throw new InvalidArgumentException('Postal account number has an invalid check digit.');
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Z38/SwissPayment/PostalAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function asDom(DOMDocument $doc)
return $root;
}

private static function calculateCheckDigit($number)
public static function calculateCheckDigit($number)
{
$lookup = [0, 9, 4, 6, 8, 2, 7, 1, 3, 5];
$carry = 0;
Expand Down
7 changes: 4 additions & 3 deletions tests/Z38/SwissPayment/Tests/ISRParticipantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ public function testFormat()
public function validSamples()
{
return [
['80-2-2'],
['80-0470-3'],
['123456789'],
['01-394971-8'],
['010059136'],
['01-137-5'],
];
}

public function invalidSamples()
{
return [
['01-394971-9'],
['01-7777777-2'],
['80-470-3-1'],
['12345678'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected function buildMessage()
'instr-010',
'e2e-010',
new Money\CHF(20000), // CHF 200.00
new ISRParticipant('80-5928-4'),
new ISRParticipant('01-1439-8'),
'210000000003139471430009017'
);

Expand All @@ -146,7 +146,7 @@ protected function buildMessage()
'instr-010',
'e2e-010',
new Money\CHF(20000), // CHF 200.00
new ISRParticipant('80-5928-4'),
new ISRParticipant('01-95106-8'),
'210000000003139471430009017'
);
$transaction12->setCreditorDetails(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testCreditorDetails()
'id000',
'name',
new Money\CHF(100),
new ISRParticipant('10-2424-4'),
new ISRParticipant('01-25083-7'),
'120000000000234478943216899'
);

Expand Down