Skip to content

Commit 4dd8b6f

Browse files
added FirmaDigitale and PecMassiva API
1 parent c00a66b commit 4dd8b6f

5 files changed

Lines changed: 178 additions & 3 deletions

File tree

src/OpenApi.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ function __construct(array $scopes, string $username, string $apikey, $environme
2121
$domainsRealScopes = [];
2222
$prefix = $environment=="production"?"":$environment.".";
2323
$domains = [];
24+
//var_dump($scopes);exit;
2425
foreach($scopes as $s){
26+
if($s == NULL){
27+
continue;
28+
}
2529
if(is_array($s)){
2630
$domain = $s['domain'];
2731
$realScope = $s['mode'].":".$prefix.$s['domain']."/".$s['method'];
@@ -47,6 +51,7 @@ function __construct(array $scopes, string $username, string $apikey, $environme
4751
$this->prefix = $prefix;
4852
$this->scopes = $realScopes;
4953
$token = $this->getToken();
54+
5055
list($moduli,$nomi) = $this->getListaModuli();
5156
$this->clients = [];
5257
foreach($domains as $d){
@@ -97,7 +102,10 @@ private function getListaModuli(){
97102

98103

99104
$moduli['ws.firmadigitale.com'] = "\\OpenApi\\classes\\FirmaDigitale";
100-
$nomi['ws.firmadigitale.com'] = "FirmaDigitale";
105+
$nomi['ws.firmadigitale.com'] = "firmaDigitale";
106+
107+
$moduli['ws.pecmassiva.com'] = "\\OpenApi\\classes\\PecMassiva";
108+
$nomi['ws.pecmassiva.com'] = "pecMassiva";
101109
return array($moduli,$nomi);
102110
}
103111

src/classes/FirmaDigitale.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
namespace OpenApi\classes;
3+
class FirmaDigitale extends OpenApiBase {
4+
5+
/**
6+
* @param string $token Il token da utilizzare per il collegamento
7+
* @param array $scopes Array con la lista degli scope per cui il token è abilitato
8+
* @param object $cache Classe che gestisce la cahce, deve essere una classe che estende {@see OpenApi\clasess\utility\DummyCache} o comunque compatibile con essa (stessi metodi)
9+
*/
10+
function __construct(string $token, array $scopes, object $cache, string $prefix){
11+
parent::__construct($token, $scopes, $cache, $prefix);
12+
$this->basePath = "https://ws.firmadigitale.com";
13+
}
14+
15+
16+
function getModule($id, $add_header = TRUE){
17+
18+
$pdf = $this->connect("richiesta/$id/modulo", "GET", [], 0, FALSE, TRUE);
19+
if(!$add_header){
20+
return $pdf;
21+
}
22+
header("Content-type:application/pdf");
23+
header("Content-Disposition:attachment;filename={$id}.pdf");
24+
header('Content-Length: '.strlen( $pdf ));
25+
header('Cache-Control: public, must-revalidate, max-age=0');
26+
header('Pragma: public');
27+
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
28+
echo $pdf;
29+
}
30+
31+
32+
function requestProduct($data){
33+
$type = isset($data['tipo'])?$data['tipo']:NULL;
34+
$codice_prodotto = isset($data['codice_prodotto'])?$data['codice_prodotto']:NULL;
35+
$anagrafica = isset($data['anagrafica'])?$data['anagrafica']:NULL;
36+
$spedizione = isset($data['spedizione'])?$data['spedizione']:NULL;
37+
$urgenza = isset($data['urgenza'])?$data['urgenza']:NULL;
38+
$assistenza = isset($data['assistenza'])?$data['assistenza']:NULL;
39+
$callback = isset($data['callback'])?$data['callback']:NULL;
40+
41+
$params = [];
42+
if($anagrafica != NULL){
43+
if($type == "lettore"){
44+
$params['anagrafica_spedizione'] = $anagrafica;
45+
}else{
46+
$params['anagrafica'] = $anagrafica;
47+
}
48+
49+
}
50+
51+
if($spedizione != NULL && ($type == "lettore" || $type == "firma")){
52+
$params['spedizione'] = $spedizione;
53+
}
54+
55+
if($urgenza != NULL && ($type == "lettore" || $type == "firma")){
56+
$params['urgenza'] = $urgenza;
57+
}
58+
if($assistenza != NULL){
59+
$params['assistenza'] = $assistenza;
60+
}
61+
62+
if($callback != NULL){
63+
$params['callback'] = $callback;
64+
}
65+
$ret = $this->connect("richiesta/$codice_prodotto","POST",$params);
66+
return $ret;
67+
}
68+
69+
}

src/classes/OpenApiBase.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private function checkHasScope(string $url, string $type){
8989
*
9090
* @return mixed
9191
*/
92-
protected function connect(string $endpoint, $type = "GET", $param = [], $ttr = 0, $force = false){
92+
protected function connect(string $endpoint, $type = "GET", $param = [], $ttr = 0, $force = false, $addHeader = NULL){
9393
$url = $this->basePath;
9494
$url = str_replace("https://","https://".$this->prefix,$url);
9595
$url = str_replace("http://","http://".$this->prefix,$url);
@@ -119,7 +119,13 @@ protected function connect(string $endpoint, $type = "GET", $param = [], $ttr =
119119
curl_setopt($ch, CURLOPT_URL, $url);
120120
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
121121
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
122-
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Authorization: Bearer ".$this->token));
122+
$header = array("Authorization: Bearer ".$this->token);
123+
if($addHeader != NULL && is_array($addHeader) && count($addHeader)>0){
124+
$header = array_merge($header, $addHeader);
125+
}
126+
127+
128+
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
123129
curl_setopt($ch, CURLOPT_HEADER, 1);
124130
$response = curl_exec($ch);
125131
// var_dump($response);exit;

src/classes/PecMassiva.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
namespace OpenApi\classes;
3+
class PecMassiva extends OpenApiBase {
4+
5+
private string $username;
6+
private string $password;
7+
private bool $inizialized;
8+
9+
10+
11+
/**
12+
* @param string $token Il token da utilizzare per il collegamento
13+
* @param array $scopes Array con la lista degli scope per cui il token è abilitato
14+
* @param object $cache Classe che gestisce la cahce, deve essere una classe che estende {@see OpenApi\clasess\utility\DummyCache} o comunque compatibile con essa (stessi metodi)
15+
*/
16+
function __construct(string $token, array $scopes, object $cache, string $prefix){
17+
parent::__construct($token, $scopes, $cache, $prefix);
18+
$this->basePath = "https://ws.pecmassiva.com";
19+
$this->inizialized = FALSE;
20+
21+
}
22+
23+
function initialize(string $username, string $password){
24+
$this->username = $username;
25+
$this->password = $password;
26+
$this->inizialized = TRUE;
27+
}
28+
29+
function getStatus($messageId){
30+
if(!$this->inizialized){
31+
throw new \OpenApi\classes\exception\OpenApiPecMassivaException("class must initialized calling initialize function", 40011);
32+
}
33+
34+
35+
try{
36+
$header[] = 'x-username: '.$this->username;
37+
$header[] = 'x-password: '.$this->password;
38+
return $this->connect("send/$messageId","GET",[],0,false,$header);
39+
40+
}catch(\OpenApi\classes\exception\OpenApiConnectionsException $e){
41+
if(isset($e->getServerResponse()->message_id)){
42+
throw new \OpenApi\classes\exception\OpenApiPecMassivaException("error occurred connecting to SMTP: ".$e->getServerResponse()->message_id, 40012);
43+
}
44+
throw $e;
45+
46+
}
47+
}
48+
49+
function send($recipient, $subject, $body, $attachments = [], $sender = NULL){
50+
if(!$this->inizialized){
51+
throw new \OpenApi\classes\exception\OpenApiPecMassivaException("class must initialized calling initialize function", 40011);
52+
}
53+
$sender = $sender ? $sender : $this->username;
54+
55+
$params['username'] = $this->username;
56+
$params['password'] = $this->password;
57+
$params['recipient'] = $recipient;
58+
$params['subject'] = $subject;
59+
$params['body'] = $body;
60+
if(count($attachments)>0){
61+
$params['attachments'] = $attachments;
62+
}
63+
$params['sender'] = $sender;
64+
try{
65+
return $this->connect("send","POST",$params);
66+
}catch(\OpenApi\classes\exception\OpenApiConnectionsException $e){
67+
if(isset($e->getServerResponse()->message_id)){
68+
throw new \OpenApi\classes\exception\OpenApiPecMassivaException("error occurred connecting to SMTP: ".$e->getServerResponse()->message_id, 40012);
69+
}
70+
throw $e;
71+
72+
}
73+
74+
}
75+
76+
77+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace OpenApi\classes\exception;
3+
4+
/**
5+
* Gestisce le eccezioni relative alla richiesta del token, di seguito i codici errore ritornati:
6+
* 40011: Si è tentato di lanciare una operazione senza aver prima inizializzato la libreria con username e password
7+
* 40012: C'è stato un errore di connessione al server
8+
*/
9+
class OpenApiPecMassivaException extends OpenApiExceptionBase
10+
{
11+
public function __construct($message, $code = 0, \Exception $previous = null) {
12+
parent::__construct($message, $code, $previous);
13+
}
14+
15+
}

0 commit comments

Comments
 (0)