Skip to content

Commit 0f6713f

Browse files
committed
feat: aggiunto servizio di verifica iban tramite ibanapi
1 parent 283d10c commit 0f6713f

4 files changed

Lines changed: 366 additions & 3 deletions

File tree

modules/banche/actions.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,51 @@
114114
]);
115115

116116
break;
117+
118+
case 'check_balance':
119+
$api_key = filter('api_key');
120+
121+
// Verifica il credito residuo su ibanapi.com
122+
$ch = curl_init();
123+
curl_setopt($ch, CURLOPT_URL, setting('Endpoint ibanapi.com').'/v1/balance?api_key='.$api_key);
124+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
125+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
126+
$result = curl_exec($ch);
127+
128+
if (curl_errno($ch)) {
129+
http_response_code(500);
130+
echo json_encode(['error' => 'Errore durante la connessione a ibanapi.com']);
131+
exit;
132+
}
133+
curl_close($ch);
134+
135+
echo $result;
136+
break;
137+
138+
case 'verify_iban':
139+
$iban = filter('iban');
140+
$type = filter('type');
141+
$api_key = filter('api_key');
142+
143+
// Verifica l'IBAN tramite ibanapi.com
144+
$ch = curl_init();
145+
$endpoint = ($type === 'bank') ? setting('Endpoint ibanapi.com').'/v1/validate' : setting('Endpoint ibanapi.com').'/v1/validate-basic';
146+
curl_setopt($ch, CURLOPT_URL, $endpoint);
147+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
148+
curl_setopt($ch, CURLOPT_POST, 1);
149+
curl_setopt($ch, CURLOPT_POSTFIELDS, ['iban' => $iban, 'api_key' => $api_key]);
150+
$headers = [];
151+
$headers[] = 'Accept: application/json';
152+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
153+
$result = curl_exec($ch);
154+
155+
if (curl_errno($ch)) {
156+
http_response_code(500);
157+
echo json_encode(['error' => 'Errore durante la connessione a ibanapi.com']);
158+
exit;
159+
}
160+
curl_close($ch);
161+
162+
echo $result;
163+
break;
117164
}

modules/banche/add.php

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
include_once __DIR__.'/../../core.php';
2121

2222
$id_anagrafica = filter('id_anagrafica');
23+
$ibanapi_key = setting('Api key ibanapi.com');
24+
$endpoint = setting('Endpoint ibanapi.com');
2325

