Skip to content
Open
Changes from all commits
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
84 changes: 46 additions & 38 deletions CRM/Bic/Parser/CH.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,69 @@
| written permission from the original author(s). |
+--------------------------------------------------------*/

declare(strict_types = 1);
require_once 'CRM/Bic/Parser/Parser.php';

/**
* Abstract class defining the basis for national bank info parsers
*/
class CRM_Bic_Parser_CH extends CRM_Bic_Parser_Parser {

public static $page_url = 'https://api.six-group.com/api/epcd/bankmaster/v2/public/downloads/bcbankenstamm';
static $page_url = 'https://api.six-group.com/api/epcd/bankmaster/v3/bankmaster_V3.csv';

public function update() {
// first, download the page
$banks = [];
$data = $this->downloadFile(CRM_Bic_Parser_CH::$page_url);
if (empty($data)) {
return $this->createParserOutdatedError(ts("Couldn't download data set"));
static $country_code = 'CH';

public function update()
{
// First, download the file
$file_name = sys_get_temp_dir() . '/CH-banks.csv';
$downloaded_file = $this->downloadFile(CRM_Bic_Parser_CH::$page_url);

if (empty($downloaded_file)) {
return $this->createParserOutdatedError(ts("Couldn't download data file"));
}

$lines = preg_split('/\n/', $data);
// store file
file_put_contents($file_name, $downloaded_file);
unset($downloaded_file);

// Open and read CSV file
if (($handle = fopen($file_name, "r")) === false) {
return $this->createParserOutdatedError(ts("Couldn't open data file"));
}

foreach ($lines as $line) {
$bc_code = substr($line, 16, 5);
$bic = trim(substr($line, 284, 11));
$name = trim(substr($line, 54, 60));
$address = substr($line, 184, 4) . ' ' . trim(substr($line, 194, 35));
// Skip header row
fgetcsv($handle, 1000, ';');

// we only want branches with BICs
if (empty($bic)) {
continue;
}
$banks = [];
while (($data = fgetcsv($handle, 1000, ';')) !== false) {
// skip entries with no bic
if (empty($data[14])) continue;

// encode names
$name = mb_convert_encoding($name, 'UTF-8', 'ISO-8859-1');
$address = mb_convert_encoding($address, 'UTF-8', 'ISO-8859-1');
// Process row
$bank = array(
'value' => $data[0],
'name' => $data[14],
'label' => $data[8],
'description' => $data[8],
);

$banks[$bc_code] = [
'value' => $bc_code,
'name' => $bic,
'label' => $name,
'description' => $address,
];
$banks[] = $bank;
}

// // finally, update DB
return $this->updateEntries('CH', $banks);
fclose($handle);
unlink($file_name);

// Finally, update DB
return $this->updateEntries(CRM_Bic_Parser_CH::$country_code, $banks);
}

/**
*
* Extracts the National Bank Identifier from an Austrian IBAN.
*
/*
* Extracts the National Bank Identifier from an IBAN.
*/
public function extractNBIDfromIBAN($iban) {
return [
substr($iban, 4, 5),
];
public function extractNBIDfromIBAN($iban)
{
return array(
substr($iban, 4, 5),
);
}

}