require_once("MedodoDiPagamento.php");
class PausePay extends MedodoDiPagamento{
var $nome_metodo = "PausePay";
var $apiUri = "https://api.pausepay.it/";
var $apiUri_demo = "https://test-api.pausepay.it/";
var $apiKey = "4a7c454e958faa2ee9e368fc97c8aba14c93414d349cf3f82a6403b206470cfe";
var $apiKey_demo = "4a7c454e958faa2ee9e368fc97c8aba14c93414d349cf3f82a6403b206470cfe";
var $causale = "";
var $buyerInfo = [];
var $items = [];
var $errNoBuyer = "Non è stato possibile recuperare le informazioni sulla tua anagrafica";
var $errMissingBueryData = "Alcune informazioni sulla tua anagrafica non risultano compilate";
var $errMissingItems = "Non è stato possibile recuperare le informazioni sui prodotti";
var $errItemsUncomplete = "Alcuni prodotti non sono stati compilati correttamente";
var $controllaConfermatoInStep = [];
function demoInfo(){
return "
Non è possibile registrare aziende in ambiente demo, vanno quindi usate queste credenziali
Username: Martino.Bianchi1269592030633719343@fly.sbx
Password: 1OpDmoHKmi
Sulla richiesta bancaria usare i seguenti dati per il login:
Username: TEST00
Password: numeri random
Per testare i 4 flussi disponibili:
";
}
function ignoreSetupError(){
if(in_array($_GET[$this->step_var], array('webhook','s2s'))) return true;
return false;
}
function auto(){
if($this->demo){
$this->apiUri = $this->apiUri_demo;
$this->apiKey = $this->apiKey_demo;
}
// $this->importo = number_format(str_replace(",", ".", $this->importo), 2, ".", "");
switch($_GET[$this->step_var]){
default:
if(!$this->buyerInfo || !is_array($this->buyerInfo)){
$stepVarError = "errNoBuyer";
} else if(!$this->buyerInfo["name"] || !$this->buyerInfo["vatCode"] || !$this->buyerInfo["email"] || !$this->buyerInfo["pec"]){
$stepVarError = "errMissingBueryData";
} else if(!$this->items || !is_array($this->items)){
$stepVarError = "errMissingItems";
} else {
$allItemsFilled = true;
foreach($this->items as $item){
if(!$item["description"] || !$item["quantity"] || !$item["amount"]) $stepVarError = "errItemsUncomplete";
}
}
if($stepVarError){
$this->Procedura->redirect($this->Procedura->genera_link_agg([$this->step_var=>$stepVarError]));
} else {
$url = $this->Procedura->genera_link_agg(array($this->step_var=>"return_ok"));
$this->Procedura->iniziato($this->nome_metodo);
$orderInfo = $this->creaOrdine();
if($orderInfo->url && $orderInfo->id){
$identificativoSaved = db_q("UPDATE " . $this->Procedura->tabella_log . " SET identificativo_pagamento = '" . addslashes((string) $orderInfo->id) . "' WHERE identificativo_ordine = '" . addslashes($this->identificativo) . "'");
if($identificativoSaved){
$this->Procedura->redirect((string) $orderInfo->url);
} else {
$this->Procedura->redirect($this->Procedura->genera_link_agg([$this->step_var=>"error"]));
}
} else {
$this->Procedura->redirect($this->Procedura->genera_link_agg([$this->step_var=>"error"]));
}
}
break;
case 'return_ok':
return $this->Procedura->concluso();
case 'return_ko':
return $this->Procedura->annulla();
case 'errNoBuyer':
case 'errMissingBueryData':
case 'errMissingItems':
case 'errItemsUncomplete':
case 'error':
$errorMessage = $this->$_GET[$this->step_var] ? $this->$_GET[$this->step_var] : null;
return $this->Procedura->errore($errorMessage);
case 's2s':
$confermato = false;
if($_GET["test"]){
$orderId = $_GET["testOrder"] ? $_GET["testOrder"] : "CC1DE4C0-D284-4520-B278-1DFBFE1026C8";
$eventType = $_GET["testEvent"] ? $_GET["testEvent"] : "order.ok";
$payload = '{"eventType":"'. $eventType . '", "eventID":"1235051235156", "orderID":"' . $orderId . '", "createdAt":"2023-05-10 09:37:26"}';
} else {
$payload = file_get_contents("php://input");
}
if($payload){
// mail("a.toce@tnx.it", "Ricevuta conferma payload PausePay", print_r($payload, true));
$jsonPayload = json_decode($payload);
if($jsonPayload){
$orderId = (string) $jsonPayload->orderID;
$identificativo = db_qrs("SELECT identificativo_ordine FROM " . $this->Procedura->tabella_log . " WHERE identificativo_pagamento = '" . addslashes($orderId) . "'");
if($identificativo){
$confermato = true;
$statoConferma = (string)$jsonPayload->eventType;
if($statoConferma == "order.ok"){
$this->Procedura->confermato($identificativo);
} else {
$this->Procedura->errore();
}
}
}
if(!$confermato) trigger_error("Conferma pagamento PausePay: non sono riuscito a recuperare l'identificativo di pagamento");
}
break;
}
}
function creaOrdine(){
$number = $this->identificativo;
if($this->demo){
$number = ($_GET["buyerType"] ? $_GET["buyerType"] : "YC") . "-" . $this->identificativo . "#IT06436170481";
$this->buyerInfo["vatCode"] = "IT82500048660";
}
$createOrderJson = [];
$createOrderJson["amount"] = (float) $this->importo;
$createOrderJson["number"] = $number;
$createOrderJson["remittance"] = $this->causale ? $this->causale : $this->identificativo;
$createOrderJson["issueDate"] = date("Y-m-d");
$createOrderJson["description"] = $this->descrizione;
$createOrderJson["allowToEditRemittance"] = true;
$createOrderJson["okRedirect"] = $this->Procedura->genera_link_agg(array($this->step_var=>"return_ok"));
$createOrderJson["koRedirect"] = $this->Procedura->genera_link_agg(array($this->step_var=>"return_ko"));
$createOrderJson["buyerInfo"] = $this->buyerInfo;
$createOrderJson["items"] = $this->items;
$createOrderJson["allowSCTPayment"] = false;
$createOrderHeader = [];
$createOrderHeader[] = "accept: application/json";
$createOrderHeader[] = "X-PausePay-Key: " . $this->apiKey;
$createOrderHeader[] = "Content-Type: application/json";
$createOrderRequest = curl_init();
curl_setopt($createOrderRequest, CURLOPT_URL, $this->apiUri . "order");
curl_setopt($createOrderRequest, CURLOPT_HTTPHEADER, $createOrderHeader);
curl_setopt($createOrderRequest, CURLOPT_RETURNTRANSFER, true);
curl_setopt($createOrderRequest, CURLOPT_VERBOSE, true);
curl_setopt($createOrderRequest, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($createOrderRequest, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($createOrderRequest, CURLOPT_POST, 1);
curl_setopt($createOrderRequest, CURLOPT_POSTFIELDS, json_encode($createOrderJson));
$createOrderResponse = curl_exec($createOrderRequest);
$createOrderInfo = curl_getinfo($createOrderRequest);
if($createOrderInfo["http_code"] == 400){
return null;
} else {
return json_decode($createOrderResponse);
}
}
}
?>