Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obsługa płatności masowych Alior Banku #232

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 65 additions & 20 deletions contrib/bin/lms-cashimport.php
Expand Up @@ -192,12 +192,13 @@ function parse_file($filename, $contents) {
if($patterns_cnt) foreach($patterns as $idx => $pattern)
{
$theline = $line;

if(strtoupper($pattern['encoding']) != 'UTF-8')
{
$theline = @iconv($pattern['encoding'], 'UTF-8//TRANSLIT', $theline);
}

$theline = str_replace('�',' ',$theline); //usuni�cie krzak�w alior-a

if (preg_match($pattern['pattern'], $theline, $matches))
break;
if (isset($pattern['pattern_sum']) && preg_match($pattern['pattern_sum'], $theline, $matches)) {
Expand All @@ -206,7 +207,6 @@ function parse_file($filename, $contents) {
}
$count++;
}

// line isn't matching to any pattern
if($count == $patterns_cnt)
{
Expand Down Expand Up @@ -443,25 +443,70 @@ function commit_cashimport()
$posts = imap_search($ih, chkconfig($CONFIG['cashimport']['use_seen_flag'], true) ? 'UNSEEN' : 'ALL');
if (!empty($posts))
foreach ($posts as $postid) {
$post = imap_fetchstructure($ih, $postid);
if ($post->type == 1) {
$parts = $post->parts;
//print_r($parts);
foreach ($parts as $partid => $part )
if ($part->ifdisposition && strtoupper($part->disposition) == 'ATTACHMENT' && $part->type == 0) {
$fname = $part->dparameters[0]->value;
$msg = imap_fetchbody($ih, $postid, $partid + 1);
if ($part->encoding == 3)
$msg = imap_base64($msg);
if (chkconfig($CONFIG['cashimport']['use_seen_flag'], true))
imap_setflag_full($ih, $postid, "\\Seen");
parse_file($fname, $msg);
if (chkconfig($CONFIG['cashimport']['autocommit']))
commit_cashimport();
}
}
if ($CONFIG['cashimport']['bank'] == 'aliorbank') {
$imap_structure = imap_fetchstructure($ih, $postid);
if ($imap_structure->type == 1) { //1 = multipart
$parts = $imap_structure->parts;
foreach ($parts as $partid => $part ){
if ($part->type == 1 && count($part->parts)){ // Czy element wiadomo�� zawiera zagnie�d�one cz�ci?
foreach ($part->parts as $multipartid => $multipart){ // Poszukiwanie za��cznika .7z
if ($multipart->type ==3 && strpos($multipart->dparameters[0]->value, '.7z')){
$attachments[$partid][filename] = $multipart->dparameters[0]->value;
$attachments[$partid][bytes] = $multipart->bytes;
$attachments[$partid][pos] = ($partid+1).".".($multipartid+1);
$attachments[$partid][id] = $postid;
}
}
}else{ // Poszukiwanie za��cznika .7z
if ($part->type == 3 && strpos($part->dparameters[0]->value, '.7z')){
$attachments[$partid][filename] = $part->dparameters[0]->value;
$attachments[$partid][bytes] = $part->bytes;
$attachments[$partid][pos] = $partid+1;
$attachments[$partid][id] = $postid;
}
}
}
}
if ($attachments){
foreach ($attachments as $attachment){ // zapis za��cznik�w
$filename = '/tmp/'.$attachment[filename];
$file = fopen($filename,'w');
stream_filter_append($file,'convert.base64-decode',STREAM_FILTER_WRITE);
if (chkconfig($CONFIG['cashimport']['use_seen_flag']))
imap_savebody ($ih, $file, $attachment[id], $attachment[pos] );
else
imap_savebody ($ih, $file, $attachment[id], $attachment[pos], FT_PEEK);
exec('7za e '. $filename .' -p'. $CONFIG['cashimport']['7zpasswd'] .' -o/tmp -y', $debug); // rozpakowanie pliku
//print_r($debug);
fclose($file);
$file_content = file_get_contents(str_replace('.7z', '.'. $CONFIG['cashimport']['raport_type'], $filename));
parse_file($attachment[filename], $file_content);
if (chkconfig($CONFIG['cashimport']['autocommit']))
commit_cashimport();
}
}
}
else{
$post = imap_fetchstructure($ih, $postid);
if ($post->type == 1) {
$parts = $post->parts;
//print_r($parts);
foreach ($parts as $partid => $part )
if ($part->ifdisposition && strtoupper($part->disposition) == 'ATTACHMENT' && $part->type == 0) {
$fname = $part->dparameters[0]->value;
$msg = imap_fetchbody($ih, $postid, $partid + 1);
if ($part->encoding == 3)
$msg = imap_base64($msg);
if (chkconfig($CONFIG['cashimport']['use_seen_flag'], true))
imap_setflag_full($ih, $postid, "\\Seen");
parse_file($fname, $msg);
if (chkconfig($CONFIG['cashimport']['autocommit']))
commit_cashimport();
}
}
}
}

imap_close($ih);

?>
?>
60 changes: 60 additions & 0 deletions contrib/platnosci_masowe/alior bank/cashimportcfg-aliorbank.php
@@ -0,0 +1,60 @@
<?php

/*
* LMS version 1.11-git
*
* (C) Copyright 2001-2013 LMS Developers
*
* Please, see the doc/AUTHORS for more information about authors!
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* $Id$
*/
$account = '249010289000000388'; // Prefix numeru rozliczeniowego naszej us³ugi MPT. To jest ten sam numer, który wpisujemy w konfiguracji LMS. Np. numer indywidualnego konta to: 55 2490 1028 9000 0003 8800 0001, prefix bêdzie nastêpuj¹cy: 249010289000000388

$patterns[] = array(
'id' => NULL, // import source identifier (from 'cashsources' table)
'pattern' => '/[0-9]+,([0-9]{4})([0-9]{2})([0-9]{2}),[0-9]{2}'.$account.'([0-9]{6}),[0-9]{4},([0-9]+),([A-Z0-9 ]+|),"([^"]*)","([^"]*)","([^"]*)"/'',
'pid' => 4, // customer ID position in expression
// if zero - we try to search ID by regexp,
// invoice number or customer name and forename in entire line - nie rozumiem do konca: czy jesli wartosc do przypisania bedzie wynosila 0 czy jesli pozostanie na aktualnym zerowym substringu/submatchu
// 'pname' => , // name position old: 8
'plastname' => 6, // forename position old: 9
'pvalue' => 5, // value position
'pcomment' => 7, // operation comment position
// 'pdate' => 2, // date position
// 'date_regexp' => '/([0-9]{4})([0-9]{2})([0-9]{2})/', // date format CHANGED (yyyymmdd)
'pday' => 3,
'pmonth' => 2,
'pyear' => 1,
'pid_regexp' => '/","\d*(\d{4})"/', // if 'pid' is not specified
// try to find it by regexp
// Komentarz takze wyzej. Jesli all good to pid = cztery ostatnie cyfry nr nadawcy
'invoice_regexp' => '(\d{4})(\d{2}).*?(\d{4})"',// format of invoice number
// default %N/LMS/%Y CHANGED to yyyymmID
'pinvoice_number' => 3, // position of invoice number in $invoice_regexp
'pinvoice_year' => 1, // year position in $invoice_regexp
'pinvoice_month' => 2, // month position in $invoice_regexp

'encoding' => 'Windows-1250', // imported data encoding (for conversion)

'modvalue' => 0.01, // if not zero do value = value * modvalue
'use_line_hash' => TRUE, // create md5 hash for whole import line instead of
// time, value, customer name and comment
'line_idx_hash' => TRUE, // include line number into hash data
);

?>
36 changes: 36 additions & 0 deletions contrib/platnosci_masowe/alior bank/instrukcja.txt
@@ -0,0 +1,36 @@
OPIS:

Skrypt obsługi płatności MPT Alior Banku.


INSTALACJA:


cashimportcfg-aliorbank.php należy edytować w 26 lini i uzupełnić o numer rozliczeniowy banku dla MPT.
Następnie umieścić np. w lms/modules/

W sekcji phpui: import_config jako wartość podać ścieżkę do pliku cashimportcfg-aliorbank.php

Sekcja finances: cashimport_checkinvoices, jako wartość: true - dzięki temu faktury, będą rozliczane. W przeciwym wypadku rozliczane będzie tylko saldo.

W contrib/bin znajduje się lms-cashimport.php, który pobiera maile poprzez IMAP, rozpakowuje 7zip i rozlicza płatności. Najlepiej dodać go do cron-a.

W LMS -> Konfiguracja -> Źródła importu dodać nowe źródło o nazwie np. "Alior bank".


KONFIGURACJA LMS.ini

[cashimport]
server = serwer.pl:993/novalidate-cert/ssl // (lub inna składnia, w zależności czy używamy SSL)
username = "platnosci@serwer.pl" // Login do konta email
password = "haslo_do_konta_email" // Hasło do email-a
autocommit = true // Ta opcja sprawia, że dane wczytane przez skrypt od razu będą rozliczane w LMS-a. W przeciwnym wypadku będą zimportowane i nie rozliczone w Finanse > Import
use_seen_flag = true // Oznacza email-e jako odczytane
bank = aliorbank // To chyba nie wymaga komnentarza :)
7zpasswd = HaslodoMPT24 // Hasło wypakowania załącznika .7z
raport_type = rpt // Typ raportu jaki wybraliśmy w banku. W tej chwili pattern obsługuje tylko RPT.


Voilla! Wszystko powinno śmigać. :)
Na testy warto zrobić sobie drugą baze danych i sprawdzić czy wszystko jest OK, ewentualnie nie dawać autocommit = true na początku.
Dodatkowo najlepiej najpierw przykładowy plik z banku wczytać ręcznie poprzez LMS > Finanse > Importuj i zobaczyć czy nie będzie błędów.