= 0.01 ? $value : null; } /** * Suggested amounts typed on the block, separated by spaces or semicolons ("5 10 20", "5; 12,50"). * A comma with no space after it is a decimal comma. Invalid ones are dropped, duplicates merged. * * @return float[] */ function parse_amounts( string $amounts ): array { $out = array(); foreach ( preg_split( '/;|,(?=\s)|\s+/', $amounts ) as $part ) { $value = parse_amount( $part ); if ( null !== $value ) { $out[ number_format( $value, 2, '.', '' ) ] = $value; } } return array_slice( array_values( $out ), 0, 6 ); } /** * The remainder of a string of letters and digits, read as a number, divided by 97 (ISO 7064), * with letters counting as 10 (A) to 35 (Z). Used by IBANs and RF references. */ function mod97( string $chars ): int { $digits = ''; foreach ( str_split( $chars ) as $char ) { $digits .= ctype_alpha( $char ) ? (string) ( ord( $char ) - 55 ) : $char; } $rest = 0; foreach ( str_split( $digits, 7 ) as $chunk ) { $rest = (int) ( $rest . $chunk ) % 97; } return $rest; } /** * A structured creditor reference (ISO 11649) from what the block was given: a complete * "RF18 5390 0754 7034" is checked and kept, and the site's own reference ("2027MEMBERS") gets * "RF" and its check digits. '' when it is empty, too long, or a mistyped RF reference. */ function rf_reference( string $reference ): string { $reference = strtoupper( (string) preg_replace( '/\s+/', '', $reference ) ); if ( preg_match( '/^RF\d{2}[A-Z0-9]{1,21}$/', $reference ) ) { return 1 === mod97( substr( $reference, 4 ) . substr( $reference, 0, 4 ) ) ? $reference : ''; } if ( ! preg_match( '/^[A-Z0-9]{1,21}$/', $reference ) ) { return ''; } return sprintf( 'RF%02d%s', 98 - mod97( $reference . 'RF00' ), $reference ); } /** * "RF185390075470 34" to "RF18 5390 0754 7034", as banks print it. */ function format_reference( string $reference ): string { return trim( chunk_split( $reference, 4, ' ' ) ); } /** * The payload for one account, or '' when there is not enough for a valid code. * * @param array{recipient: string, iban: string, bic: string} $account As from accounts(). * @param string $purpose Payment details. * @param float|null $amount Euros; null leaves it to the payer. * @param string $reference A valid RF reference, which replaces the payment details. */ function epc_payload( array $account, string $purpose, ?float $amount = null, string $reference = '' ): string { $recipient = trim( $account['recipient'] ); $iban = $account['iban']; if ( '' === $recipient || ! iban_valid( $iban ) ) { return ''; } $lines = array( 'BCD', // Service tag. '002', // Version: the BIC is optional. '1', // Character set: UTF-8. 'SCT', // SEPA Credit Transfer. $account['bic'], mb_substr( $recipient, 0, 70 ), $iban, null !== $amount ? 'EUR' . number_format( $amount, 2, '.', '' ) : '', // Amount, or left to the payer. '', // Purpose code. $reference, // Structured reference. '' === $reference ? mb_substr( $purpose, 0, 140 ) : '', // Unstructured remittance: one or the other. ); return rtrim( implode( "\n", $lines ), "\n" ); }