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 ISR creditor reference validation
  • Loading branch information
sdespont committed Feb 27, 2018
commit 11cd1e26a22139f0de1c8977a127267d39121c53
23 changes: 23 additions & 0 deletions src/Z38/SwissPayment/TransactionInformation/ISRCreditTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function __construct($instructionId, $endToEndId, Money\Money $amount, IS
));
}

if (self::modulo10(substr($creditorReference, 0, -1)) != (int) substr($creditorReference, -1)) {
throw new InvalidArgumentException('Invalid ISR creditor reference.');
}

$this->instructionId = (string) $instructionId;
$this->endToEndId = (string) $endToEndId;
$this->amount = $amount;
Expand Down Expand Up @@ -109,4 +113,23 @@ public function setCreditorDetails($creditorName, PostalAddressInterface $credit
$this->creditorName = $creditorName;
$this->creditorAddress = $creditorAddress;
}

/**
* Creates Modulo10 recursive check digit
*
* @param string $number Number to create recursive check digit off.
*
* @return int Recursive check digit.
*/
private static function modulo10(string $number)
{
$moduloTable = [0, 9, 4, 6, 8, 2, 7, 1, 3, 5];

$next = 0;
for ($i = 0; $i < strlen($number); $i++) {
$next = $moduloTable[($next + intval(substr($number, $i, 1))) % 10];
}

return (int) (10 - $next) % 10;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected function buildMessage()
'e2e-010',
new Money\CHF(20000), // CHF 200.00
new ISRParticipant('01-95106-8'),
'210000000003139471430009017'
'6019701803969733825'
);
$transaction12->setCreditorDetails(
'Fritz Bischof',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ public function testInvalidAmount()
);
}

/**
* @covers ::__construct
* @expectedException \InvalidArgumentException
*/
public function testInvalidCreditorReference()
{
$transfer = new ISRCreditTransfer(
'id000',
'name',
new Money\CHF(100),
new ISRParticipant('01-25083-7'),
'120000000000234478943216891'
);
}

/**
* @covers ::setRemittanceInformation
* @expectedException \LogicException
Expand All @@ -38,7 +53,7 @@ public function testSetRemittanceInformation()
'id000',
'name',
new Money\CHF(100),
new ISRParticipant('10-2424-4'),
new ISRParticipant('01-25083-7'),
'120000000000234478943216899'
);

Expand Down