2426
echo '
2527
<form action="" method="post" id="add-form">
@@ -38,7 +40,7 @@
3840
3941
<div class="row">
4042
<div class="col-md-8">
41-
{[ "type": "text", "label": "'.tr('IBAN').'", "name": "iban", "required": "1", "class": "alphanumeric-mask", "maxlength": 32, "value": "$iban$" ]}
43+
{[ "type": "text", "label": "'.tr('IBAN').'", "name": "iban", "required": "1", "class": "alphanumeric-mask", "maxlength": 32, "value": "$iban$", "icon-after": "'.(!empty($ibanapi_key) && !empty($endpoint)?'<a class=\'fa fa-search clickable\' id=\'check-iban\'></a>':'<span class=\'tip\' title=\'Da impostazioni sezione API è possibile attivare la verifica iban tramite ibanapi.com\'><i class=\'fa fa-search text-danger clickable\'></i></span>').'" ]}
4244
</div>
4345
4446
<div class="col-md-4">
@@ -179,4 +181,156 @@ function compilaCampi(values) {
179181
}
180182
}
181183
}
184+
185+
// Funzione per verificare l'IBAN tramite ibanapi.com
186+
function checkIban() {
187+
let value = iban.get();
188+
if (value.length < 15) {
189+
swal("<?php echo tr('Errore'); ?>", "<?php echo tr('Inserire un IBAN valido'); ?>", "error");
190+
return;
191+
}
192+
193+
// Verifica il credito residuo e la data di scadenza
194+
$.ajax({
195+
url: globals.rootdir + '/actions.php',
196+
data: {
197+
id_module: id_module,
198+
op: "check_balance",
199+
api_key: "<?php echo $ibanapi_key; ?>"
200+
},
201+
type: 'GET',
202+
dataType: "json",
203+
success: function(balance) {
204+
if (new Date(balance.data.expiry_date) < new Date()) {
205+
swal("<?php echo tr('Errore'); ?>", "<?php echo tr('La chiave API è scaduta'); ?>", "error");
206+
return;
207+
}
208+
209+
// Determina quale tipo di verifica utilizzare in base al credito residuo
210+
let verificationType = '';
211+
if (balance.data.bank_balance > 0) {
212+
verificationType = 'bank';
213+
} else if (balance.data.basic_balance > 0) {
214+
verificationType = 'basic';
215+
} else {
216+
swal("<?php echo tr('Errore'); ?>", "<?php echo tr('Credito insufficiente per la verifica IBAN'); ?>", "error");
217+
return;
218+
}
219+
220+
// Verifica l'IBAN
221+
$.ajax({
222+
url: globals.rootdir + '/actions.php',
223+
data: {
224+
id_module: id_module,
225+
op: "verify_iban",
226+
iban: value,
227+
type: verificationType,
228+
api_key: "<?php echo $ibanapi_key; ?>"
229+
},
230+
type: 'GET',
231+
dataType: "json",
232+
success: function(response) {
233+
// Verifica se l'IBAN è valido (result: 200 indica validità)
234+
if (response.result === 200) {
235+
// Compila i campi se disponibili
236+
if (verificationType === 'bank' && response.data && response.data.bank) {
237+
if (response.data.bank.bic) {
238+
$('#modals > div #bic').val(response.data.bank.bic);
239+
}
240+
if (response.data.bank.bank_name) {
241+
$('#modals > div #nome').val(response.data.bank.bank_name);
242+
}
243+
}
244+
245+
// Formatta le informazioni per l'alert
246+
let infoHtml = "<p><strong><?php echo tr('IBAN valido'); ?></strong></p>";
247+
248+
// Informazioni sul paese
249+
if (response.data) {
250+
infoHtml += "<p><strong><?php echo tr('Informazioni paese'); ?>:</strong></p>";
251+
infoHtml += "<p><?php echo tr('Paese'); ?>: " + response.data.country_code + " - " + response.data.country_name + "</p>";
252+
infoHtml += "<p><?php echo tr('Valuta'); ?>: " + response.data.currency_code + "</p>";
253+
infoHtml += "<p>SEPA: " + response.data.sepa_member + "</p>";
254+
}
255+
256+
// Informazioni sulla banca
257+
if (response.data && response.data.bank) {
258+
infoHtml += "<p><strong><?php echo tr('Informazioni banca'); ?>:</strong></p>";
259+
infoHtml += "<p><?php echo tr('Nome banca'); ?>: " + response.data.bank.bank_name + "</p>";
260+
261+
if (response.data.bank.bic) {
262+
infoHtml += "<p>BIC: " + response.data.bank.bic + "</p>";
263+
}
264+
265+
if (response.data.bank.address) {
266+
infoHtml += "<p><?php echo tr('Indirizzo'); ?>: " + response.data.bank.address + "</p>";
267+
}
268+
269+
if (response.data.bank.city) {
270+
let location = response.data.bank.city;
271+
if (response.data.bank.zip) {
272+
location += " - " + response.data.bank.zip;
273+
}
274+
if (response.data.bank.state) {
275+
location += " (" + response.data.bank.state + ")";
276+
}
277+
infoHtml += "<p><?php echo tr('Località'); ?>: " + location + "</p>";
278+
}
279+
}
280+
281+
if(response.data.sepa){
282+
infoHtml += "<p><strong><?php echo tr('Informazioni sepa'); ?>:</strong></p>";
283+
infoHtml += "<div class='row'>";
284+
285+
// Funzione per creare un badge SEPA
286+
function createSepaBadge(value, label) {
287+
var status = value === 'Yes';
288+
var badgeClass = status ? 'success' : 'danger';
289+
return "<div class='col-md-6 mb-2'>"
290+
+ "<div class='card h-100'>"
291+
+ "<div class='card-body p-1 d-flex align-items-center'>"
292+
+ "<span class='small' style='font-size:9pt'>" + label + "</span>"
293+
+ "<span class='badge badge-" + badgeClass + " ml-auto small' style='font-size: 0.75rem;'>" + (status ? '<?php echo tr("Attivo"); ?>' : '<?php echo tr("Non attivo"); ?>') + "</span>"
294+
+ "</div></div></div>";
295+
}
296+
297+
// Crea i badge per ogni servizio SEPA
298+
infoHtml += createSepaBadge(response.data.sepa.sepa_credit_transfer, "SEPA Credit Transfer");
299+
infoHtml += createSepaBadge(response.data.sepa.sepa_credit_transfer_inst, "SEPA Credit Transfer Instant");
300+
infoHtml += createSepaBadge(response.data.sepa.sepa_direct_debit, "SEPA Direct Debit");
301+
infoHtml += createSepaBadge(response.data.sepa.sepa_sdd_core, "SEPA Direct Debit Core");
302+
infoHtml += createSepaBadge(response.data.sepa.sepa_b2b, "SEPA B2B");
303+
infoHtml += createSepaBadge(response.data.sepa.sepa_card_clearing, "SEPA Card Clearing");
304+
305+
infoHtml += "</div></div>";
306+
}
307+
308+
swal({
309+
title: "<?php echo tr('Verifica completata'); ?>",
310+
html: infoHtml,
311+
type: "success"
312+
});
313+
314+
// Aggiorna i campi del form
315+
scomponiIban();
316+
} else {
317+
swal("<?php echo tr('IBAN non valido'); ?>", response.message || "<?php echo tr('Formato IBAN non valido'); ?>", "error");
318+
}
319+
},
320+
error: function() {
321+
swal("<?php echo tr('Errore'); ?>", "<?php echo tr('Errore durante la verifica dell\'IBAN'); ?>", "error");
322+
}
323+
});
324+
},
325+
error: function() {
326+
swal("<?php echo tr('Errore'); ?>", "<?php echo tr('Errore durante la verifica del credito'); ?>", "error");
327+
}
328+
});
329+
}
330+
331+
// Aggiungi l'evento click al pulsante di verifica IBAN
332+
$("#check-iban").on("click", function(e) {
333+
e.preventDefault();
334+
checkIban();
335+
});
182336
</script>

0 commit comments

Comments
 (0)