Skip to content

Instantly share code, notes, and snippets.

@denysvitali
Last active February 14, 2017 22:57
Show Gist options
  • Select an option

  • Save denysvitali/86f99be1a0e118d1ed28fe20849d8926 to your computer and use it in GitHub Desktop.

Select an option

Save denysvitali/86f99be1a0e118d1ed28fe20849d8926 to your computer and use it in GitHub Desktop.
Generate IS (Inpay Slip by Postfinance) codeline with PHP
<?php
// Check for more info: https://www.postfinance.ch/binp/postfinance/public/dam.Ii-X5NgtAixO8cQPvja46blV6d7cZCyGUscxO15L5S8.spool/content/dam/pf/de/doc/consult/manual/dldata/efin_recdescr_man_en.pdf
// Thanks to:
// - https://gist.github.com/gbaudoin/9066c1a3d6ab68351635
// - https://gist.github.com/christianmeichtry/9348451
// - http://dnando.github.io/blog/2014/09/23/check-digit-computation-swiss-pay-slips/
$slipType = "01";
$amount = 3949.75;
$referenceNumber = "12 00000 00000 23447 89432 16899";
$subNumber = "01-162-8";
function genCodeline($slipType, $amount, $rnumb, $subNumb)
{
$isEur = false;
if ((int) $slipType > 14) {
$isEur = true;
} else {
$amount = .5 * round((float) $amount/.5, 1);
}
if (!$isEur && $amount > 99999999.95) {
throw new Error("Invalid amount");
} elseif ($isEur && $amount > 99999999.99) {
throw new Error("Invalid amount");
}
$amountLine = sprintf("%010d", $amount * 100);
$checkSlAmount = recMod10($slipType.$amountLine);
if(!preg_match("/\d{2}-\d{1,6}-\d{1}/", $subNumb)){
throw new Error("Invalid subscriber number");
}
$subNumb = explode("-", $subNumb);
$fullSub = $subNumb[0].sprintf("%08d", $subNumb[1]).$subNumb[2];
$rnumb = preg_replace('/\s+/', '', $rnumb);
return $slipType.$amountLine.$checkSlAmount.">".$rnumb."+ ".$fullSub.">";
}
function recMod10($in)
{
$line = [0,9,4,6,8,2,7,1,3,5];
$carry = 0;
$chars = str_split($in);
foreach($chars as $char){
$carry = $line[($carry+intval($char))% 10];
}
return (10-$carry) %10;
}
echo genCodeline($slipType, $amount, $referenceNumber, $subNumber);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment