| 1 |
<?php |
| 2 |
|
| 3 |
namespace Moloni\Services\Documents; |
| 4 |
|
| 5 |
use Moloni\Curl; |
| 6 |
use Moloni\Exceptions\APIException; |
| 7 |
|
| 8 |
class DownloadDocument |
| 9 |
{ |
| 10 |
private $documentId; |
| 11 |
|
| 12 |
public function __construct($documentId) |
| 13 |
{ |
| 14 |
$this->documentId = $documentId; |
| 15 |
|
| 16 |
$this->run(); |
| 17 |
} |
| 18 |
|
| 19 |
public function run(): void |
| 20 |
{ |
| 21 |
try { |
| 22 |
$result = Curl::simple('documents/getPDFLink', [ |
| 23 |
'document_id' => $this->documentId |
| 24 |
]); |
| 25 |
|
| 26 |
if (isset($result['url'])) { |
| 27 |
$downloadUrl = 'https://www.moloni.pt/downloads/index.php?action=getDownload&'; |
| 28 |
$downloadUrl .= substr($result['url'], strpos($result['url'], '?') + 1); |
| 29 |
$downloadUrl .= '&e=wordpress.auto.download@moloni.pt'; |
| 30 |
$downloadUrl .= '&t=n'; |
| 31 |
|
| 32 |
header('Location: ' . $downloadUrl); |
| 33 |
} else { |
| 34 |
$this->showError(__('Documento não existe')); |
| 35 |
} |
| 36 |
} catch (APIException $e) { |
| 37 |
$this->showError(__('Erro a obter documento')); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
private function showError($message): void |
| 42 |
{ |
| 43 |
echo "<script>"; |
| 44 |
echo " alert('" . esc_js($message) . "');"; |
| 45 |
echo " window.close();"; |
| 46 |
echo "</script>"; |
| 47 |
} |
| 48 |
} |
| 49 |
|